Skip to content

Commit

Permalink
remove all attributes on method signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Dilly committed Nov 11, 2019
1 parent afaebce commit 46dbba6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion rest-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ proc-macro = true
[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
syn = {version="1.0", features=["full"]}
syn = {version="1.0", features=["full","visit-mut"]}
26 changes: 25 additions & 1 deletion rest-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use proc_macro2::{Ident, Span};
use quote::quote;
use syn::export::TokenStream;
use syn::Meta;
use syn::visit_mut::{self,VisitMut};

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

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

// remove all method param attributes
FnSigAttrRemove.visit_item_trait_mut(&mut input);

// outputting it all
let result = quote! {
#input
Expand Down Expand Up @@ -102,6 +106,9 @@ fn get_impl_method(trait_item: syn::TraitItemMethod) -> syn::ImplItemMethod {

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

// remove all method param attributes
FnSigAttrRemove.visit_signature_mut(&mut method_impl.sig);

method_impl
}

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

struct FnSigAttrRemove;
impl VisitMut for FnSigAttrRemove {
fn visit_fn_arg_mut(&mut self, node: &mut syn::FnArg) {
match node {
syn::FnArg::Receiver(arg) => {
arg.attrs.clear();
}
syn::FnArg::Typed(arg) => {
arg.attrs.clear();
}
}

// Delegate to the default impl to visit nested nodes
visit_mut::visit_fn_arg_mut(self, node);
}
}

#[proc_macro_attribute]
pub fn endpoint(_attr: TokenStream, item: TokenStream) -> TokenStream {
item
Expand Down

0 comments on commit 46dbba6

Please sign in to comment.