From ddb1b48e719f0c27575f3337d21ff9e50f31c814 Mon Sep 17 00:00:00 2001 From: Devin Ragotzy Date: Sat, 8 Aug 2020 21:01:40 -0400 Subject: [PATCH] Pass the attributes of any type deriving Outgoing to the Incoming type --- ruma-api-macros/src/derive_outgoing.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ruma-api-macros/src/derive_outgoing.rs b/ruma-api-macros/src/derive_outgoing.rs index bb81b753..2a5dfc29 100644 --- a/ruma-api-macros/src/derive_outgoing.rs +++ b/ruma-api-macros/src/derive_outgoing.rs @@ -24,6 +24,9 @@ pub fn expand_derive_outgoing(input: DeriveInput) -> syn::Result { quote!(::ruma_api::exports::serde::Deserialize) }; + let input_attrs = + input.attrs.clone().into_iter().filter(filter_input_attrs).collect::>(); + let data = match input.data.clone() { Data::Union(_) => panic!("#[derive(Outgoing)] does not support Union types"), Data::Enum(e) => DataKind::Enum(e.variants.into_iter().collect()), @@ -67,6 +70,7 @@ pub fn expand_derive_outgoing(input: DeriveInput) -> syn::Result { Ok(quote! { #[doc = #doc] #[derive(Debug, #derive_deserialize)] + #( #input_attrs )* #vis enum #incoming_ident #ty_gen { #( #vars, )* } impl #original_impl_gen ::ruma_api::Outgoing for #original_ident #original_ty_gen { @@ -104,6 +108,7 @@ pub fn expand_derive_outgoing(input: DeriveInput) -> syn::Result { Ok(quote! { #[doc = #doc] #[derive(Debug, #derive_deserialize)] + #( #input_attrs )* #vis struct #incoming_ident #ty_gen #struct_def impl #original_impl_gen ::ruma_api::Outgoing for #original_ident #original_ty_gen { @@ -114,6 +119,12 @@ pub fn expand_derive_outgoing(input: DeriveInput) -> syn::Result { } } +/// Keep any `serde` or `non_exhaustive` attributes found and +/// pass them to the Incoming variant. +fn filter_input_attrs(attr: &Attribute) -> bool { + attr.path.is_ident("serde") || attr.path.is_ident("non_exhaustive") +} + fn no_deserialize_in_attrs(attrs: &[Attribute]) -> bool { attrs.iter().any(|attr| attr.path.is_ident("incoming_no_deserialize")) }