Skip to main content

actify_macros/
lib.rs

1//! Procedural macros for the [actify](https://docs.rs/actify) crate.
2//!
3//! Depend on `actify` rather than this crate: everything here is re-exported
4//! from there, and the generated code refers to `actify`'s types.
5#![warn(missing_docs)]
6
7mod codegen;
8mod parse;
9
10use proc_macro::TokenStream;
11
12/// Marks a method inside an `#[actify]` impl block as not broadcasting.
13///
14/// ```ignore
15/// #[actify]
16/// impl Counter {
17///     #[actify::skip_broadcast]
18///     fn bump_quietly(&mut self) { self.count += 1 }
19/// }
20/// ```
21///
22/// Expands to nothing: `#[actify]` reads it and strips it from the output.
23#[proc_macro_attribute]
24pub fn skip_broadcast(_args: TokenStream, input: TokenStream) -> TokenStream {
25    input
26}
27
28/// Restores broadcasting for one method of an `#[actify(skip_broadcast)]` block.
29///
30/// ```ignore
31/// #[actify(skip_broadcast)]
32/// impl Counter {
33///     #[actify::broadcast]
34///     fn bump_loudly(&mut self) { self.count += 1 }
35/// }
36/// ```
37///
38/// Expands to nothing: `#[actify]` reads it and strips it from the output.
39#[proc_macro_attribute]
40pub fn broadcast(_args: TokenStream, input: TokenStream) -> TokenStream {
41    input
42}
43
44/// Parsed arguments from `#[actify(...)]`.
45struct ActifyArgs {
46    skip_broadcast: bool,
47    custom_name: Option<syn::LitStr>,
48}
49
50impl syn::parse::Parse for ActifyArgs {
51    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
52        let mut args = ActifyArgs {
53            skip_broadcast: false,
54            custom_name: None,
55        };
56
57        while !input.is_empty() {
58            let ident: syn::Ident = input.parse()?;
59            if ident == "skip_broadcast" {
60                args.skip_broadcast = true;
61            } else if ident == "name" {
62                input.parse::<syn::Token![=]>()?;
63                let name: syn::LitStr = input.parse()?;
64                args.custom_name = Some(name);
65            } else if ident == "broadcast" {
66                return Err(syn::Error::new_spanned(
67                    ident,
68                    "methods already broadcast by default; `#[actify(broadcast)]` is unnecessary",
69                ));
70            } else {
71                return Err(syn::Error::new_spanned(
72                    ident,
73                    "unknown actify attribute; expected `skip_broadcast` or `name = \"...\"`",
74                ));
75            }
76
77            if !input.is_empty() {
78                input.parse::<syn::Token![,]>()?;
79            }
80        }
81
82        Ok(args)
83    }
84}
85
86/// Emit diagnostics together with the impl block they came from.
87///
88/// Returning only the errors would delete every method of the type, so each
89/// call site would report a further "no method named ..." error and bury the
90/// diagnostic that actually explains the problem.
91fn report(error: syn::Error, impl_block: &syn::ItemImpl) -> TokenStream {
92    let compile_errors = error.to_compile_error();
93    quote::quote! {
94        #compile_errors
95        #impl_block
96    }
97    .into()
98}
99
100/// The actify macro expands an impl block of a rust struct to support usage in an actor model.
101/// Effectively, this macro allows to remotely call an actor method through a handle.
102/// By using traits, the methods on the handle have the same signatures, so that type checking is enforced
103#[proc_macro_attribute]
104pub fn actify(attr: TokenStream, item: TokenStream) -> TokenStream {
105    let mut impl_block = syn::parse_macro_input!(item as syn::ItemImpl);
106
107    let args = match syn::parse::<ActifyArgs>(attr) {
108        Ok(args) => args,
109        Err(error) => return report(error, &impl_block),
110    };
111
112    match parse::ImplInfo::from_impl_block(&mut impl_block, args.skip_broadcast, args.custom_name) {
113        Ok(info) => codegen::generate(&info).into(),
114        Err(error) => report(error, &impl_block),
115    }
116}