toml icon indicating copy to clipboard operation
toml copied to clipboard

`Display` implementation for `toml_edit::Document` produces an empty string

Open ibraheemdev opened this issue 6 months ago • 3 comments

The code below prints an empty string.

fn main() {
    let toml = r##"
[project]
x = "y"
    "##;

    let doc = toml.parse::<toml_edit::Document<String>>().unwrap();
    println!("{}", doc.to_string());
}

If I remove the project header it seems to print the key and value pair. DocumentMut also works perfectly fine.

ibraheemdev avatar Jul 11 '25 23:07 ibraheemdev

#!/usr/bin/env nargo
---
[dependencies]
toml_edit = "0.23"
---

fn main() {
    let toml = r##"
[project]
x = "y"
    "##;

    let doc = toml.parse::<toml_edit::Document<String>>().unwrap();
    println!(
        "---Document
{}
---",
        doc.to_string()
    );

    let doc = toml.parse::<toml_edit::DocumentMut>().unwrap();
    println!(
        "---DocumentMut
{doc}
---"
    );
}

I also switched it to [email protected] and Document with ImDocument and got the same result.

its important to note that there is no impl Display for Document. You can't move "{]", doc to "{doc}". The to_string function must be coming from the deref to the underlying table.

When we print a table, we don't recurse into sub-tables and print those. Since no keys are in the root of the document, we print nothing.

epage avatar Jul 18 '25 18:07 epage

Going to need to put more thought into this for how this should be fixed.

epage avatar Jul 18 '25 18:07 epage

Also encounter this within AI generated test code.

Maybe Document should not have .to_string() method which confuse AI, or just render non empty string.

let toml_string = doc.to_string();
let parsed_doc: Document<String> = toml_string.parse()?;

assert_eq!(doc.to_string(), parsed_doc.to_string());
assertion `left == right` failed
  left: "XXX"
 right: ""
let toml_string = doc.to_string();
let parsed_doc: Document<String> = toml_string.parse()?;
let parsed_doc = parsed_doc.into_mut();

assert_eq!(doc.to_string(), parsed_doc.to_string());

loynoir avatar Nov 24 '25 04:11 loynoir