mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
rustical_xml: Add new trait EnumVariants
This commit is contained in:
@@ -61,3 +61,15 @@ pub fn derive_xml_document(input: proc_macro::TokenStream) -> proc_macro::TokenS
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
#[proc_macro_derive(EnumVariants, attributes(xml))]
|
||||
pub fn derive_enum_variants(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
|
||||
match &input.data {
|
||||
syn::Data::Struct(_) => panic!("Struct not supported, use XmlRootTag instead"),
|
||||
syn::Data::Enum(e) => Enum::parse(&input, e).impl_enum_variants(),
|
||||
syn::Data::Union(_) => panic!("Union not supported as root"),
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
@@ -193,4 +193,27 @@ impl Enum {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn impl_enum_variants(&self) -> proc_macro2::TokenStream {
|
||||
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
|
||||
let ident = &self.ident;
|
||||
|
||||
let tagged_variants = self.variants.iter().map(|variant| {
|
||||
let ns = match &variant.attrs.common.ns {
|
||||
Some(ns) => quote! { Some(#ns) },
|
||||
None => quote! { None },
|
||||
};
|
||||
let b_xml_name = variant.xml_name().value();
|
||||
let xml_name = String::from_utf8_lossy(&b_xml_name);
|
||||
quote! {(#ns, #xml_name)}
|
||||
});
|
||||
|
||||
quote! {
|
||||
impl #impl_generics ::rustical_xml::EnumVariants for #ident #type_generics #where_clause {
|
||||
const TAGGED_VARIANTS: &'static [(Option<::quick_xml::name::Namespace<'static>>, &'static str)] = &[
|
||||
#(#tagged_variants),*
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user