Files
rustical/crates/xml/derive/src/xml_enum/impl_se.rs

66 lines
2.6 KiB
Rust

use quote::quote;
use crate::Variant;
use super::Enum;
impl Enum {
pub fn impl_se(&self) -> proc_macro2::TokenStream {
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
let ident = &self.ident;
let enum_untagged = self.attrs.untagged.is_present();
let variant_serializers = self.variants.iter().map(Variant::se_branch);
quote! {
impl #impl_generics ::rustical_xml::XmlSerialize for #ident #type_generics #where_clause {
fn serialize(
&self,
ns: Option<::quick_xml::name::Namespace>,
tag: Option<&str>,
namespaces: &::std::collections::HashMap<::quick_xml::name::Namespace, &str>,
writer: &mut ::quick_xml::Writer<&mut Vec<u8>>
) -> ::std::io::Result<()> {
use ::quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
let prefix = ns
.map(|ns| namespaces.get(&ns))
.unwrap_or(None)
.map(|prefix| {
if !prefix.is_empty() {
format!("{prefix}:")
} else {
String::new()
}
});
let has_prefix = prefix.is_some();
let tagname = tag.map(|tag| [&prefix.unwrap_or_default(), tag].concat());
let qname = tagname.as_ref().map(|tagname| ::quick_xml::name::QName(tagname.as_bytes()));
const enum_untagged: bool = #enum_untagged;
if let Some(qname) = &qname {
let mut bytes_start = BytesStart::from(qname.to_owned());
if !has_prefix {
if let Some(ns) = &ns {
bytes_start.push_attribute((b"xmlns".as_ref(), ns.as_ref()));
}
}
writer.write_event(Event::Start(bytes_start))?;
}
#(#variant_serializers);*
if let Some(qname) = &qname {
writer.write_event(Event::End(BytesEnd::from(qname.to_owned())))?;
}
Ok(())
}
fn attributes<'a>(&self) -> Option<Vec<::quick_xml::events::attributes::Attribute<'a>>> {
None
}
}
}
}
}