mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 12:52:27 +00:00
xml: Move XmlRoot implementation into dedicated derive macro
This commit is contained in:
@@ -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)]
|
||||
|
||||
@@ -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>,
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user