Skip to content

Working with typetag for xs:choice / variant types? #880

Description

@furrz

This is a question, rather than an issue.

I'd like to use typetag or a similar crate along with quick-xml, to deserialize a sequence of varying elements without hardcoding all the possible variants in an enum.

<Data>
    <Foo a="abc" />
    <Foo a="def" />
    <Bar b="123" c="456">
        <BarChild />
    </Bar>
    <!-- other typetag'd elements can appear here -->
</Data>

Is there some way to make this work with typetag and quick-xml?

I tried:

#[derive(Deserialize)]
pub struct Document {
    #[serde(rename = "$value")]
    pub items: Vec<Box<dyn SceneLoadable>>,
}

#[typetag::deserialize]
pub(crate) trait SceneLoadable {}

But after encountering an element, it proceeds to try and interpret the attributes on that tag as type info, e.g.: Unknown variant @id, expected one of ..... (Edit: my apologies to the github user with username id, I mentioned them by mistake. Oops!)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionserdeIssues 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