add support for objects

This commit is contained in:
JMARyA 2023-10-27 09:46:26 +02:00
parent 9fa39fc1c2
commit 8ef293a576
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
2 changed files with 33 additions and 8 deletions

View file

@ -246,12 +246,37 @@ impl Document {
} }
_ => {} _ => {}
} }
let split_path: Vec<_> = key.split('.').collect();
if split_path.len() > 1 {
let data = self
.frontmatter
.as_mapping()
.unwrap()
.get(split_path.first().unwrap());
if data.is_none() {
return String::new();
}
let mut data = data.unwrap();
for path in &split_path[1..] {
let data_opt = data.as_mapping().unwrap().get(path);
if data_opt.is_none() {
return String::new();
}
data = data_opt.unwrap();
}
stringify(data)
} else {
self.frontmatter self.frontmatter
.as_mapping() .as_mapping()
.unwrap() .unwrap()
.get(key) .get(key)
.map_or_else(String::new, stringify) .map_or_else(String::new, stringify)
} }
}
} }
fn stringify(val: &serde_yaml::Value) -> String { fn stringify(val: &serde_yaml::Value) -> String {
@ -260,8 +285,9 @@ fn stringify(val: &serde_yaml::Value) -> String {
serde_yaml::Value::Bool(b) => b.to_string(), serde_yaml::Value::Bool(b) => b.to_string(),
serde_yaml::Value::Number(n) => n.to_string(), serde_yaml::Value::Number(n) => n.to_string(),
serde_yaml::Value::String(s) => s.to_owned(), serde_yaml::Value::String(s) => s.to_owned(),
serde_yaml::Value::Sequence(_) => serde_json::to_string(&val).unwrap(), serde_yaml::Value::Sequence(_) | serde_yaml::Value::Mapping(_) => {
serde_yaml::Value::Mapping(_o) => todo!(), serde_json::to_string(&val).unwrap()
}
serde_yaml::Value::Tagged(_) => unimplemented!(), serde_yaml::Value::Tagged(_) => unimplemented!(),
} }
} }

View file

@ -6,7 +6,6 @@ mod args;
// TODO : Add documentation comments // TODO : Add documentation comments
// TODO : Add tests // TODO : Add tests
// TODO : Add GROUP BY Function
fn main() { fn main() {
env_logger::init(); env_logger::init();