xml: Implement XmlSerialize for enums

This commit is contained in:
Lennart
2024-12-27 13:53:30 +01:00
parent fc8d64220f
commit c787a6e8f3
4 changed files with 96 additions and 2 deletions

View File

@@ -172,4 +172,38 @@ impl Variant {
}
})
}
pub fn se_branch(&self) -> proc_macro2::TokenStream {
let ident = self.ident();
let variant_name = self.xml_name();
match &self.variant.fields {
Fields::Named(_) => {
panic!(
"struct variants are not supported, please use a tuple variant with a struct"
)
}
Fields::Unnamed(FieldsUnnamed { unnamed, .. }) => {
if unnamed.len() != 1 {
panic!("tuple variants should contain exactly one element");
}
quote! {
if let Self::#ident(val) = &self {
if !enum_untagged {
::rustical_xml::XmlSerialize::serialize(val, None, Some(#variant_name), writer)?;
} else {
::rustical_xml::XmlSerialize::serialize(val, None, None, writer)?;
};
}
}
}
Fields::Unit => {
quote! {
if let Self::#ident = self {
::rustical_xml::XmlSerialize::serialize(&(), ns, tag, writer)?;
}
}
}
}
}
}