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
+70 -18

No files matched your search

+16 -9
View File
@@ -512,15 +512,22 @@ impl<'s> IntoIterator for &'s InlineTable {
fn decorate_inline_table(table: &mut InlineTable) { fn decorate_inline_table(table: &mut InlineTable) {
use indexmap::map::MutableKeys; use indexmap::map::MutableKeys;
for (mut key, value) in table for (key, value) in table.items.iter_mut2() {
.items match value {
.iter_mut2() // Dotted keyed-items are stored as dotted inline tables, so reformat the
.filter(|(_, value)| value.is_value()) // key segment and recurse to cover any values nested beneath it.
.map(|(key, value)| (key.as_mut(), value.as_value_mut().unwrap())) Item::Value(Value::InlineTable(dotted)) if dotted.is_dotted() => {
{ key.leaf_decor_mut().clear();
key.leaf_decor_mut().clear(); key.dotted_decor_mut().clear();
key.dotted_decor_mut().clear(); decorate_inline_table(dotted);
value.decor_mut().clear(); }
Item::Value(value) => {
key.leaf_decor_mut().clear();
key.dotted_decor_mut().clear();
value.decor_mut().clear();
}
_ => {}
}
} }
} }
+16 -9
View File
@@ -540,15 +540,22 @@ pub(crate) type KeyValuePairs = IndexMap<Key, Item>;
fn decorate_table(table: &mut Table) { fn decorate_table(table: &mut Table) {
use indexmap::map::MutableKeys; use indexmap::map::MutableKeys;
for (mut key, value) in table for (key, value) in table.items.iter_mut2() {
.items match value {
.iter_mut2() Item::Value(value) => {
.filter(|(_, value)| value.is_value()) key.leaf_decor_mut().clear();
.map(|(key, value)| (key.as_mut(), value.as_value_mut().unwrap())) key.dotted_decor_mut().clear();
{ value.decor_mut().clear();
key.leaf_decor_mut().clear(); }
key.dotted_decor_mut().clear(); // Dotted keyed-items are stored as dotted tables, so reformat the key
value.decor_mut().clear(); // 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);
}
_ => {}
}
} }
} }
+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 { macro_rules! as_inline_table {
($entry:ident) => {{ ($entry:ident) => {{
assert!($entry.is_value()); assert!($entry.is_value());