make serde Serialize and Deserialize generation optional
This commit is contained in:
parent
700e98917d
commit
c5d46270b4
@ -5,7 +5,7 @@ use std::convert::identity;
|
|||||||
use convert_case::{Case, Casing};
|
use convert_case::{Case, Casing};
|
||||||
use darling::{
|
use darling::{
|
||||||
ast::{self, Data},
|
ast::{self, Data},
|
||||||
util::{Override, PathList},
|
util::{Flag, Override, PathList},
|
||||||
Error, FromDeriveInput, FromField, FromMeta,
|
Error, FromDeriveInput, FromField, FromMeta,
|
||||||
};
|
};
|
||||||
use proc_macro2::{Span, TokenStream};
|
use proc_macro2::{Span, TokenStream};
|
||||||
@ -26,6 +26,7 @@ pub struct ConfigField {
|
|||||||
typ: Option<syn::Type>,
|
typ: Option<syn::Type>,
|
||||||
get: Option<syn::Path>,
|
get: Option<syn::Path>,
|
||||||
set: Option<syn::Path>,
|
set: Option<syn::Path>,
|
||||||
|
no_serde: Flag,
|
||||||
#[darling(skip)]
|
#[darling(skip)]
|
||||||
description: Option<syn::Expr>,
|
description: Option<syn::Expr>,
|
||||||
#[darling(skip)]
|
#[darling(skip)]
|
||||||
@ -34,7 +35,7 @@ pub struct ConfigField {
|
|||||||
|
|
||||||
impl ConfigField {
|
impl ConfigField {
|
||||||
pub(crate) fn needs_newtype(&self) -> bool {
|
pub(crate) fn needs_newtype(&self) -> bool {
|
||||||
self.helper_keys().len() > 1
|
self.helper_keys().len() > 1 || self.typ.is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn helper(&self, attrs: &[syn::Attribute]) -> (Option<TokenStream>, syn::Type) {
|
pub(crate) fn helper(&self, attrs: &[syn::Attribute]) -> (Option<TokenStream>, syn::Type) {
|
||||||
@ -170,7 +171,10 @@ impl ConfigField {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn helper_serde(&self) -> TokenStream {
|
pub(crate) fn helper_serde(&self) -> Option<TokenStream> {
|
||||||
|
if self.no_serde.is_present() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
let ident = self.helper_ident();
|
let ident = self.helper_ident();
|
||||||
let conversion = if self.has_custom_limits() {
|
let conversion = if self.has_custom_limits() {
|
||||||
quote! {
|
quote! {
|
||||||
@ -184,7 +188,7 @@ impl ConfigField {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
let ty = &self.ty;
|
let ty = &self.ty;
|
||||||
quote! {
|
Some(quote! {
|
||||||
impl ::serde::Serialize for #ident {
|
impl ::serde::Serialize for #ident {
|
||||||
fn serialize<S>(&self, serializer: S) -> ::core::result::Result::<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> ::core::result::Result::<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@ -204,7 +208,7 @@ impl ConfigField {
|
|||||||
#conversion
|
#conversion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn helper_tree(&self) -> TokenStream {
|
pub(crate) fn helper_tree(&self) -> TokenStream {
|
||||||
@ -350,6 +354,13 @@ impl ConfigField {
|
|||||||
.typ
|
.typ
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map_or_else(|| quote!(Self), |typ| quote!(#typ));
|
.map_or_else(|| quote!(Self), |typ| quote!(#typ));
|
||||||
|
let match_range = if num_keys > 1 {
|
||||||
|
Some(
|
||||||
|
quote!(1..=#num_keys => ::core::result::Result::Err(::miniconf::Traversal::Access(0, "Cannot write limits").into()),),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
quote! {
|
quote! {
|
||||||
impl<'de> ::miniconf::TreeDeserialize<'de, 1> for #ident {
|
impl<'de> ::miniconf::TreeDeserialize<'de, 1> for #ident {
|
||||||
fn deserialize_by_key<K, D>(
|
fn deserialize_by_key<K, D>(
|
||||||
@ -374,8 +385,7 @@ impl ConfigField {
|
|||||||
#set(<#typ as ::serde::Deserialize>::deserialize(de).map_err(|err| ::miniconf::Error::Inner(0, err))?)?;
|
#set(<#typ as ::serde::Deserialize>::deserialize(de).map_err(|err| ::miniconf::Error::Inner(0, err))?)?;
|
||||||
Ok(0)
|
Ok(0)
|
||||||
}
|
}
|
||||||
,
|
#match_range
|
||||||
1..=#num_keys => ::core::result::Result::Err(::miniconf::Traversal::Access(0, "Cannot write limits").into()),
|
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -391,6 +401,13 @@ impl ConfigField {
|
|||||||
} else {
|
} else {
|
||||||
quote!(::core::result::Result::Ok(&mut *self))
|
quote!(::core::result::Result::Ok(&mut *self))
|
||||||
};
|
};
|
||||||
|
let match_range = if num_keys > 1 {
|
||||||
|
Some(
|
||||||
|
quote!(1..#num_keys => ::core::result::Result::Err(::miniconf::Traversal::Access(1, "cannot return reference to local variable")),),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
quote! {
|
quote! {
|
||||||
impl ::miniconf::TreeAny<1> for #ident {
|
impl ::miniconf::TreeAny<1> for #ident {
|
||||||
fn ref_any_by_key<K>(&self, mut keys: K) -> ::core::result::Result<&dyn ::core::any::Any, ::miniconf::Traversal>
|
fn ref_any_by_key<K>(&self, mut keys: K) -> ::core::result::Result<&dyn ::core::any::Any, ::miniconf::Traversal>
|
||||||
@ -406,7 +423,7 @@ impl ConfigField {
|
|||||||
}
|
}
|
||||||
match index {
|
match index {
|
||||||
0 => ::core::result::Result::Ok(&*self),
|
0 => ::core::result::Result::Ok(&*self),
|
||||||
1..#num_keys => ::core::result::Result::Err(::miniconf::Traversal::Access(1, "cannot return reference to local variable")),
|
#match_range
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -424,7 +441,7 @@ impl ConfigField {
|
|||||||
}
|
}
|
||||||
match index {
|
match index {
|
||||||
0 => #ref_mut,
|
0 => #ref_mut,
|
||||||
1..#num_keys => ::core::result::Result::Err(::miniconf::Traversal::Access(1, "cannot return reference to local variable")),
|
#match_range
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user