Skip to content

Commit

Permalink
Merge pull request #32 from wasmCloud/derive-empty-sendonly
Browse files Browse the repository at this point in the history
generate empty dispatch for providers that do not implement any recei…
  • Loading branch information
stevelr authored Sep 13, 2021
2 parents bb59298 + d0766a1 commit dbb8309
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasmbus-macros"
version = "0.1.5"
version = "0.1.6"
edition = "2018"
authors = [ "wasmcloud Team" ]
license = "Apache-2.0"
Expand Down
34 changes: 20 additions & 14 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use syn::{
};

/// extract traits from attribute
/// `#[services(Apple,Banana)]` returns vec![ Piano, Tuba ]
/// `#[services(Piano,Tuba)]` returns vec![ Piano, Tuba ]
/// items in the vec are syn::Path, and may have more than one path segment,
/// as in instruments::Piano
///
Expand Down Expand Up @@ -215,6 +215,20 @@ fn gen_dispatch(traits: &[syn::Path], ident: &Ident) -> TokenStream2 {
)
}

// for providers that do not implement any Service Receivers
// (for example, HttpServer that sends only)
// implement MessageDispatch that always return error if we receive rpc
fn gen_empty_dispatch(ident: &Ident) -> TokenStream2 {
quote!(
#[async_trait]
impl MessageDispatch for #ident {
async fn dispatch(&self,_ctx: &Context,message: Message<'_>)->Result<Message<'_>, RpcError> {
Err(RpcError::MethodNotHandled(message.method.to_string()))
}
}
)
}

#[proc_macro_error]
#[proc_macro_derive(Provider, attributes(services))]
pub fn derive_provider(input: TokenStream) -> TokenStream {
Expand All @@ -224,19 +238,11 @@ pub fn derive_provider(input: TokenStream) -> TokenStream {
for attr in provider_receiver.attrs.iter() {
traits.extend(attr_traits(attr, "services"));
}
if traits.is_empty() {
abort!(
provider_receiver.attrs_span,
"Missing list of traits. try `#[services(Trait1,Trait2)]`"
);
}
let ident = provider_receiver.ident;
//let fields = actor_receiver.fields;
let dispatch_impl = gen_dispatch(&traits, &ident);
let output = quote!(

#dispatch_impl

);
let output = if traits.is_empty() {
gen_empty_dispatch(&ident)
} else {
gen_dispatch(&traits, &ident)
};
output.into()
}

0 comments on commit dbb8309

Please sign in to comment.