Ignore skipped fields

This commit is contained in:
Osspial 2018-04-09 23:43:49 -04:00
parent c413775574
commit fd14332729
3 changed files with 22 additions and 10 deletions

View File

@ -55,21 +55,22 @@ pub fn with_where_predicates_from_fields<F, W>(
generics: &syn::Generics,
trait_bound: &syn::Path,
from_field: F,
where_ty: W,
gen_bound_where: W,
) -> syn::Generics
where
F: Fn(&attr::Field) -> Option<&[syn::WherePredicate]>,
W: for<'a> Fn(&attr::Field) -> Option<&syn::ExprPath>,
W: for<'a> Fn(&attr::Field) -> bool,
{
let predicates = cont.data
.all_fields()
.flat_map(|field| {
let field_bound: Option<syn::WherePredicate> = match where_ty(&field.attrs) {
Some(_) => None,
None => {
let field_bound: Option<syn::WherePredicate> = match gen_bound_where(&field.attrs) {
true => {
let field_ty = field.ty;
Some(parse_quote!(#field_ty: #trait_bound))
}
let predicate: syn::WherePredicate = parse_quote!(#field_ty: #trait_bound);
Some(predicate)
},
false => None
};
field_bound.into_iter().chain(from_field(&field.attrs).into_iter().flat_map(|predicates| predicates.to_vec()))
});

View File

@ -126,7 +126,13 @@ fn build_generics(cont: &Container, borrowed: &BorrowedLifetimes) -> syn::Generi
let delife = borrowed.de_lifetime();
let de_bound = parse_quote!(_serde::Deserialize<#delife>);
let generics = bound::with_where_predicates_from_fields(cont, &generics, &de_bound, attr::Field::de_bound, attr::Field::deserialize_with);
let generics = bound::with_where_predicates_from_fields(
cont,
&generics,
&de_bound,
attr::Field::de_bound,
|field| field.deserialize_with().is_none() && !field.skip_deserializing()
);
match cont.attrs.de_bound() {
Some(predicates) => bound::with_where_predicates(&generics, predicates),

View File

@ -131,8 +131,13 @@ fn build_generics(cont: &Container) -> syn::Generics {
let generics = bound::without_defaults(cont.generics);
let trait_bound = parse_quote!(_serde::Serialize);
let generics =
bound::with_where_predicates_from_fields(cont, &generics, &trait_bound, attr::Field::ser_bound, attr::Field::serialize_with);
let generics = bound::with_where_predicates_from_fields(
cont,
&generics,
&trait_bound,
attr::Field::ser_bound,
|field| field.serialize_with().is_none() && !field.skip_serializing()
);
match cont.attrs.ser_bound() {
Some(predicates) => bound::with_where_predicates(&generics, predicates),