Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing output on or_insert()ing on a nested Item::None table #742

Open
MarijnS95 opened this issue Jun 10, 2024 · 3 comments
Open

Missing output on or_insert()ing on a nested Item::None table #742

MarijnS95 opened this issue Jun 10, 2024 · 3 comments
Labels
A-edit Area: TOML editing API C-enhancement Category: Raise on the bar on expectations

Comments

@MarijnS95
Copy link

Consider the following inexistant patch."ssh://foo.bar" table, that I'd like to fill with a my_crate entry:

fn main() {
    let mut x = toml_edit::DocumentMut::new();
    let patch = &mut x["patch"]["ssh://foo.bar"];
    assert!(patch.is_none());
    patch["my_crate"]["path"] = toml_edit::Item::Value("foo/baz/bar".into());
    println!("{}", x);
}

It seems the IndexMut access here causes all None tables to be added, with an implicit moniker, which is neat:

patch = { "ssh://foo.bar" = { my_crate = { path = "foo/baz/bar" } } }

However, I'd like the table to not be inline, to get [patch."ssh://foo.bar"]. It seems that toml_edit::table() returns such a table, so I assumed .or_insert()ing it would work:

fn main() {
    let mut x = toml_edit::DocumentMut::new();
    let patch = x["patch"]["ssh://foo.bar"].or_insert(toml_edit::table()); // This table is _not_ inline
    patch["my_crate"]["path"] = toml_edit::Item::Value("foo/baz/bar".into());
    println!("{}", x);
}

Somewhat surprisingly, this does not seem to recursively insert an implicit table for patch, and the output is:

patch = {}

The only way I've found around this thus far is to insert another table on ["patch"].or_insert(...). However, to prevent an empty [patch] from showing up, this has to be marked as implicit with this rather verbose syntax:

fn main() {
    let mut x = toml_edit::DocumentMut::new();
    let patch = &mut x["patch"].or_insert({
        let mut table = toml_edit::Table::new();
        table.set_implicit(true);
        toml_edit::Item::Table(table)
    })["ssh://foo.bar"]
        .or_insert(toml_edit::table());
    patch["my_crate"]["path"] = toml_edit::Item::Value("foo/baz/bar".into());
    println!("{}", x);
}

With the desired output:

[patch."ssh://foo.bar"]
my_crate = { path = "foo/baz/bar" }

Is it expected that no implicit table was created for patch? If so, how about having a toml_edit::implicit_table() helper or the like?

Thanks!

@epage
Copy link
Member

epage commented Jun 10, 2024

That is expected behavior. I'm also hesitant to create specialized helpers like that.

@epage epage added C-enhancement Category: Raise on the bar on expectations A-edit Area: TOML editing API labels Jun 10, 2024
@MarijnS95
Copy link
Author

Is this documented anywhere that I can rely on, and/or is there a better way to write my "workaround" above?

@epage
Copy link
Member

epage commented Jun 10, 2024

Not aware of a place its documented and not sure where would be a good place. However, it is our API contract so you can rely on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-edit Area: TOML editing API C-enhancement Category: Raise on the bar on expectations
Projects
None yet
Development

No branches or pull requests

2 participants