1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#![deny(
missing_docs,
trivial_casts,
trivial_numeric_casts,
unsafe_code,
unused_import_braces,
unused_qualifications,
warnings
)]
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Error, ItemFn};
#[proc_macro_attribute]
pub fn node(_args: TokenStream, item: TokenStream) -> TokenStream {
let input_function = parse_macro_input!(item as ItemFn);
if input_function.sig.asyncness.is_none() {
let message = "a function with attribute '#[ockam::node]' must be declared as 'async'";
let token = input_function.sig.fn_token;
return Error::new_spanned(token, message).to_compile_error().into();
}
if input_function.sig.inputs.len() != 1 {
let message = "a function with '#[ockam::node]' must have exactly one argument";
let token = input_function.sig.fn_token;
return Error::new_spanned(token, message).to_compile_error().into();
}
let input_function_attrs = &input_function.attrs;
let input_function_ident = &input_function.sig.ident;
let input_function_block = &input_function.block;
let output_function = quote! {
#(#input_function_attrs)*
fn #input_function_ident() {
let (context, mut executor) = ockam::node();
executor.execute(async move { #input_function_block }).unwrap()
}
};
TokenStream::from(output_function)
}