From a575fc627a5e67ecb5a662436229c3f161ae96c7 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Tue, 22 Oct 2024 21:38:39 +0200 Subject: [PATCH] fix: support all accumulators in enum forward deserialization Previously limited to Option only. --- instant-xml-macros/src/de.rs | 4 ++-- instant-xml/tests/forward-enum.rs | 32 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/instant-xml-macros/src/de.rs b/instant-xml-macros/src/de.rs index 81fa93c..8e240f3 100644 --- a/instant-xml-macros/src/de.rs +++ b/instant-xml-macros/src/de.rs @@ -170,9 +170,9 @@ fn deserialize_forward_enum( let v_ident = &variant.ident; variants.extend( 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)?; - *into = value.map(#ident::#v_ident); + *into = ::instant_xml::Accumulate::try_done(value, #field_str).map(#ident::#v_ident).ok(); }), ); } diff --git a/instant-xml/tests/forward-enum.rs b/instant-xml/tests/forward-enum.rs index e6dcff7..46b020c 100644 --- a/instant-xml/tests/forward-enum.rs +++ b/instant-xml/tests/forward-enum.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use similar_asserts::assert_eq; 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!(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#"test"#; + + assert_eq!(xml, to_string(&v).unwrap()); + assert_eq!(v, from_str(xml).unwrap()); +}