Fix clippy warnings and build with heck 0.4

This commit is contained in:
Ephraim Kunz 2022-01-07 08:39:25 -08:00
parent 19b51168c9
commit 7148ec67c5
8 changed files with 46 additions and 59 deletions

View File

@ -474,10 +474,7 @@ fn struct_default_namespace_no_prefix() {
init();
#[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
#[yaserde(
rename = "book",
namespace = "http://www.sample.com/ns/domain"
)]
#[yaserde(rename = "book", namespace = "http://www.sample.com/ns/domain")]
pub struct Book {
author: String,
title: String,
@ -497,4 +494,4 @@ fn struct_default_namespace_no_prefix() {
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, Book);
}
}

View File

@ -21,7 +21,7 @@ fn get_value(iter: &mut IntoIter) -> Option<String> {
(iter.next(), iter.next())
{
if operator.as_char() == '=' {
Some(value.to_string().replace("\"", ""))
Some(value.to_string().replace('"', ""))
} else {
None
}
@ -138,14 +138,13 @@ impl YaSerdeAttribute {
let namespaces_matches: TokenStream = self
.namespaces
.iter()
.map(|(prefix, namespace)| {
.filter_map(|(prefix, namespace)| {
if configured_prefix.eq(prefix) {
Some(quote!(#namespace => {}))
} else {
None
}
})
.flatten()
.collect();
quote!(
@ -316,7 +315,10 @@ fn parse_attributes_with_values() {
let attrs = YaSerdeAttribute::parse(&attributes);
let mut namespaces = BTreeMap::new();
namespaces.insert(Some("example".to_string()), "http://example.org".to_string());
namespaces.insert(
Some("example".to_string()),
"http://example.org".to_string(),
);
assert_eq!(
YaSerdeAttribute {

View File

@ -1,5 +1,5 @@
use crate::common::attribute::YaSerdeAttribute;
use heck::CamelCase;
use heck::ToUpperCamelCase;
use proc_macro2::Span;
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote};
@ -89,7 +89,7 @@ impl YaSerdeField {
Ident::new(
&format!(
"__Visitor_{}_{}",
label.replace(".", "_").to_camel_case(),
label.replace('.', "_").to_upper_camel_case(),
struct_id
),
self.get_span(),
@ -109,7 +109,7 @@ impl YaSerdeField {
.attributes
.default
.as_ref()
.map(|default| Ident::new(&default, self.get_span()))
.map(|default| Ident::new(default, self.get_span()))
}
pub fn get_skip_serializing_if_function(&self) -> Option<Ident> {
@ -117,7 +117,7 @@ impl YaSerdeField {
.attributes
.skip_serializing_if
.as_ref()
.map(|skip_serializing_if| Ident::new(&skip_serializing_if, self.get_span()))
.map(|skip_serializing_if| Ident::new(skip_serializing_if, self.get_span()))
}
pub fn get_namespace_matching(

View File

@ -19,8 +19,7 @@ pub fn parse(
let match_to_enum: TokenStream = data_enum
.variants
.iter()
.map(|variant| parse_variant(variant, name))
.flatten()
.filter_map(|variant| parse_variant(variant, name))
.collect();
let flatten = root_attributes.flatten;
@ -218,7 +217,7 @@ fn build_unnamed_visitor_calls(
.iter()
.map(|field| YaSerdeField::new(field.clone()))
.enumerate()
.map(|(idx, field)| {
.filter_map(|(idx, field)| {
let visitor_label = Ident::new(&format!("__Visitor_{}", idx), field.get_span());
let call_simple_type_visitor = |simple_type: Field, action| {
@ -302,6 +301,5 @@ fn build_unnamed_visitor_calls(
simple_type => call_simple_type_visitor(simple_type, set_val),
}
})
.flatten()
.collect()
}

View File

@ -1,6 +1,6 @@
use crate::common::{Field, YaSerdeAttribute, YaSerdeField};
use crate::de::build_default_value::build_default_value;
use heck::CamelCase;
use heck::ToUpperCamelCase;
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{DataStruct, Ident};
@ -22,7 +22,7 @@ pub fn parse(
.fields
.iter()
.map(|field| YaSerdeField::new(field.clone()))
.map(|field| match field.get_type() {
.filter_map(|field| match field.get_type() {
Field::FieldStruct { struct_name } => build_default_value(
&field,
Some(quote!(#struct_name)),
@ -57,14 +57,13 @@ pub fn parse(
build_default_value(&field, Some(type_token), value_builder)
}
})
.flatten()
.collect();
let field_visitors: TokenStream = data_struct
.fields
.iter()
.map(|field| YaSerdeField::new(field.clone()))
.map(|field| {
.filter_map(|field| {
let struct_visitor = |struct_name: syn::Path| {
let struct_id: String = struct_name
.segments
@ -137,7 +136,6 @@ pub fn parse(
simple_type => simple_type_visitor(simple_type),
}
})
.flatten()
.collect();
let call_visitors: TokenStream = data_struct
@ -145,7 +143,7 @@ pub fn parse(
.iter()
.map(|field| YaSerdeField::new(field.clone()))
.filter(|field| !field.is_attribute() || !field.is_flatten())
.map(|field| {
.filter_map(|field| {
let value_label = field.get_value_label();
let label_name = field.renamed_label_without_namespace();
@ -175,7 +173,7 @@ pub fn parse(
&field_visitor,
&action,
&field,
&root_attributes,
root_attributes,
)
};
@ -194,7 +192,6 @@ pub fn parse(
simple_type => visit_simple(simple_type, quote! { = value }),
}
})
.flatten()
.collect();
let call_flatten_visitors: TokenStream = data_struct
@ -202,7 +199,7 @@ pub fn parse(
.iter()
.map(|field| YaSerdeField::new(field.clone()))
.filter(|field| !field.is_attribute() && field.is_flatten())
.map(|field| {
.filter_map(|field| {
let value_label = field.get_value_label();
match field.get_type() {
@ -218,7 +215,6 @@ pub fn parse(
field_type => unimplemented!(r#""flatten" is not implemented for {:?}"#, field_type),
}
})
.flatten()
.collect();
let attributes_loading: TokenStream = data_struct
@ -226,7 +222,7 @@ pub fn parse(
.iter()
.map(|field| YaSerdeField::new(field.clone()))
.filter(|field| field.is_attribute())
.map(|field| {
.filter_map(|field| {
let label = field.get_value_label();
let label_name = field.renamed_label_without_namespace();
let visitor_label = build_visitor_ident(&label_name, field.get_span(), None);
@ -285,14 +281,13 @@ pub fn parse(
simple_type => visit_simple(simple_type, quote! { = value }),
}
})
.flatten()
.collect();
let set_text: TokenStream = data_struct
.fields
.iter()
.map(|field| YaSerdeField::new(field.clone()))
.map(|field| {
.filter_map(|field| {
let label = field.get_value_label();
let set_text = |action: &TokenStream| {
@ -318,7 +313,6 @@ pub fn parse(
}
}
})
.flatten()
.collect();
let struct_builder: TokenStream = data_struct
@ -489,7 +483,7 @@ fn build_visitor_ident(label: &str, span: Span, struct_name: Option<&syn::Path>)
Ident::new(
&format!(
"__Visitor_{}_{}",
label.replace(".", "_").to_camel_case(),
label.replace('.', "_").to_upper_camel_case(),
struct_id
),
span,

View File

@ -37,22 +37,22 @@ fn inner_enum_inspector(
let variant_attrs = YaSerdeAttribute::parse(&variant.attrs);
let label = &variant.ident;
let label_name = build_label_name(&label, &variant_attrs, &root_attributes.default_namespace);
let label_name = build_label_name(label, &variant_attrs, &root_attributes.default_namespace);
match variant.fields {
Fields::Unit => Some(quote! {
Fields::Unit => quote! {
&#name::#label => {
let data_event = ::yaserde::__xml::writer::XmlEvent::characters(#label_name);
writer.write(data_event).map_err(|e| e.to_string())?;
}
}),
},
Fields::Named(ref fields) => {
let enum_fields: TokenStream = fields
.named
.iter()
.map(|field| YaSerdeField::new(field.clone()))
.filter(|field| !field.is_attribute())
.map(|field| {
.filter_map(|field| {
let field_label = field.label();
if field.is_text_content() {
@ -124,14 +124,13 @@ fn inner_enum_inspector(
Field::FieldOption { .. } => None,
}
})
.flatten()
.collect();
Some(quote! {
quote! {
&#name::#label{..} => {
#enum_fields
}
})
}
}
Fields::Unnamed(ref fields) => {
let enum_fields: TokenStream = fields
@ -191,37 +190,35 @@ fn inner_enum_inspector(
Field::FieldOption { data_type } => {
let write = write_sub_type(*data_type);
Some(match_field(&quote! {
match_field(&quote! {
if let ::std::option::Option::Some(item) = item {
#write
}
}))
})
}
Field::FieldVec { data_type } => {
let write = write_sub_type(*data_type);
Some(match_field(&quote! {
match_field(&quote! {
for item in item {
#write
}
}))
})
}
Field::FieldStruct { .. } => Some(write_element(&match_field(&serialize))),
Field::FieldString => Some(match_field(&write_element(&write_string_chars))),
_simple_type => Some(match_field(&write_simple_type)),
Field::FieldStruct { .. } => write_element(&match_field(&serialize)),
Field::FieldString => match_field(&write_element(&write_string_chars)),
_simple_type => match_field(&write_simple_type),
}
})
.flatten()
.collect();
Some(quote! {
quote! {
&#name::#label{..} => {
#enum_fields
}
})
}
}
}
})
.flatten()
.collect()
}

View File

@ -142,7 +142,7 @@ pub fn serialize(
.iter()
.map(|field| YaSerdeField::new(field.clone()))
.filter(|field| !field.is_attribute())
.map(|field| {
.filter_map(|field| {
let label = field.label();
if field.is_text_content() {
return match field.get_type() {
@ -324,7 +324,6 @@ pub fn serialize(
},
}
})
.flatten()
.collect();
implement_serializer(

View File

@ -8,14 +8,14 @@ pub fn generate_namespaces_definition(attributes: &YaSerdeAttribute) -> TokenStr
.iter()
.map(|(prefix, namespace)| {
if attributes.default_namespace.eq(prefix) {
return Some(quote!(
.default_ns(#namespace)
));
quote!(
.default_ns(#namespace)
)
} else {
quote!(
.ns(#prefix, #namespace)
)
}
Some(quote!(
.ns(#prefix, #namespace)
))
})
.flatten()
.collect()
}