xml: Move XmlRoot implementation into dedicated derive macro

This commit is contained in:
Lennart
2024-12-22 12:44:19 +01:00
parent 9fe5c00687
commit 043ce8bcd0
5 changed files with 37 additions and 25 deletions

View File

@@ -24,7 +24,8 @@ pub struct VariantAttrs {
#[darling(attributes(xml))]
pub struct EnumAttrs {
#[darling(flatten)]
container: ContainerAttrs,
pub container: ContainerAttrs,
pub untagged: Flag,
}
#[derive(Default, FromDeriveInput, Clone)]

View File

@@ -56,6 +56,17 @@ pub struct NamedStruct {
}
impl NamedStruct {
pub fn impl_xml_root(&self) -> proc_macro2::TokenStream {
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
let ident = &self.ident;
let root = self.attrs.root.as_ref().expect("No root attribute found");
quote! {
impl #impl_generics ::rustical_xml::XmlRoot for #ident #type_generics #where_clause {
fn root_tag() -> &'static [u8] { #root }
}
}
}
pub fn impl_de(&self) -> proc_macro2::TokenStream {
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
let ident = &self.ident;
@@ -76,21 +87,9 @@ impl NamedStruct {
let builder_field_builds = self.fields.iter().map(Field::builder_field_build);
let xml_root_impl = if let Some(root) = &self.attrs.root {
quote! {
impl #impl_generics ::rustical_xml::XmlRoot for #ident #type_generics #where_clause {
fn root_tag() -> &'static [u8] { #root }
}
}
} else {
quote! {}
};
let invalid_field_branch = invalid_field_branch(self.attrs.allow_invalid.is_present());
quote! {
#xml_root_impl
impl #impl_generics ::rustical_xml::XmlDeserialize for #ident #type_generics #where_clause {
fn deserialize<R: BufRead>(
reader: &mut quick_xml::NsReader<R>,

View File

@@ -2,7 +2,6 @@ use core::panic;
use syn::{parse_macro_input, DeriveInput};
mod de;
use de::{impl_de_enum, NamedStruct};
#[proc_macro_derive(XmlDeserialize, attributes(xml))]
@@ -16,3 +15,15 @@ pub fn derive_xml_deserialize(input: proc_macro::TokenStream) -> proc_macro::Tok
}
.into()
}
#[proc_macro_derive(XmlRoot, attributes(xml))]
pub fn derive_xml_root(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse_macro_input!(input as DeriveInput);
match &input.data {
syn::Data::Struct(s) => NamedStruct::parse(&input, s).impl_xml_root(),
syn::Data::Enum(_) => panic!("Enum not supported as root"),
syn::Data::Union(_) => panic!("Union not supported as root"),
}
.into()
}