Skip to content

Commit

Permalink
config: preserve key formatting on set_value()
Browse files Browse the repository at this point in the history
I just noticed toml_edit::Table has a format-preserving insert() API. I think
it's better to retain the user-specified quoting style.
  • Loading branch information
yuja committed Dec 29, 2024
1 parent 10783f9 commit 43ac4fa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
6 changes: 3 additions & 3 deletions cli/tests/test_config_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,13 +522,13 @@ fn test_config_set_for_user() {
// Ensure test-key successfully written to user config.
let user_config_toml = std::fs::read_to_string(&user_config_path)
.unwrap_or_else(|_| panic!("Failed to read file {}", user_config_path.display()));
insta::assert_snapshot!(user_config_toml, @r###"
insta::assert_snapshot!(user_config_toml, @r#"
test-key = "test-val"
[test-table]
foo = true
"bar()" = 0
"###);
'bar()' = 0
"#);
}

#[test]
Expand Down
32 changes: 30 additions & 2 deletions lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ impl ConfigLayer {
.map_err(|keys| ConfigUpdateError::WouldOverwriteValue {
name: keys.join("."),
})?;
match parent_table.entry(leaf_key) {
match parent_table.entry_format(leaf_key) {
toml_edit::Entry::Occupied(mut entry) => {
if !entry.get().is_value() {
return Err(ConfigUpdateError::WouldOverwriteTable {
Expand All @@ -409,6 +409,9 @@ impl ConfigLayer {
}
toml_edit::Entry::Vacant(entry) => {
entry.insert(toml_edit::value(new_value));
// Reset whitespace formatting (i.e. insert space before '=')
let mut new_key = parent_table.key_mut(leaf_key).unwrap();
new_key.leaf_decor_mut().clear();
Ok(None)
}
}
Expand Down Expand Up @@ -476,7 +479,7 @@ fn ensure_parent_table<'a, 'b>(
let mut keys = name.components();
let leaf_key = keys.next_back().ok_or(&name.0[..])?;
let parent_table = keys.enumerate().try_fold(root_table, |table, (i, key)| {
let sub_item = table.entry(key).or_insert_with(new_implicit_table);
let sub_item = table.entry_format(key).or_insert_with(new_implicit_table);
sub_item.as_table_mut().ok_or(&name.0[..=i])
})?;
Ok((parent_table, leaf_key))
Expand Down Expand Up @@ -918,6 +921,31 @@ mod tests {
"#);
}

#[test]
fn test_config_layer_set_value_formatting() {
let mut layer = ConfigLayer::empty(ConfigSource::User);
// Quoting style should be preserved on insertion
layer
.set_value(
"'foo' . bar . 'baz'",
ConfigValue::from_str("'value'").unwrap(),
)
.unwrap();
insta::assert_snapshot!(layer.data, @r"
['foo' . bar]
'baz' = 'value'
");

// Style of existing keys isn't updated
layer.set_value("foo.bar.baz", "new value").unwrap();
layer.set_value("foo.'bar'.blah", 0).unwrap();
insta::assert_snapshot!(layer.data, @r#"
['foo' . bar]
'baz' = "new value"
blah = 0
"#);
}

#[test]
fn test_config_layer_delete_value() {
let mut layer = ConfigLayer::empty(ConfigSource::User);
Expand Down

0 comments on commit 43ac4fa

Please sign in to comment.