Skip to content
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

With serde, is it possible to get unknown variant names outside of a deserializing error? #602

Closed
Dekkonot opened this issue May 16, 2023 · 3 comments
Labels
question serde Issues related to mapping from Rust types to XML

Comments

@Dekkonot
Copy link

I'm using serde to deserialize a file format that looks something like this:

<root>
  <list>
     <type1>content</type1>
     <type2>a new and exciting content type </type2>
     <!-- etc -->
  </list>
</root>

This is easy to implement and I've done so using an enum because I know all the types that currently exist under list. However, I'm not in control of the file format so at any point in the future new variants to to that enum might be added without me supporting them. I'd like to not fail in this case, so using #[serde(other)] on a variant is the obvious solution.

This however means that I'm silently skipped over unknown data. I'd prefer to emit a warning to the user in this case simply because it's bad UX. While that's (again) easy to do, I would also like to include the type of the new data if at all possible so that people reporting it know what to tell me. However, it seems like there's no way to get this information outside of the error raised when parsing an unknown variant.

I realize this might be a limitation with Serde but I may also be missing something so I though I'd ask directly. Is there a way to get the variant name of an unknown enum without the error that's returned when failing to parse it?

@Mingun Mingun added question serde Issues related to mapping from Rust types to XML labels May 17, 2023
@Mingun
Copy link
Collaborator

Mingun commented May 17, 2023

@Dekkonot
Copy link
Author

A quick followup: Is it possible to catch the name of an element using Serde? I know it's not generally used for deserializing but since it exists, is it exposed anywhere?

@Mingun
Copy link
Collaborator

Mingun commented May 18, 2023

Yes, that is possible. MapAccess::next_key() will deserialize from element of <ns:element>.

@Dekkonot Dekkonot closed this as completed Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question serde Issues related to mapping from Rust types to XML
Projects
None yet
Development

No branches or pull requests

2 participants