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

Error when serializing vector of enums #321

Closed
vikigenius opened this issue Jul 5, 2022 · 3 comments
Closed

Error when serializing vector of enums #321

vikigenius opened this issue Jul 5, 2022 · 3 comments
Labels
A-serde Area: Serde integration C-bug Category: Things not working as expected

Comments

@vikigenius
Copy link

This is probably carried over from toml-rs, but reproducing anyway.

use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
pub struct Module1 {
    x: u8,
    y: u8
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Module2 {
    h: u8,
    i: u8
}

#[derive(Debug, Deserialize, Serialize)]
pub enum Module {
    Module1(Module1),
    Module2(Module2)
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Container {
    modules: Vec<Module>,
}


#[cfg(test)]
mod tests {
    use super::*;
    use serde_json;
    use toml;

    #[test]
    fn test_container_serialization() {
        let module1 = Module1 {x: 4, y: 5};
        let module2 = Module2 {h: 2, i: 3};
        let container: Container = Container {modules: vec![Module::Module1(module1), Module::Module2(module2)]};
        dbg!(serde_json::to_string(&container).unwrap()); // This works
        dbg!(toml_edit::easy::to_string(&container).unwrap()); // This panics
        dbg!(toml::to_string(&container).unwrap()); // This also panics
    }
}
@epage epage added the C-bug Category: Things not working as expected label Jul 18, 2022
@epage epage added the A-serde Area: Serde integration label Sep 23, 2022
@epage
Copy link
Member

epage commented Jan 18, 2023

Structured more like a test we'd add:

#[test]
fn test_container_serialization() {
    #[derive(Debug, Deserialize, Serialize)]
    pub struct Module1 {
        x: u8,
        y: u8,
    }

    #[derive(Debug, Deserialize, Serialize)]
    pub struct Module2 {
        h: u8,
        i: u8,
    }

    #[derive(Debug, Deserialize, Serialize)]
    pub enum Module {
        Module1(Module1),
        Module2(Module2),
    }

    #[derive(Debug, Deserialize, Serialize)]
    pub struct Container {
        modules: Vec<Module>,
    }

    let module1 = Module1 { x: 4, y: 5 };
    let module2 = Module2 { h: 2, i: 3 };
    let container: Container = Container {
        modules: vec![Module::Module1(module1), Module::Module2(module2)],
    };
    toml::to_string(&container).unwrap();
}

@epage epage changed the title Panic when serializing vector of enums Error when serializing vector of enums Jan 18, 2023
@crazymerlyn
Copy link

Should this be closed? I can't reproduce it with the latest version.

@epage
Copy link
Member

epage commented Aug 19, 2024

This was fixed in #557 which has a sufficient test for this case.

Thanks for checking!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-serde Area: Serde integration C-bug Category: Things not working as expected
Projects
None yet
Development

No branches or pull requests

3 participants