instant-labs / instant-xml

11 stars 3 forks source link

Allow using unit-variant enums as scalars #18

Closed djc closed 1 year ago

djc commented 1 year ago

I'd like to be able to define an enum that only has unit variants to be serialized/deserialized as a scalar.

For example:

#[xml(scalar)]
enum Foo {
    Bar,
    Baz,
}

Should serialize as the string Bar. Deserializing any other string should yield an error. Depending on the order with #17, we should also support using rename_all on enums to rename variants. While you're in there, in the case that the variant has an assigned value we should serialize that value:

#[xml(scalar)]
enum Foo {
    Bar = 12,
    Baz,
}

It should be a compile-time error to have #[xml(scalar)] on an enum that has non-unit variants like Foo(u8) or Bar { n: usize }.