macros: Allow opt-out of Debug
deriving for incoming types
This commit is contained in:
parent
83d60d1fab
commit
146c237ec4
@ -261,6 +261,10 @@ pub fn user_id(input: TokenStream) -> TokenStream {
|
|||||||
/// Generating an 'Incoming' version of the type this derive macro is used on.
|
/// Generating an 'Incoming' version of the type this derive macro is used on.
|
||||||
///
|
///
|
||||||
/// This type will be a fully-owned version of the input type, using no lifetime generics.
|
/// This type will be a fully-owned version of the input type, using no lifetime generics.
|
||||||
|
///
|
||||||
|
/// By default, the generated type will derive `Debug` and `serde::Deserialize`. To derive
|
||||||
|
/// additional traits, use `#[incoming_derive(ExtraDeriveMacro)]`. To disable the default derives,
|
||||||
|
/// use `#[incoming_derive(!Debug, !Deserialize)]`.
|
||||||
#[proc_macro_derive(Incoming, attributes(incoming_derive))]
|
#[proc_macro_derive(Incoming, attributes(incoming_derive))]
|
||||||
pub fn derive_incoming(input: TokenStream) -> TokenStream {
|
pub fn derive_incoming(input: TokenStream) -> TokenStream {
|
||||||
let input = parse_macro_input!(input as DeriveInput);
|
let input = parse_macro_input!(input as DeriveInput);
|
||||||
|
@ -59,27 +59,35 @@ pub fn expand_derive_incoming(mut ty_def: DeriveInput) -> syn::Result<TokenStrea
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut derives = vec![quote! { Debug }];
|
let meta: Vec<Meta> = ty_def
|
||||||
|
.attrs
|
||||||
|
.iter()
|
||||||
|
.filter(|attr| attr.path.is_ident("incoming_derive"))
|
||||||
|
.map(|attr| attr.parse_args())
|
||||||
|
.collect::<syn::Result<_>>()?;
|
||||||
|
|
||||||
|
let mut derive_debug = true;
|
||||||
let mut derive_deserialize = true;
|
let mut derive_deserialize = true;
|
||||||
|
|
||||||
derives.extend(
|
let mut derives: Vec<_> = meta
|
||||||
ty_def
|
.into_iter()
|
||||||
.attrs
|
.flat_map(|m| m.derive_macs)
|
||||||
.iter()
|
.filter_map(|derive_mac| match derive_mac {
|
||||||
.filter(|attr| attr.path.is_ident("incoming_derive"))
|
DeriveMac::Regular(id) => Some(quote! { #id }),
|
||||||
.map(|attr| attr.parse_args())
|
DeriveMac::NegativeDebug => {
|
||||||
.collect::<syn::Result<Vec<Meta>>>()?
|
derive_debug = false;
|
||||||
.into_iter()
|
None
|
||||||
.flat_map(|meta| meta.derive_macs)
|
}
|
||||||
.filter_map(|derive_mac| match derive_mac {
|
DeriveMac::NegativeDeserialize => {
|
||||||
DeriveMac::Regular(id) => Some(quote! { #id }),
|
derive_deserialize = false;
|
||||||
DeriveMac::NegativeDeserialize => {
|
None
|
||||||
derive_deserialize = false;
|
}
|
||||||
None
|
})
|
||||||
}
|
.collect();
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
|
if derive_debug {
|
||||||
|
derives.push(quote! { ::std::fmt::Debug });
|
||||||
|
}
|
||||||
derives.push(if derive_deserialize {
|
derives.push(if derive_deserialize {
|
||||||
quote! { #ruma_common::exports::serde::Deserialize }
|
quote! { #ruma_common::exports::serde::Deserialize }
|
||||||
} else {
|
} else {
|
||||||
@ -265,6 +273,7 @@ impl Parse for Meta {
|
|||||||
|
|
||||||
pub enum DeriveMac {
|
pub enum DeriveMac {
|
||||||
Regular(Path),
|
Regular(Path),
|
||||||
|
NegativeDebug,
|
||||||
NegativeDeserialize,
|
NegativeDeserialize,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -274,14 +283,16 @@ impl Parse for DeriveMac {
|
|||||||
let _: Token![!] = input.parse()?;
|
let _: Token![!] = input.parse()?;
|
||||||
let mac: Ident = input.parse()?;
|
let mac: Ident = input.parse()?;
|
||||||
|
|
||||||
if mac != "Deserialize" {
|
if mac == "Debug" {
|
||||||
return Err(syn::Error::new_spanned(
|
Ok(Self::NegativeDebug)
|
||||||
|
} else if mac == "Deserialize" {
|
||||||
|
Ok(Self::NegativeDeserialize)
|
||||||
|
} else {
|
||||||
|
Err(syn::Error::new_spanned(
|
||||||
mac,
|
mac,
|
||||||
"Negative incoming_derive can only be used for Deserialize",
|
"Negative incoming_derive can only be used for Debug and Deserialize",
|
||||||
));
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Self::NegativeDeserialize)
|
|
||||||
} else {
|
} else {
|
||||||
let mac = input.parse()?;
|
let mac = input.parse()?;
|
||||||
Ok(Self::Regular(mac))
|
Ok(Self::Regular(mac))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user