Skip to content
This repository has been archived by the owner on Sep 24, 2022. It is now read-only.

How to serialize to inline table vs. dotted table? #265

Closed
maackle opened this issue Oct 18, 2018 · 8 comments
Closed

How to serialize to inline table vs. dotted table? #265

maackle opened this issue Oct 18, 2018 · 8 comments

Comments

@maackle
Copy link

maackle commented Oct 18, 2018

It seems that nested Tables are always serialized to dotted tables. e.g.:

let in_string = "[dependencies]\nhdk = { git = \"https://github.com/foo/bar\", branch = \"develop\" }"
let val = toml::from_str::<Value>(in_string)?;
println!("{:?}", toml::to_string(&val));

Even though the input string is an inline table, the output string uses a dotted table.

Is there no way to specify inline table when serializing?

@alexcrichton
Copy link
Collaborator

Currently this crate unfortunately doesn't support a lot of control over serialization and formatting, it just guarantees it emits some valid toml document, but not necessarily the prettiest

@samuelallan72
Copy link

Support for serializing inline tables would be great, because I suspect that is why I'm getting a ValueAfterTable error when serializing an object like:

#[derive(Deserialize, Serialize, Debug)]
#[serde(untagged)]
pub enum StringOrCmd {
    S(String),
    Cmd { cmd: Vec<String> },
}

#[derive(Deserialize, Serialize, Debug)]
pub struct BackendConfig {
    pub client_id: StringOrCmd,
    pub client_secret: StringOrCmd,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct Config {
  backend: BackendConfig,
}

This should serialize to something like:

[backend]
client_id = { cmd = ["cat", ".clientid"] }
client_secret = "somesecret"

But becomes invalid TOML if not using inline table format:

[backend]

[backend.client_id]
cmd = ["cat", ".clientid"]

# oops!
client_secret = "somesecret"

The solution would be either supporting inline tables or using extra logic to ensure all literal types under a table are written out before any nested table types.

@DianaNites
Copy link

I just got hit by that ValueAfterTable problem and it'd be really nice if it worked.

I'm parsing perfectly valid toml and then can't write it out and thats no fun.

@samuelallan72
Copy link

Just for reference, cargo-edit uses toml_edit for parsing and writing cargo.toml files. Could be useful to see how they do it for anyone planning on implementing this for toml-rs.

@gabbifish
Copy link

I'm definitely on team "would love to have an inline table serialization option!" here :)

@deg4uss3r
Copy link

👍 Hitting this issue with cargo-outdated when trying to format Cargo.toml for cargo API. This causes interference with the namespaced-features feature.

@timabell
Copy link

There's a round-trip test of an inline table at https://github.com/alexcrichton/toml-rs/blob/60b874308e6792a73cc00517a60bbef60a12e3cc/test-suite/tests/valid/example-v0.4.0.toml#L34-L46= which makes me think this must be possible somehow with the right structures. Maybe it only works with the toml-rs internal types at the moment and not with types defined outside the toml-rs library.

@epage
Copy link
Member

epage commented Sep 23, 2022

Maintenance of this crate has moved to the https://github.com/toml-rs/toml repo. As a heads up, we plan to move toml to be on top of toml_edit, see toml-rs/toml#340.

Closing this out. If this is still a problem, feel free to recreate this issue in the new repo.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants