New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deserializing $primitive=
unit variants doesn't work correctly yet
#354
Comments
@fwcd thanks for investigating this. I would like to have a go at fixing this. Has anyone started researching it? Is there a simple fix, or would there likely have to be some major machinery changes? It seems like that a node would just have to "remember" its primitive designation. Edit misunderstood the original example. Should we just add a new |
I found #304 It appears that we just need to execute the opposite step for I'll take a stab at implementing it. |
enum E {
Unit
}
struct AnyName {
element: E,
#[serde(rename = "@attribute")] // prepend @
attribute: E,
#[serde(rename = "#any")] // special name
any_content: E,
#[serde(rename = "#text")] // special name
text_content: E,
}
assert_eq!(
from_str("\
<any-name attribute=\"Unit\">\
<Unit/>\
<!-- #any content -->\
<Unit/>\
<!-- #text content -->\
Unit\
</any-name>\
").unwrap(),
AnyName {
element: E::Unit,
attribute: E::Unit,
any_content: E::Unit,
text_content: E::Unit,
}
); Note, that this is only illustration, Feel free to investigate, what of that not yet working (assuming that #490 is merged) |
FYI you can work around this issue like this: enum Node {
Unit,
#[serde(rename(serialize = "$primitive=PrimitiveUnit"))]
PrimitiveUnit
} In case you'd need to rename it on deserialize also, you could use: #[serde(rename(serialize = "$primitive=primitive-unit", deserialize = "primitive-unit"))] Source: Serde Field attributes documentation |
Currently, the deserializer doesn't recognize a primitive unit variant (i.e. one that uses the mechanism introduced in #304) correctly. Consider the following example (simplified from the unit tests):
Deserializing it with
from_str("PrimitiveUnit").unwrap()
throwsExperimenting a bit with
EnumAccess
, tracking the primitive variants in a similar fashion to unflattened fields seems to do the trick on this test (see this branch and this implementation). However, it doesn't fix the error when the enum is nested in a struct, e.g. like this:In this case, deserializing
from_str(r#"<Wrapper node="PrimitiveUnit"/>"#)
yields us the same error. The problem is that the deserializer attempts to deserialize it as a string (apparently?) in the context of aMapAccess
. I am not entirely sure how the Serde machinery works here, but we might have to keep track of primitive variants even outside of direct enum contexts, which could be quite a bit harder to do.The corresponding unit tests can be found here.
The text was updated successfully, but these errors were encountered: