Skip to content

Make it possible to deserialize a new type variant that contain None #965

@zetafunction

Description

@zetafunction

Example snippet:

use serde::{Deserialize};

#[derive(Debug, Deserialize)]
struct S {
    value: ExampleEnum,
}

#[derive(Debug, Deserialize)]
enum ExampleEnum {
  Variant(Option<NestedEnum>),
}

#[derive(Debug, Deserialize)]
enum NestedEnum {
    NestedVariant,
}

fn main() {
    println!("{:?}", toml::from_str::<S>("value = { Variant = 'NestedVariant' }"));
    println!("{:?}", toml::from_str::<S>("value = { Variant = 'None' }"));
    println!("{:?}", toml::from_str::<S>("value = { Variant = '' }"));
    println!("{:?}", toml::from_str::<S>("value = { Variant = {}}"));
    println!("{:?}", toml::from_str::<S>("value = { Variant }"));
    println!("{:?}", toml::from_str::<S>("value = 'Variant'"));
}

Currently this prints:

Ok(S { value: Variant(Some(NestedVariant)) })
Err(TomlError { message: "unknown variant `None`, expected `NestedVariant`", raw: Some("value = { Variant = 'None' }"), keys: ["value"], span: Some(20..26) })
Err(TomlError { message: "unknown variant ``, expected `NestedVariant`", raw: Some("value = { Variant = '' }"), keys: ["value"], span: Some(20..22) })
Err(TomlError { message: "wanted exactly 1 element, found 0 elements", raw: Some("value = { Variant = {}}"), keys: ["value"], span: Some(20..22) })
Err(TomlError { message: "expected `.`, `=`", raw: Some("value = { Variant }"), keys: [], span: Some(18..19) })
Err(TomlError { message: "invalid type: unit variant, expected newtype variant", raw: Some("value = 'Variant'"), keys: ["value"], span: Some(8..17) })

It would be nice if there were some way for this to work. toml-lang/toml#1025 indicates there is no null/none value, but it'd still be nice to be able to deserialize this somehow. Perhaps there's just no nice way to do that though...

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-serdeArea: Serde integrationC-enhancementCategory: Raise on the bar on expectations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions