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 simple enum leads to UnsupportedType #553

Closed
soywod opened this issue May 16, 2023 · 4 comments · Fixed by #557
Closed

Serializing simple enum leads to UnsupportedType #553

soywod opened this issue May 16, 2023 · 4 comments · Fixed by #557

Comments

@soywod
Copy link

soywod commented May 16, 2023

Sorry if the answer is obvious, but I cannot understand why toml is not able to serialize this struct (but serde_json can):

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct Struct {
    field: Enum,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
enum Enum {
    Variant(u8),
}

let ser = Struct {
    field: Enum::Variant(21),
};

println!("json: {:?}", serde_json::to_string(&ser));

println!("toml: {:?}", toml::to_string(&ser));
json: Ok("{\"field\":{\"Variant\":21}}")
toml: Err(Error { inner: UnsupportedType(Some("Enum")) })

Even mor surprising, the same struct can be deserialized without any problem:

let de: Result<Struct, toml::de::Error> = toml::from_str("[field]\nVariant = 21");

println!("toml: {:?}", de);
toml: Ok(Struct { field: Variant(21) })
@soywod
Copy link
Author

soywod commented May 17, 2023

May be linked to #534.

@epage
Copy link
Member

epage commented May 18, 2023

All in one reproduction case:

#!/usr/bin/env rust-script

//! ```cargo
//! [dependencies]
//! toml = "0.7.3"
//! serde = { version = "1", features = ["derive"] }
//! serde_json = "1"
//! ```

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct Struct {
    field: Enum,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
enum Enum {
    Variant(u8),
}

fn main() {
    let ser = Struct {
        field: Enum::Variant(21),
    };
    println!("json: {:?}", serde_json::to_string(&ser));
    println!("toml: {:?}", toml::to_string(&ser));

    let de: Result<Struct, toml::de::Error> = toml::from_str("[field]\nVariant = 21");
    println!("toml: {:?}", de);
}

epage added a commit to epage/toml_edit that referenced this issue May 18, 2023
epage added a commit to epage/toml_edit that referenced this issue May 18, 2023
@epage
Copy link
Member

epage commented May 18, 2023

New release out with newtype variant support

@soywod
Copy link
Author

soywod commented May 18, 2023 via email

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

Successfully merging a pull request may close this issue.

2 participants