serde icon indicating copy to clipboard operation
serde copied to clipboard

accept #[serde(other)] on non-unit enum variants

Open lovasoa opened this issue 4 years ago • 4 comments

Hello ! What would you think of accepting the #[serde(other) attribute on struct- and tuple- enum variants ?

Currently, the following code :

#[derive(Deserialize, Debug)]
enum Item {
    Foo,
    Bar,
    #[serde(other)]
    Other(Contents),
}

raises the following compilation error :

error: #[serde(other)] must be on a unit variant

This would allow collecting the data corresponding to an unknown or unexpected enum variant.

This would be very helpful for instance in serde-xml-rs to parse data from complex xml schema without all possible tags in advance. (see https://github.com/RReverser/serde-xml-rs/issues/55)

lovasoa avatar Jan 28 '21 09:01 lovasoa

https://github.com/serde-rs/serde/issues/912 is sort of already tracking this. It started off asking for #[serde(other)] in general; then when that was implemented for unit variants it also asked for that on variants with data.

Arnavion avatar Feb 12 '21 09:02 Arnavion

Try use serde-enum-str , the examples

vkill avatar Jul 10 '21 16:07 vkill

See https://github.com/serde-rs/serde/issues/912#issuecomment-1868785603

mohe2015 avatar Jan 06 '24 19:01 mohe2015