Skip to content

Commit 43ac4fa

Browse files
committed
config: preserve key formatting on set_value()
I just noticed toml_edit::Table has a format-preserving insert() API. I think it's better to retain the user-specified quoting style.
1 parent 10783f9 commit 43ac4fa

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

cli/tests/test_config_command.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,13 +522,13 @@ fn test_config_set_for_user() {
522522
// Ensure test-key successfully written to user config.
523523
let user_config_toml = std::fs::read_to_string(&user_config_path)
524524
.unwrap_or_else(|_| panic!("Failed to read file {}", user_config_path.display()));
525-
insta::assert_snapshot!(user_config_toml, @r###"
525+
insta::assert_snapshot!(user_config_toml, @r#"
526526
test-key = "test-val"
527527
528528
[test-table]
529529
foo = true
530-
"bar()" = 0
531-
"###);
530+
'bar()' = 0
531+
"#);
532532
}
533533

534534
#[test]

lib/src/config.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ impl ConfigLayer {
397397
.map_err(|keys| ConfigUpdateError::WouldOverwriteValue {
398398
name: keys.join("."),
399399
})?;
400-
match parent_table.entry(leaf_key) {
400+
match parent_table.entry_format(leaf_key) {
401401
toml_edit::Entry::Occupied(mut entry) => {
402402
if !entry.get().is_value() {
403403
return Err(ConfigUpdateError::WouldOverwriteTable {
@@ -409,6 +409,9 @@ impl ConfigLayer {
409409
}
410410
toml_edit::Entry::Vacant(entry) => {
411411
entry.insert(toml_edit::value(new_value));
412+
// Reset whitespace formatting (i.e. insert space before '=')
413+
let mut new_key = parent_table.key_mut(leaf_key).unwrap();
414+
new_key.leaf_decor_mut().clear();
412415
Ok(None)
413416
}
414417
}
@@ -476,7 +479,7 @@ fn ensure_parent_table<'a, 'b>(
476479
let mut keys = name.components();
477480
let leaf_key = keys.next_back().ok_or(&name.0[..])?;
478481
let parent_table = keys.enumerate().try_fold(root_table, |table, (i, key)| {
479-
let sub_item = table.entry(key).or_insert_with(new_implicit_table);
482+
let sub_item = table.entry_format(key).or_insert_with(new_implicit_table);
480483
sub_item.as_table_mut().ok_or(&name.0[..=i])
481484
})?;
482485
Ok((parent_table, leaf_key))
@@ -918,6 +921,31 @@ mod tests {
918921
"#);
919922
}
920923

924+
#[test]
925+
fn test_config_layer_set_value_formatting() {
926+
let mut layer = ConfigLayer::empty(ConfigSource::User);
927+
// Quoting style should be preserved on insertion
928+
layer
929+
.set_value(
930+
"'foo' . bar . 'baz'",
931+
ConfigValue::from_str("'value'").unwrap(),
932+
)
933+
.unwrap();
934+
insta::assert_snapshot!(layer.data, @r"
935+
['foo' . bar]
936+
'baz' = 'value'
937+
");
938+
939+
// Style of existing keys isn't updated
940+
layer.set_value("foo.bar.baz", "new value").unwrap();
941+
layer.set_value("foo.'bar'.blah", 0).unwrap();
942+
insta::assert_snapshot!(layer.data, @r#"
943+
['foo' . bar]
944+
'baz' = "new value"
945+
blah = 0
946+
"#);
947+
}
948+
921949
#[test]
922950
fn test_config_layer_delete_value() {
923951
let mut layer = ConfigLayer::empty(ConfigSource::User);

0 commit comments

Comments
 (0)