fix(toml_edit): reformat dotted keyed-items in TableLike::fmt

`TableLike::fmt` only reset the decor of value items, leaving dotted
keyed-items untouched. They are stored as dotted tables and dotted
inline tables, so recurse into them to reformat their key segments and
nested values as well.

Fixes #262
This commit is contained in:
dualfroz committed 2026-09-02 11:42:16 +02:00
1 parent 9f7a403e88
commit 6572de03e2
3 files changed
+64 -12

No files matched your search

+13 -6
View File
@@ -512,16 +512,23 @@ impl<'s> IntoIterator for &'s InlineTable {
fn decorate_inline_table(table: &mut InlineTable) {
use indexmap::map::MutableKeys;
for (mut key, value) in table
.items
.iter_mut2()
.filter(|(_, value)| value.is_value())
.map(|(key, value)| (key.as_mut(), value.as_value_mut().unwrap()))
{
for (key, value) in table.items.iter_mut2() {
match value {
// Dotted keyed-items are stored as dotted inline tables, so reformat the
// key segment and recurse to cover any values nested beneath it.
Item::Value(Value::InlineTable(dotted)) if dotted.is_dotted() => {
key.leaf_decor_mut().clear();
key.dotted_decor_mut().clear();
decorate_inline_table(dotted);
}
Item::Value(value) => {
key.leaf_decor_mut().clear();
key.dotted_decor_mut().clear();
value.decor_mut().clear();
}
_ => {}
}
}
}
/// An owned iterator type over an [`InlineTable`]'s [`Key`]/[`Value`] pairs
+13 -6
View File
@@ -540,16 +540,23 @@ pub(crate) type KeyValuePairs = IndexMap<Key, Item>;
fn decorate_table(table: &mut Table) {
use indexmap::map::MutableKeys;
for (mut key, value) in table
.items
.iter_mut2()
.filter(|(_, value)| value.is_value())
.map(|(key, value)| (key.as_mut(), value.as_value_mut().unwrap()))
{
for (key, value) in table.items.iter_mut2() {
match value {
Item::Value(value) => {
key.leaf_decor_mut().clear();
key.dotted_decor_mut().clear();
value.decor_mut().clear();
}
// Dotted keyed-items are stored as dotted tables, so reformat the key
// segment and recurse to cover any values nested beneath it.
Item::Table(dotted) if dotted.is_dotted() => {
key.leaf_decor_mut().clear();
key.dotted_decor_mut().clear();
decorate_table(dotted);
}
_ => {}
}
}
}
// `key1 = value1`
+38
View File
@@ -1238,6 +1238,44 @@ fn test_format_array() {
"#]]);
}
#[test]
fn test_format_dotted_keys() {
given(
r#"
a . b = 1
c = 2
d . e . f = 3
"#,
)
.running(|root| {
root.fmt();
})
.produces_display(str![[r#"
a.b = 1
c = 2
d.e.f = 3
"#]]);
}
#[test]
fn test_format_dotted_keys_in_inline_table() {
given(
r#"
a = { b . c = 1, d = 2 }
"#,
)
.running(|root| {
let a = root.get_mut("a").unwrap().as_inline_table_mut().unwrap();
a.fmt();
})
.produces_display(str![[r#"
a = { b.c = 1, d = 2 }
"#]]);
}
macro_rules! as_inline_table {
($entry:ident) => {{
assert!($entry.is_value());