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,6 +246,30 @@ 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
.as_mapping()
.unwrap()
@ -253,6 +277,7 @@ impl Document {
.map_or_else(String::new, stringify)
}
}
}
fn stringify(val: &serde_yaml::Value) -> String {
match val {
@ -260,8 +285,9 @@ fn stringify(val: &serde_yaml::Value) -> String {
serde_yaml::Value::Bool(b) => b.to_string(),
serde_yaml::Value::Number(n) => n.to_string(),
serde_yaml::Value::String(s) => s.to_owned(),
serde_yaml::Value::Sequence(_) => serde_json::to_string(&val).unwrap(),
serde_yaml::Value::Mapping(_o) => todo!(),
serde_yaml::Value::Sequence(_) | serde_yaml::Value::Mapping(_) => {
serde_json::to_string(&val).unwrap()
}
serde_yaml::Value::Tagged(_) => unimplemented!(),
}
}

View file

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