Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions instant-xml-macros/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}),
);
}
Expand Down
38 changes: 35 additions & 3 deletions instant-xml/tests/forward-enum.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
use std::borrow::Cow;

use similar_asserts::assert_eq;

use instant_xml::{from_str, to_string, FromXml, ToXml};

#[derive(Debug, Eq, FromXml, PartialEq, ToXml)]
#[derive(Debug, FromXml, PartialEq, ToXml)]
#[xml(forward)]
enum Foo {
Bar(Bar),
Baz(Baz),
}

#[derive(Debug, Eq, FromXml, PartialEq, ToXml)]
#[derive(Debug, FromXml, PartialEq, ToXml)]
struct Bar {
bar: u8,
}

#[derive(Debug, Eq, FromXml, PartialEq, ToXml)]
#[derive(Debug, FromXml, PartialEq, ToXml)]
struct Baz {
baz: String,
}
Expand All @@ -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#"<Bar><bar>test</bar></Bar>"#;

assert_eq!(xml, to_string(&v).unwrap());
assert_eq!(v, from_str(xml).unwrap());
}
Loading