Open dishmaker opened 8 months ago
You can find answer in this chapter about mapping of Rust types to XML. TL;DR: your enum variant is not a unit variant and it cannot be serialized in arbitrary-named field of struct.
Then why does it work for toml
, yaml
, msgpack
and der
but does not for quick-xml
?
Why do I have to compile different binary just for your library? It forces me to use a feature flag in compile time.
Is there any way to show serde(rename)
exclusively to this crate?
struct AnyName {
#[serde(rename = "$value")]
any_name: Choice,
}
But still, even when using #[serde(rename = "$value")]
it does not serialize
<any_name> ... </any_name>
but only the inside variant.
It is hard to answer to your questions because you do not provide your expectations. The mentioned piece of documentation shows how quick-xml performs mapping in a consistent manner. If you have concrete suggestions, please describe them and even better open a PR with them!
My expectations:
Kind In normal field
Unit <field>Unit</field>
Newtype <field><Newtype>42</Newtype></field>
Tuple <field><Tuple>42</Tuple><Tuple>answer</Tuple></field>
Struct <field><Struct><q>42</q><a>answer</a></Struct></field>
Just like JSON:
Unit "field": "Unit",
Newtype "field": { "Newtype": 42 },
Tuple "field": [42, "answer"],
Struct "field": { "Struct": {"q": 42, "a":"answer"} },
Issue should be reopened - other crates support enum lists. Only quick-xml is a black sheep here.
You feel free to submit PR that would implement the desired behavior and make it in the consistent way. Probably this is possible. We also should keep the ability to use tag name as enum discriminator, because this is natural way how xs:choice
in XML is represented. It must be representable with Rust enum
.
"field": "Unit",
"field": { "Newtype": 42 },
"field": { "Tuple": [42, "answer"] },
"field": { "Struct": {"q": 42, "a":"answer"} },
https://docs.rs/quick-xml/latest/quick_xml/de/index.html#normal-enum-variant
<field>Unit</field>
<field><Newtype>42</Newtype></field>
<field><Tuple>42</Tuple><Tuple>answer</Tuple></field>
<field><Struct><q>42</q><a>answer</a></Struct></field>
Currently the above XML won't be generated (gives an error) while JSON would even parse back into Rust enum.
Unsupported operation: cannot serialize enum newtype variant
Serialization of enum - crate comparison: