remove root attribute, use rename

This commit is contained in:
Marc-Antoine Arnaud
2020-04-22 19:04:43 +02:00
parent 0872461c41
commit 975baabd76
11 changed files with 104 additions and 134 deletions

View File

@@ -1,6 +1,4 @@
use proc_macro2::token_stream::IntoIter;
use proc_macro2::Delimiter;
use proc_macro2::TokenTree;
use proc_macro2::{token_stream::IntoIter, Delimiter, TokenStream, TokenTree};
use std::collections::BTreeMap;
use syn::Attribute;
@@ -12,7 +10,6 @@ pub struct YaSerdeAttribute {
pub flatten: bool,
pub namespaces: BTreeMap<String, String>,
pub prefix: Option<String>,
pub root: Option<String>,
pub rename: Option<String>,
pub skip_serializing_if: Option<String>,
pub text: bool,
@@ -41,7 +38,6 @@ impl YaSerdeAttribute {
let mut namespaces = BTreeMap::new();
let mut prefix = None;
let mut rename = None;
let mut root = None;
let mut skip_serializing_if = None;
let mut text = false;
@@ -84,9 +80,6 @@ impl YaSerdeAttribute {
"rename" => {
rename = get_value(&mut attr_iter);
}
"root" => {
root = get_value(&mut attr_iter);
}
"skip_serializing_if" => {
skip_serializing_if = get_value(&mut attr_iter);
}
@@ -110,11 +103,51 @@ impl YaSerdeAttribute {
namespaces,
prefix,
rename,
root,
skip_serializing_if,
text,
}
}
pub fn get_namespace_matching(
&self,
prefix: &Option<String>,
element_namespace: TokenStream,
element_name: TokenStream,
take_root_prefix: bool
) -> TokenStream {
let configured_prefix =
if take_root_prefix {
self.prefix.clone()
} else {
prefix.clone()
};
let namespaces_matches : TokenStream =
self
.namespaces
.iter()
.map(|(prefix, namespace)| {
if configured_prefix == Some(prefix.to_string()) {
Some(quote!(#namespace => {}))
} else {
None
}
})
.filter_map(|x| x)
.collect();
quote!(
if let Some(namespace) = #element_namespace {
match namespace.as_str() {
#namespaces_matches
bad_namespace => {
let msg = format!("bad namespace for {}, found {}", #element_name, bad_namespace);
return Err(msg);
}
}
}
)
}
}
#[test]
@@ -130,7 +163,6 @@ fn parse_empty_attributes() {
flatten: false,
namespaces: BTreeMap::new(),
prefix: None,
root: None,
rename: None,
skip_serializing_if: None,
text: false,
@@ -180,7 +212,6 @@ fn parse_attributes() {
flatten: false,
namespaces: BTreeMap::new(),
prefix: None,
root: None,
rename: None,
skip_serializing_if: None,
text: false,
@@ -205,7 +236,6 @@ fn parse_attributes_with_values() {
arguments: PathArguments::None,
});
// #[()]
let attributes = vec![Attribute {
pound_token: Pound {
spans: [Span::call_site()],
@@ -234,7 +264,6 @@ fn parse_attributes_with_values() {
flatten: true,
namespaces,
prefix: None,
root: None,
rename: None,
skip_serializing_if: None,
text: false,

View File

@@ -118,19 +118,13 @@ impl YaSerdeField {
.map(|skip_serializing_if| Ident::new(&skip_serializing_if, self.get_span()))
}
pub fn get_namespace_matching(&self, root_attributes: &YaSerdeAttribute) -> TokenStream {
root_attributes
.namespaces
.iter()
.map(|(prefix, namespace)| {
if self.attributes.prefix == Some(prefix.to_string()) {
Some(quote!(#namespace => {}))
} else {
None
}
})
.filter_map(|x| x)
.collect()
pub fn get_namespace_matching(
&self,
root_attributes: &YaSerdeAttribute,
element_namespace: TokenStream,
element_name: TokenStream,
) -> TokenStream {
root_attributes.get_namespace_matching(&self.attributes.prefix, element_namespace, element_name, false)
}
pub fn ser_wrap_default_attribute(
@@ -184,37 +178,6 @@ pub enum Field {
}
impl Field {
pub fn is_attribute(token_field: &syn::Field) -> bool {
YaSerdeAttribute::parse(&token_field.attrs).attribute
}
pub fn is_text_content(token_field: &syn::Field) -> bool {
YaSerdeAttribute::parse(&token_field.attrs).text
}
pub fn label(token_field: &syn::Field) -> Option<Ident> {
token_field.ident.clone()
}
pub fn renamed_label(token_field: &syn::Field, root_attributes: &YaSerdeAttribute) -> String {
let attributes = YaSerdeAttribute::parse(&token_field.attrs);
let prefix = if root_attributes.default_namespace == attributes.prefix {
"".to_string()
} else {
attributes
.prefix
.clone()
.map_or("".to_string(), |prefix| prefix + ":")
};
let label = attributes
.rename
.unwrap_or_else(|| token_field.ident.as_ref().unwrap().to_string());
format!("{}{}", prefix, label)
}
pub fn get_simple_type_visitor(&self) -> TokenStream {
let ident = format_ident!("visit_{}", self.to_string());
quote! {#ident}

View File

@@ -9,18 +9,7 @@ pub fn parse(
root: &str,
root_attributes: &YaSerdeAttribute,
) -> TokenStream {
let namespaces_matches: TokenStream = root_attributes
.namespaces
.iter()
.map(|(prefix, namespace)| {
if root_attributes.prefix.as_ref() == Some(prefix) {
Some(quote!(#namespace => {}))
} else {
None
}
})
.filter_map(|x| x)
.collect();
let namespaces_matching = root_attributes.get_namespace_matching(&None, quote!(struct_namespace), quote!(named_element), true);
let variables: TokenStream = data_struct
.fields
@@ -330,15 +319,7 @@ pub fn parse(
debug!("Struct: start to parse {:?}", named_element);
if reader.depth() == 0 {
if let Some(ref namespace) = struct_namespace {
match namespace.as_str() {
#namespaces_matches
bad_ns => {
let msg = format!("bad namespace for {}, found {}", named_element, bad_ns);
return Err(msg);
}
}
}
#namespaces_matching
}
#variables
@@ -419,21 +400,13 @@ fn build_call_visitor(
let label_name = field.renamed_label_without_namespace();
let visitor_label = build_visitor_ident(&label_name, field.get_span(), None);
let namespaces_matches = field.get_namespace_matching(root_attributes);
let namespaces_matching = field.get_namespace_matching(root_attributes, quote!(name.namespace.as_ref()), quote!(name.local_name.as_str()));
Some(quote! {
#label_name => {
let visitor = #visitor_label{};
if let Some(namespace) = name.namespace.as_ref() {
match namespace.as_str() {
#namespaces_matches
bad_ns => {
let msg = format!("bad field namespace for {}, found {}", name.local_name.as_str(), bad_ns);
return Err(msg);
}
}
}
#namespaces_matching
let result = reader.read_inner_value::<#field_type, _>(|reader| {
if let Ok(XmlEvent::Characters(s)) = reader.peek() {

View File

@@ -12,14 +12,14 @@ pub fn expand_derive_deserialize(ast: &syn::DeriveInput) -> Result<TokenStream,
let attrs = &ast.attrs;
let data = &ast.data;
let root_attrs = YaSerdeAttribute::parse(attrs);
let root = root_attrs.clone().root.unwrap_or_else(|| name.to_string());
let root_attributes = YaSerdeAttribute::parse(attrs);
let root_name = root_attributes.clone().rename.unwrap_or_else(|| name.to_string());
let impl_block = match *data {
syn::Data::Struct(ref data_struct) => {
expand_struct::parse(data_struct, name, &root, &root_attrs)
expand_struct::parse(data_struct, name, &root_name, &root_attributes)
}
syn::Data::Enum(ref data_enum) => expand_enum::parse(data_enum, name, &root, &root_attrs),
syn::Data::Enum(ref data_enum) => expand_enum::parse(data_enum, name, &root_name, &root_attributes),
syn::Data::Union(ref _data_union) => unimplemented!(),
};

View File

@@ -15,25 +15,30 @@ pub fn expand_derive_serialize(ast: &syn::DeriveInput) -> Result<TokenStream, St
let attrs = &ast.attrs;
let data = &ast.data;
let root_attrs = YaSerdeAttribute::parse(attrs);
let root = root_attrs.clone().root.unwrap_or_else(|| name.to_string());
let root_attributes = YaSerdeAttribute::parse(attrs);
let root_name = root_attributes
.clone()
.rename
.unwrap_or_else(|| name.to_string());
let prefix = if root_attrs.default_namespace == root_attrs.prefix {
let prefix = if root_attributes.default_namespace == root_attributes.prefix {
"".to_string()
} else {
root_attrs
root_attributes
.clone()
.prefix
.map_or("".to_string(), |prefix| prefix + ":")
};
let root = format!("{}{}", prefix, root);
let root_name = format!("{}{}", prefix, root_name);
let impl_block = match *data {
syn::Data::Struct(ref data_struct) => {
expand_struct::serialize(data_struct, name, &root, &root_attrs)
expand_struct::serialize(data_struct, name, &root_name, &root_attributes)
}
syn::Data::Enum(ref data_enum) => {
expand_enum::serialize(data_enum, name, &root_name, &root_attributes)
}
syn::Data::Enum(ref data_enum) => expand_enum::serialize(data_enum, name, &root, &root_attrs),
syn::Data::Union(ref _data_union) => unimplemented!(),
};