Skip to content

Deserializing $primitive= unit variants doesn't work correctly yet #354

@fwcd

Description

@fwcd

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):

enum Node {
  Unit,
  #[serde(rename = "$primitive=PrimitiveUnit")]
  PrimitiveUnit
}

Deserializing it with from_str("PrimitiveUnit").unwrap() throws

thread ... panicked at 'called `Result::unwrap()` on an `Err` value: Custom("unknown variant `PrimitiveUnit`, expected one of `Unit`, `$primitive=PrimitiveUnit`")`

Experimenting 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:

struct Wrapper {
  node: Node
}

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 a MapAccess. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugserdeIssues related to mapping from Rust types to XML

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions