From 2dd477b667d8e23ae1dc0c9306e69491a5756074 Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Tue, 31 Dec 2024 17:07:11 -0800 Subject: [PATCH 1/3] append and set should have parity --- src/main.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9c5ddb9..cf09e0e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -81,7 +81,7 @@ pub enum Command { /// The key to look for. Use dots as path separators. Must key: Keyspec, /// The new value. - value: String, + value: TomlVal, /// The toml file to read from. Omit to read from stdin. file: Option, }, @@ -296,7 +296,7 @@ pub fn set_key( pub fn append_value( toml: &mut Document, dotted_key: &Keyspec, - value: &str, + value: &TomlVal, ) -> Result { let mut node: &mut Item = toml.as_item_mut(); let iterator = dotted_key.subkeys.iter(); @@ -315,7 +315,7 @@ pub fn append_value( node.or_insert(Item::Value(Value::Array(toml_edit::Array::new()))) .as_array_mut() .ok_or_else(|| anyhow::anyhow!("unable to append to a non-array at {}", dotted_key))? - .push(value); + .push(value.inner.clone()); Ok(original) } @@ -487,7 +487,7 @@ mod tests { .expect("test doc should be valid toml"); let key = Keyspec::from_str("testcases.fruits").expect("test key should be valid"); - let item = append_value(&mut doc, &key, "orange") + let item = append_value(&mut doc, &key, &TomlVal::from_str("orange").unwrap()) .expect("expected to be able to insert value 'orange'"); let formatted = format_toml(&item); assert_eq!( @@ -508,14 +508,14 @@ mod tests { let key = Keyspec::from_str("testcases.these.are.not.fruits").expect("test key should be valid"); - let item = append_value(&mut doc, &key, "leek") + let item = append_value(&mut doc, &key, &TomlVal::from_str("leek").unwrap()) .expect("expected to be able to insert value 'leek'"); assert!(item.is_none()); assert!(doc .to_string() .contains(r#"these = { are = { not = { fruits = ["leek"] } } }"#)); - let item = append_value(&mut doc, &key, "artichoke") + let item = append_value(&mut doc, &key, &TomlVal::from_str("artichoke").unwrap()) .expect("expected to be able to insert value 'artichoke'"); assert_eq!(format_toml(&item), r#"["leek"]"#); assert!(doc @@ -524,7 +524,7 @@ mod tests { let key = Keyspec::from_str("testcases.these.are.maybe.fruits") .expect("test key should be valid"); - let item = append_value(&mut doc, &key, "banana") + let item = append_value(&mut doc, &key, &TomlVal::from_str("banana").unwrap()) .expect("expected to be able to insert value 'banana'"); eprintln!("{}", doc.to_string()); assert!(item.is_none()); From ed75aae2f80a9871ca3f25480f8ad41f3cd9cbcb Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Tue, 31 Dec 2024 17:07:32 -0800 Subject: [PATCH 2/3] At the very least, permit constructing empty arrays and tables --- src/main.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main.rs b/src/main.rs index cf09e0e..3dad03a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -149,6 +149,10 @@ impl FromStr for TomlVal { Value::try_from(v).unwrap() } else if let Ok(v) = f64::from_str(s) { Value::try_from(v).unwrap() + } else if s == "[]" { + Value::Array(toml_edit::Array::new()) + } else if s == "{}" { + Value::InlineTable(toml_edit::InlineTable::new()) } else { s.into() }; From d1d80d974920a1d7cb183240e52a9f6c2bc7b81e Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Thu, 2 Jan 2025 12:29:48 -0800 Subject: [PATCH 3/3] Add some tests --- src/main.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/main.rs b/src/main.rs index 3dad03a..83e9074 100644 --- a/src/main.rs +++ b/src/main.rs @@ -669,6 +669,60 @@ mod tests { } } + #[test] + fn tomlval_parser_handles_inline_tables() { + let quoted = r#""{}""#; + let tval = TomlVal::from_str(quoted).expect("conversion should work"); + match tval.inner { + Value::String(s) => { + assert_eq!(*s.value(), "{}"); + } + _ => { + eprintln!("{:?}", tval.inner); + assert!(false, "should have been a string"); + } + } + + let inty = "{}"; + let tval2 = TomlVal::from_str(inty).expect("conversion should work"); + match tval2.inner { + Value::InlineTable(t) => { + assert_eq!(t.len(), 0); // It should be empty to start + } + _ => { + eprintln!("{:?}", tval2.inner); + assert!(false, "should have been an inline table"); + } + } + } + + #[test] + fn tomlval_parser_handles_inline_arrays() { + let quoted = r#""[]""#; + let tval = TomlVal::from_str(quoted).expect("conversion should work"); + match tval.inner { + Value::String(s) => { + assert_eq!(*s.value(), "[]"); + } + _ => { + eprintln!("{:?}", tval.inner); + assert!(false, "should have been a string"); + } + } + + let inty = "[]"; + let tval2 = TomlVal::from_str(inty).expect("conversion should work"); + match tval2.inner { + Value::Array(a) => { + assert_eq!(a.len(), 0); // It should be empty to start + } + _ => { + eprintln!("{:?}", tval2.inner); + assert!(false, "should have been an array"); + } + } + } + #[test] fn can_set_booleans() { let toml = include_str!("../fixtures/sample.toml");