toml-rs / toml

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

Error 'values must be emitted before tables' when serializing structs #403

Closed yallxe closed 1 year ago

yallxe commented 1 year ago

I have a few structs, given below

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct HoggConfig {
    pub dnsproxy: DnsProxyConfig,
    pub daemon: DaemonConfig,
    pub scanner: ScannerConfig,

    #[serde(skip)]
    _file: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DnsProxyConfig {
    pub bind: String,
    pub upstreams: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DaemonConfig {
    pub api: DaemonApiConfig,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DaemonApiConfig {
    pub enabled: bool,
    pub bind: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all(serialize = "snake_case", deserialize = "kebab-case"))]
pub struct ScannerConfig {
    pub nuclei: ScannerNucleiConfig,
    pub check_force_ssl: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all(serialize = "snake_case", deserialize = "kebab-case"))]
pub struct ScannerNucleiConfig {
    pub nuclei_executable: String,
    pub cli_args: Vec<String>,
    pub using_community_templates: Vec<String>,
}

And when I try to make HoggConfig object a string using toml::to_string_pretty(&self)?, I get an error values must be emitted before tables. How do I fix it?

matoushybl commented 1 year ago

Happened to me today too. The minimal example is:


#[derive(Serialize, Deserialize)]
struct A {
    a: String,
    b: String,
}

#[derive(Serialize, Deserialize)]
struct B {
    a: String,
    b: Vec<String>,
}

#[derive(Serialize, Deserialize)]
struct C {
    a: A,
    b: Vec<String>,
    c: Vec<B>,
}
let a = toml::to_string(&C {
        a: A {
            a: "aa".to_string(),
            b: "ab".to_string(),
        },
        b: vec!["b".to_string()],
        c: vec![B {
            a: "cba".to_string(),
            b: vec!["cbb".to_string()],
        }],
    })
    .unwrap();
x10an14 commented 1 year ago

I hit this too when on this commit, with the given cli arguments: