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

serializing enums with the default externally tagged serde option? #8

Open
divagant-martian opened this issue Apr 29, 2024 · 1 comment

Comments

@divagant-martian
Copy link

divagant-martian commented Apr 29, 2024

as per the title.

To my surprise, this fails:

use serde::{self, Deserialize, Serialize};

#[derive(Deserialize, Debug, Serialize)]
struct Config {
    store: StoreKind,
}

#[derive(Deserialize, Debug, Serialize)]
enum StoreKind {
    File(FileConfig),
    Other,
}

#[derive(Deserialize, Debug, Serialize)]
struct FileConfig {
    path: String,
}

fn main() {
    let cfg = Config {
        store: StoreKind::File(FileConfig {
            path: "path/to/thing".into(),
        }),
    };
    let repr = basic_toml::to_string(&cfg).unwrap();
    println!("{repr}");
}
@oxalica
Copy link

oxalica commented Sep 9, 2024

Deserialization does not work either. I have a configuration type like this:

#[derive(Debug, serde::Deserialize)]
enum Listen {
    Address(String),
    Unix(String),
}

fn main() {
    let conf = r#"
Address = "localhost"
    "#;
    dbg!(basic_toml::from_str::<Listen>(conf));
    dbg!(toml::from_str::<Listen>(conf));
}

It gives:

[src/main.rs:11:5] basic_toml::from_str::<Listen>(conf) = Err(
    Error {
        kind: Custom,
        line: Some(
            0,
        ),
        col: 0,
        at: Some(
            0,
        ),
        message: "invalid type: map, expected enum Listen",
        key: [],
    },
)
[src/main.rs:12:5] toml::from_str::<Listen>(conf) = Ok(
    Address(
        "localhost",
    ),
)

oxalica added a commit to Blah-IM/blahrs that referenced this issue Sep 10, 2024
basic-toml does not support externally-tagged enum yet.

See: dtolnay/basic-toml#8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants