Skip to content

Do not require parentheses around struct enum variant with default values #576

@mierak

Description

@mierak

Currently we are required to specify at least empty set of parentheses after the enum variant name when deserializing a struct variant which has all fields marked with #[serde(default)].
I find myself converting my unit variants to struct variants and this would make it possible to extend them in a backwards compatible way instead of having to create an empty struct variant from the get go.

Example to illustrate the issue:

use serde::Deserialize;

#[derive(Deserialize)]
enum Foo {
    A,
    B {
        #[serde(default)]
        field: String,
    }
}

fn main() {
    let a: Result<Foo, _> = ron::de::from_str("A");
    let b: Result<Foo, _> = ron::de::from_str(r#"B(field: "value")"#);
    let c: Result<Foo, _> = ron::de::from_str("B()");
    let d: Result<Foo, _> = ron::de::from_str("B");

    assert!(a.is_ok()); // Ok
    assert!(b.is_ok()); // Ok
    assert!(c.is_ok()); // Ok
    assert!(d.is_ok()); // Not Ok
}

Would it be possible to make the option d deserialize with default values in the same fashion as option c does?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions