Skip to content

Commit

Permalink
add support for serialization of enum tagged internally via an attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
YBeaugnon committed Apr 29, 2024
1 parent 0b8a796 commit f8eb0cd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
14 changes: 8 additions & 6 deletions yaserde/tests/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ fn unnamed_enum() {
fn tagged_enum() {
#[derive(Debug, PartialEq, YaSerialize, YaDeserialize, Default)]
#[yaserde(tag = "type")]
#[yaserde(rename = "foobar")]
enum XmlEnum {
#[default]
#[yaserde(rename = "foo")]
Expand All @@ -371,25 +372,26 @@ fn tagged_enum() {
}

#[derive(Debug, PartialEq, YaSerialize, YaDeserialize, Default)]
#[yaserde(rename = "base")]
struct XmlStruct {
#[yaserde(rename = "foobar")]
foo_bar: XmlEnum,
}

let model = XmlEnum::Foo;
let content = "<base type=\"foo\"/>";
// serialize_and_validate!(model, content);
let content = "<foobar type=\"foo\" />";
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, XmlEnum);

let model = XmlEnum::Bar;
let content = "<base type=\"bar\"/>";
// serialize_and_validate!(model, content);
let content = "<foobar type=\"bar\" />";
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, XmlEnum);

let model = XmlStruct {
foo_bar: XmlEnum::Foo,
};
let content = "<base><foobar type=\"foo\"/></base>";
// serialize_and_validate!(model, content);
let content = "<base><foobar type=\"foo\" /></base>";
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, XmlStruct);
}
35 changes: 29 additions & 6 deletions yaserde_derive/src/ser/expand_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ pub fn serialize(
.map(|variant| -> TokenStream {
let _attrs = crate::common::YaSerdeAttribute::parse(&variant.attrs);

let add_tag = if let Some(tag) = &root_attributes.tag {
let attrs = crate::common::YaSerdeAttribute::parse(&variant.attrs);
let label = variant.ident.clone();
let element_name = attrs.xml_element_name(&variant.ident);
quote! {
match self {
#name::#label { .. } => {
let tag = ::yaserde::__xml::name::OwnedName::local(#tag);
child_attributes.push(::yaserde::__xml::attribute::OwnedAttribute::new(tag, #element_name));
}
_ => {}
}
}
} else { quote!() };

let all_fields = variant
.fields
.iter()
Expand All @@ -39,7 +54,7 @@ pub fn serialize(
})
.collect();

attribute_fields
let add_attributes : TokenStream = attribute_fields
.iter()
.map(|field| {
let label = variant.ident.clone();
Expand Down Expand Up @@ -79,7 +94,9 @@ pub fn serialize(
}
}
})
.collect()
.collect();

quote!( #add_attributes #add_tag)
})
.collect();

Expand Down Expand Up @@ -109,10 +126,16 @@ fn inner_enum_inspector(
let label_name = build_label_name(label, &variant_attrs, &root_attributes.default_namespace);

match variant.fields {
Fields::Unit => quote! {
&#name::#label => {
let data_event = ::yaserde::__xml::writer::XmlEvent::characters(#label_name);
writer.write(data_event).map_err(|e| e.to_string())?;
Fields::Unit => {
if let Some(_tag) = &root_attributes.tag {
quote! { #name::#label => {} }
} else {
quote! {
#name::#label => {
let data_event = ::yaserde::__xml::writer::XmlEvent::characters(#label_name);
writer.write(data_event).map_err(|e| e.to_string())?;
}
}
}
},
Fields::Named(ref fields) => {
Expand Down

0 comments on commit f8eb0cd

Please sign in to comment.