toml-rs / toml

Rust TOML Parser
https://docs.rs/toml
Apache License 2.0
700 stars 104 forks source link

Decor in `DocumentMut` is ignored #727

Closed yyny closed 2 months ago

yyny commented 4 months ago

Example

use toml_edit::*;

let mut document = DocumentMut::new();
document.decor_mut().set_prefix("# HEADER\n");
document.decor_mut().set_prefix("# FOOTER\n");
println!("{document}");

document.as_table_mut().insert("key", value("value"));
println!("{document}");

Expected

# HEADER
# FOOTER
# HEADER
key = "value"
# FOOTER

Actual

key = "value"
epage commented 4 months ago

What this is hiding is that this isn't DocumentMut::decor_mut but Table::decor_mut that is being accessed due to a convenience Deref.

I don't think I'm against this. I'm also curious about collapsing trailing into the suffix. I'm not sure why that wasn't done before.

ysndr commented 4 months ago

We did run into the same issue today (what a coincidence). For now we only wanted to add a document wide header and probably going to resort to prepending it to the first item in the document, though adding it to the document directly would definitely be more ergonomic.