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

Value does not support enum deserialization from inline tables #364

Open
9uapaw opened this issue Oct 26, 2022 · 0 comments
Open

Value does not support enum deserialization from inline tables #364

9uapaw opened this issue Oct 26, 2022 · 0 comments
Labels
A-serde Area: Serde integration C-bug Category: Things not working as expected

Comments

@9uapaw
Copy link

9uapaw commented Oct 26, 2022

As opposed to Deserializer, Value does not support enum deserialization from inline tables.
Minimal repro steps:

    #[derive(Debug, Deserialize, PartialEq)]
    struct Main {
        test: Test
    }

    #[derive(Debug, Deserialize, PartialEq)]
    struct Test {
        a: A
    }

    #[derive(Debug, Deserialize, PartialEq)]
    enum A {
        B(String)
    }

fn main() {
    let tml = r#"[test]
        a = { B = "test" }
        "#;
        let r: Value = toml::from_str(tml).unwrap();
        let a: Main = r.try_into().unwrap();

Minimal fix would be (excluding the necessary checks from Deserializer):

#[inline]
    fn deserialize_enum<V>(
        self,
        _name: &str,
        _variants: &'static [&'static str],
        visitor: V,
    ) -> Result<V::Value, crate::de::Error>
    where
        V: de::Visitor<'de>,
    {
        match self {
            Value::String(variant) => visitor.visit_enum(variant.into_deserializer()),
            Value::Table(table) => visitor.visit_enum(MapAccessDeserializer::new(serde::de::value::MapDeserializer::new(table.into_iter()))), // Add this line to support inline tables as well
            _ => Err(de::Error::invalid_type(
                de::Unexpected::UnitVariant,
                &"string only",
            )),
        }
    }
@epage epage added C-bug Category: Things not working as expected A-serde Area: Serde integration labels Jan 18, 2023
@epage epage changed the title Value does not support enum deserialization from inline tables Value does not support enum deserialization from inline tables Jan 19, 2023
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

2 participants