Simplify output token stream for ToXml macro

This commit is contained in:
Dirkjan Ochtman 2022-09-07 09:53:11 +02:00
parent 6c2dd89ef3
commit c8cc5f5a48
1 changed files with 92 additions and 143 deletions

View File

@ -4,23 +4,15 @@ use quote::quote;
use crate::{ContainerMeta, FieldMeta}; use crate::{ContainerMeta, FieldMeta};
pub fn to_xml(input: &syn::DeriveInput) -> proc_macro2::TokenStream { pub fn to_xml(input: &syn::DeriveInput) -> proc_macro2::TokenStream {
let ident = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let root_name = ident.to_string();
let mut serializer = Serializer::new(input);
let mut header = TokenStream::new();
serializer.add_header(&mut header);
let mut body = TokenStream::new(); let mut body = TokenStream::new();
let mut attributes = TokenStream::new(); let mut attributes = TokenStream::new();
let meta = ContainerMeta::from_derive(input);
match &input.data { match &input.data {
syn::Data::Struct(ref data) => { syn::Data::Struct(ref data) => {
match data.fields { match data.fields {
syn::Fields::Named(ref fields) => { syn::Fields::Named(ref fields) => {
fields.named.iter().for_each(|field| { fields.named.iter().for_each(|field| {
serializer.process_named_field(field, &mut body, &mut attributes); process_named_field(field, &mut body, &mut attributes, &meta);
}); });
} }
syn::Fields::Unnamed(_) => todo!(), syn::Fields::Unnamed(_) => todo!(),
@ -30,10 +22,32 @@ pub fn to_xml(input: &syn::DeriveInput) -> proc_macro2::TokenStream {
_ => todo!(), _ => todo!(),
}; };
let mut footer = TokenStream::new(); let mut prefixes = TokenStream::new();
serializer.add_footer(&root_name, &mut footer); for (key, val) in &meta.ns.prefixes {
prefixes.extend(quote!(
if serializer.parent_namespaces.get(#val).is_none() {
serializer.output.write_str(" xmlns:")?;
serializer.output.write_str(#key)?;
serializer.output.write_str("=\"")?;
serializer.output.write_str(#val)?;
serializer.output.write_char('\"')?;
}
let current_namespaces = serializer.namespaces_token(); if let ::std::collections::hash_map::Entry::Vacant(v) = serializer.parent_namespaces.entry(#val) {
v.insert(#key);
// Will remove added namespaces when going "up"
to_remove.push(#val);
};
));
}
let ident = &input.ident;
let root_name = ident.to_string();
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let default_namespace = match &meta.ns.uri {
Some(ns) => quote!(#ns),
None => quote!(""),
};
quote!( quote!(
impl #impl_generics ToXml for #ident #ty_generics #where_clause { impl #impl_generics ToXml for #ident #ty_generics #where_clause {
@ -49,48 +63,8 @@ pub fn to_xml(input: &syn::DeriveInput) -> proc_macro2::TokenStream {
attribute: None, attribute: None,
}; };
#attributes // Start tag
#header
#current_namespaces
#body
#footer
// Removing current namespaces
for it in to_remove {
serializer.parent_namespaces.remove(it);
}
Ok(())
}
};
)
}
struct Serializer {
meta: ContainerMeta,
}
impl<'a> Serializer {
fn new(input: &syn::DeriveInput) -> Self {
Self {
meta: ContainerMeta::from_derive(input),
}
}
fn add_header(&mut self, output: &'a mut TokenStream) {
output.extend(quote!(
serializer.output.write_char('<')?; serializer.output.write_char('<')?;
));
let default_namespace = match &self.meta.ns.uri {
Some(ns) => quote!(#ns),
None => quote!(""),
};
output.extend(quote!(
// Check if parent default namespace equals
if serializer.parent_default_namespace() != #default_namespace { if serializer.parent_default_namespace() != #default_namespace {
if let Some(prefix) = serializer.parent_namespaces.get(#default_namespace) { if let Some(prefix) = serializer.parent_namespaces.get(#default_namespace) {
serializer.output.write_str(prefix)?; serializer.output.write_str(prefix)?;
@ -105,45 +79,38 @@ impl<'a> Serializer {
} else { } else {
serializer.output.write_str(field_context.name)?; serializer.output.write_str(field_context.name)?;
} }
serializer.update_parent_default_namespace(#default_namespace); serializer.update_parent_default_namespace(#default_namespace);
)); let mut to_remove: Vec<&str> = Vec::new();
#prefixes
for (key, val) in &self.meta.ns.prefixes { #attributes
output.extend(quote!(
if serializer.parent_namespaces.get(#val).is_none() {
serializer.output.write_str(" xmlns:")?;
serializer.output.write_str(#key)?;
serializer.output.write_str("=\"")?;
serializer.output.write_str(#val)?;
serializer.output.write_char('\"')?;
}
));
}
// Attributes
output.extend(quote!(
serializer.consume_current_attributes()?; serializer.consume_current_attributes()?;
));
output.extend(quote!(
serializer.output.write_char('>')?; serializer.output.write_char('>')?;
));
}
fn add_footer(&mut self, root_name: &str, output: &'a mut TokenStream) { #body
output.extend(quote!(
// Close tag
serializer.output.write_str("</")?; serializer.output.write_str("</")?;
serializer.output.write_str(#root_name)?; serializer.output.write_str(#root_name)?;
serializer.output.write_char('>')?; serializer.output.write_char('>')?;
serializer.retrieve_parent_default_namespace(); serializer.retrieve_parent_default_namespace();
));
// Removing current namespaces
for it in to_remove {
serializer.parent_namespaces.remove(it);
}
Ok(())
}
};
)
} }
fn process_named_field( fn process_named_field(
&mut self,
field: &syn::Field, field: &syn::Field,
body: &mut TokenStream, body: &mut TokenStream,
attributes: &mut TokenStream, attributes: &mut TokenStream,
meta: &ContainerMeta,
) { ) {
let name = field.ident.as_ref().unwrap().to_string(); let name = field.ident.as_ref().unwrap().to_string();
let field_value = field.ident.as_ref().unwrap(); let field_value = field.ident.as_ref().unwrap();
@ -170,7 +137,7 @@ impl<'a> Serializer {
let ns = match field_meta.ns.uri { let ns = match field_meta.ns.uri {
Some(ns) => quote!(#ns), Some(ns) => quote!(#ns),
None => match &self.meta.ns.uri { None => match &meta.ns.uri {
Some(ns) => quote!(#ns), Some(ns) => quote!(#ns),
None => quote!(""), None => quote!(""),
}, },
@ -186,21 +153,3 @@ impl<'a> Serializer {
self.#field_value.serialize(serializer)?; self.#field_value.serialize(serializer)?;
)); ));
} }
fn namespaces_token(&self) -> TokenStream {
let mut namespaces = quote!(
let mut to_remove: Vec<&str> = Vec::new();
);
for (k, v) in self.meta.ns.prefixes.iter() {
namespaces.extend(quote!(
// Only adding to HashMap if namespace do not exist, if it exist it will use the parent defined prefix
if let std::collections::hash_map::Entry::Vacant(v) = serializer.parent_namespaces.entry(#v) {
v.insert(#k);
// Will remove added namespaces when going "up"
to_remove.push(#v);
};
))
}
namespaces
}
}