Skip to content

Commit 46dbba6

Browse files
author
Stephan Dilly
committed
remove all attributes on method signatures
1 parent afaebce commit 46dbba6

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

rest-api/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ proc-macro = true
1111
[dependencies]
1212
proc-macro2 = "1.0"
1313
quote = "1.0"
14-
syn = {version="1.0", features=["full"]}
14+
syn = {version="1.0", features=["full","visit-mut"]}

rest-api/src/lib.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use proc_macro2::{Ident, Span};
22
use quote::quote;
33
use syn::export::TokenStream;
44
use syn::Meta;
5+
use syn::visit_mut::{self,VisitMut};
56

67
#[proc_macro_attribute]
78
pub fn api(_attr: TokenStream, item: TokenStream) -> TokenStream {
8-
let input = syn::parse_macro_input!(item as syn::ItemTrait);
9+
let mut input = syn::parse_macro_input!(item as syn::ItemTrait);
910
let name = &input.ident;
1011

1112
let struct_name = Ident::new(&format!("{}RestClient", name), Span::call_site());
@@ -44,6 +45,9 @@ pub fn api(_attr: TokenStream, item: TokenStream) -> TokenStream {
4445
.map(|a| syn::ImplItem::from(get_impl_item(a)))
4546
.collect();
4647

48+
// remove all method param attributes
49+
FnSigAttrRemove.visit_item_trait_mut(&mut input);
50+
4751
// outputting it all
4852
let result = quote! {
4953
#input
@@ -102,6 +106,9 @@ fn get_impl_method(trait_item: syn::TraitItemMethod) -> syn::ImplItemMethod {
102106

103107
method_impl.sig = trait_item.clone().sig;
104108

109+
// remove all method param attributes
110+
FnSigAttrRemove.visit_signature_mut(&mut method_impl.sig);
111+
105112
method_impl
106113
}
107114

@@ -126,6 +133,23 @@ fn get_endpoint_attr(trait_item: syn::TraitItemMethod) -> Option<String> {
126133
.next()
127134
}
128135

136+
struct FnSigAttrRemove;
137+
impl VisitMut for FnSigAttrRemove {
138+
fn visit_fn_arg_mut(&mut self, node: &mut syn::FnArg) {
139+
match node {
140+
syn::FnArg::Receiver(arg) => {
141+
arg.attrs.clear();
142+
}
143+
syn::FnArg::Typed(arg) => {
144+
arg.attrs.clear();
145+
}
146+
}
147+
148+
// Delegate to the default impl to visit nested nodes
149+
visit_mut::visit_fn_arg_mut(self, node);
150+
}
151+
}
152+
129153
#[proc_macro_attribute]
130154
pub fn endpoint(_attr: TokenStream, item: TokenStream) -> TokenStream {
131155
item

0 commit comments

Comments
 (0)