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

Could impl_deserialize_for_internally_tagged_enum! handle a default case? #629

Closed
aesteve opened this issue Aug 14, 2023 · 5 comments
Closed
Labels
enhancement help wanted serde Issues related to mapping from Rust types to XML

Comments

@aesteve
Copy link
Contributor

aesteve commented Aug 14, 2023

Hello and thanks a lot for your work.

Would it be possible (and would it make sense) for impl_deserialize_for_internally_tagged_enum to handle a default case?

Like "for every other value of this tag, just deserialise to this enum variant.

Not sure how common this is but I'm facing this exact pattern where I'd like to handle a few variants that I know are specific, and all the rest can just be fall into a generic struct.

I hope it's clear, sorry if it's already possible and I missed it. Happy to update the docs to highlight it in this case.

Thanks a lot.

@Mingun
Copy link
Collaborator

Mingun commented Aug 14, 2023

It is not clear for me what exactly you need. Perhaps this is already could be implemented using existing serde API. Could you provide an example of your Rust struct and XML that you want to map?

@Mingun Mingun added question serde Issues related to mapping from Rust types to XML labels Aug 14, 2023
@aesteve
Copy link
Contributor Author

aesteve commented Aug 14, 2023

Sorry for the confusion, let me try to explain this a bit better.

<Root>
  <SomeElement type="Specific">
      <MoreSpecific>a</MoreSpecific>
      <Common>123</Common>
  </SomeElement>
  <SomeElement type="AnotherType">
     <Common>MaybeNotAnInt</Common>
  </SomeElement>
  <SomeElement type="YetAnotherType">
     <Common>AnotherString</Common>
  </SomeElement>
</Root>

I could map to the following enum:

enum Elements {
  Specific({Common: u32, MoreSpecific: String}),
  TheRest({Common: String})
}

As of now, I deserialize to:

struct CommonStructure {
  r#type: String,
  Common: String,
  MoreSpecific: Option<String>
}

and then match on r#type to create the proper value.

I'd really like to use impl_deserialize_for_internally_tagged_enum but that means (unless I'm missing something) I have to list every possible value for type= which I can't know for sure.

The idea is that I'd like "everything that's not type=Specific to be handled as TheRest, no matter @type.

I'm sorry I can't explain it better, but basically getting the equivalent of the _ => ... pattern in a match expression.

@Mingun
Copy link
Collaborator

Mingun commented Aug 15, 2023

Ah, I didn't immediately realize it was about macro impl_deserialize_for_internally_tagged_enum! from quick-xml. Probably this could be implemented. You should replace default arm (_) here

match tag.as_ref() {
$(
$variant_tag => Ok(deserialize_variant!( de, $enum, $($variant)+ )),
)*
_ => Err(A::Error::unknown_field(&tag, &[$($variant_tag),+])),
}

with Ok(deserialize_variant!(...)) with the name of default variant. Feel free to submit a PR!

@Mingun Mingun changed the title Could impl_deserialize_for_internally_tagged_enum handle a default case? Could impl_deserialize_for_internally_tagged_enum! handle a default case? Aug 15, 2023
@aesteve
Copy link
Contributor Author

aesteve commented Aug 16, 2023

Thanks for the detailed answer.

Unfortunately I have never written a single macro, so it's probably gonna take a while for me to find the syntax to add an optional argument (so that it's backward compatible), and how to deal with this in the body macro.

I'll try to, at some point, but can't promise I'll manage to 😅

Thanks again.

aesteve added a commit to aesteve/quick-xml that referenced this issue Aug 16, 2023
aesteve added a commit to aesteve/quick-xml that referenced this issue Aug 17, 2023
@Mingun
Copy link
Collaborator

Mingun commented Apr 29, 2024

Fixed in #634

@Mingun Mingun closed this as completed Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement help wanted serde Issues related to mapping from Rust types to XML
Projects
None yet
Development

No branches or pull requests

2 participants