Skip to main content

actify_macros/
lib.rs

1//! Procedural macros for the [actify](https://docs.rs/actify) crate.
2//!
3//! Everything here is re-exported from `actify`, and the generated code refers
4//! to `actify`'s types, so the macros only work alongside that crate.
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/// Leaves one method off the generated handle trait.
45///
46/// ```ignore
47/// #[actify]
48/// impl Store {
49///     fn len(&self) -> usize { self.entries.len() }
50///
51///     #[actify::skip]
52///     fn merge(&mut self, other: &Store) { .. }
53/// }
54/// ```
55///
56/// The method stays on the type and is unchanged. It is not validated, so it
57/// may take or return references, which an actor call cannot express.
58///
59/// Expands to nothing: `#[actify]` reads it and strips it from the output.
60#[proc_macro_attribute]
61pub fn skip(_args: TokenStream, input: TokenStream) -> TokenStream {
62    input
63}
64
65/// Parsed arguments from `#[actify(...)]`.
66struct ActifyArgs {
67    skip_broadcast: bool,
68    custom_name: Option<syn::LitStr>,
69}
70
71impl syn::parse::Parse for ActifyArgs {
72    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
73        let mut args = ActifyArgs {
74            skip_broadcast: false,
75            custom_name: None,
76        };
77
78        while !input.is_empty() {
79            let ident: syn::Ident = input.parse()?;
80            if ident == "skip_broadcast" {
81                args.skip_broadcast = true;
82            } else if ident == "name" {
83                input.parse::<syn::Token![=]>()?;
84                let name: syn::LitStr = input.parse()?;
85                args.custom_name = Some(name);
86            } else if ident == "broadcast" {
87                return Err(syn::Error::new_spanned(
88                    ident,
89                    "`#[actify(broadcast)]` is not supported; methods taking &mut self broadcast by default, and a method taking &self opts in with `#[actify::broadcast]`",
90                ));
91            } else {
92                return Err(syn::Error::new_spanned(
93                    ident,
94                    "unknown actify attribute; expected `skip_broadcast` or `name = \"...\"`",
95                ));
96            }
97
98            if !input.is_empty() {
99                input.parse::<syn::Token![,]>()?;
100            }
101        }
102
103        Ok(args)
104    }
105}
106
107/// Emit diagnostics together with the impl block they came from.
108///
109/// Returning only the errors would delete every method of the type, so each
110/// call site would report a further "no method named ..." error and bury the
111/// diagnostic that actually explains the problem.
112fn report(error: syn::Error, impl_block: &syn::ItemImpl) -> TokenStream {
113    let compile_errors = error.to_compile_error();
114    quote::quote! {
115        #compile_errors
116        #impl_block
117    }
118    .into()
119}
120
121/// Expands an impl block so its methods can be called remotely through a handle.
122/// The generated handle trait keeps the method signatures, so arguments and
123/// return values stay typed.
124#[proc_macro_attribute]
125pub fn actify(attr: TokenStream, item: TokenStream) -> TokenStream {
126    let mut impl_block = syn::parse_macro_input!(item as syn::ItemImpl);
127
128    let args = match syn::parse::<ActifyArgs>(attr) {
129        Ok(args) => args,
130        Err(error) => return report(error, &impl_block),
131    };
132
133    match parse::ImplInfo::from_impl_block(&mut impl_block, args.skip_broadcast, args.custom_name) {
134        Ok(info) => codegen::generate(&info).into(),
135        Err(error) => report(error, &impl_block),
136    }
137}