remote_trait_object_macro/
lib.rs

1//! This crate provides one core attribute procedural macro that is attached to the trait that you want to use as a service.
2//! See more details in `remote-trait-object` crate.
3
4#[macro_use]
5extern crate quote;
6
7mod helper;
8mod service;
9
10use proc_macro::TokenStream;
11use proc_macro2::TokenStream as TokenStream2;
12
13/// Those necessary components for the macro is specially exported in the remote-trait-object.
14/// The macro will always specify full path using this.
15fn create_env_path() -> syn::Path {
16    syn::parse2(quote! {remote_trait_object::macro_env}).unwrap()
17}
18
19/// It generates all necessary helper `struct`s that makes the trait be able to be used as a service.
20///
21/// It takes three arguments optionally
22/// - `serde_format = _` - Specify a type that implements `trait SerdeFormat`. The default is [serde_cbor](https://github.com/pyfisch/cbor)
23/// - `no_proxy` - If provided, the trait will be used only as a service object.
24/// - `no_skeleton` - If provided, the trait will be used only as a proxy object.
25///
26/// There will be many new public `struct`s, but you don't have to know about them.
27#[proc_macro_attribute]
28pub fn service(args: TokenStream, input: TokenStream) -> TokenStream {
29    match service::service(TokenStream2::from(args), TokenStream2::from(input)) {
30        Ok(x) => TokenStream::from(x),
31        Err(x) => TokenStream::from(x),
32    }
33}
34
35/// This macro consumes the target trait, and will print the expanded code. Use this when you want to see the result of macro.
36#[proc_macro_attribute]
37pub fn service_debug(args: TokenStream, input: TokenStream) -> TokenStream {
38    match service::service(TokenStream2::from(args), TokenStream2::from(input)) {
39        Ok(x) => println!("{}", x),
40        Err(x) => println!("{}", x),
41    }
42    TokenStream::new()
43}