fix: support all accumulators in enum forward deserialization

Previously limited to Option<T> only.
This commit is contained in:
gibbz00 2024-10-22 21:38:39 +02:00 committed by Dirkjan Ochtman
parent 03d99af431
commit a575fc627a
2 changed files with 34 additions and 2 deletions

View File

@ -170,9 +170,9 @@ fn deserialize_forward_enum(
let v_ident = &variant.ident; let v_ident = &variant.ident;
variants.extend( variants.extend(
quote!(if <#no_lifetime_type as FromXml>::matches(id, None) { quote!(if <#no_lifetime_type as FromXml>::matches(id, None) {
let mut value = None; let mut value = <#no_lifetime_type as FromXml>::Accumulator::default();
<#no_lifetime_type as FromXml>::deserialize(&mut value, #field_str, deserializer)?; <#no_lifetime_type as FromXml>::deserialize(&mut value, #field_str, deserializer)?;
*into = value.map(#ident::#v_ident); *into = ::instant_xml::Accumulate::try_done(value, #field_str).map(#ident::#v_ident).ok();
}), }),
); );
} }

View File

@ -1,3 +1,5 @@
use std::borrow::Cow;
use similar_asserts::assert_eq; use similar_asserts::assert_eq;
use instant_xml::{from_str, to_string, FromXml, ToXml}; use instant_xml::{from_str, to_string, FromXml, ToXml};
@ -26,3 +28,33 @@ fn wrapped_enum() {
assert_eq!(xml, to_string(&v).unwrap()); assert_eq!(xml, to_string(&v).unwrap());
assert_eq!(v, from_str(xml).unwrap()); assert_eq!(v, from_str(xml).unwrap());
} }
#[derive(Debug, FromXml, PartialEq, ToXml)]
#[xml(forward)]
enum FooCow<'a> {
Bar(Cow<'a, [BarBorrowed<'a>]>),
Baz(Cow<'a, [BazBorrowed<'a>]>),
}
#[derive(Clone, Debug, FromXml, PartialEq, ToXml)]
#[xml(rename = "Bar")]
struct BarBorrowed<'a> {
bar: Cow<'a, str>,
}
#[derive(Clone, Debug, FromXml, PartialEq, ToXml)]
#[xml(rename = "Baz")]
struct BazBorrowed<'a> {
baz: Cow<'a, str>,
}
#[test]
fn with_cow_accumulator() {
let v = FooCow::Bar(Cow::Borrowed(&[BarBorrowed {
bar: Cow::Borrowed("test"),
}]));
let xml = r#"<Bar><bar>test</bar></Bar>"#;
assert_eq!(xml, to_string(&v).unwrap());
assert_eq!(v, from_str(xml).unwrap());
}