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
use proc_macro::TokenStream;
use syn::{parse_macro_input, ReturnType, Ident, Block};
use quote::quote;
fn quote_without_return(ident: &Ident, block: &Box<Block>, name: String) -> TokenStream {
let result = quote! {
fn #ident() -> crabzilla::ImportedFn {
crabzilla::create_sync_fn(
|args: Vec<crabzilla::Value>| -> Result<crabzilla::Value, crabzilla::AnyError> {
Ok(#block)
},
String::from(#name),
)
}
};
result.into()
}
fn quote_with_return(ident: &Ident, block: &Box<Block>, name: String) -> TokenStream {
let result = quote! {
fn #ident() -> crabzilla::ImportedFn {
crabzilla::create_sync_fn(
|args: Vec<crabzilla::Value>| -> Result<crabzilla::Value, crabzilla::AnyError> {
#block
Ok(crabzilla::Value::Null)
},
String::from(#name),
)
}
};
result.into()
}
#[proc_macro_attribute]
pub fn import_fn(_attr: TokenStream, item: TokenStream) -> TokenStream {
let input = parse_macro_input!(item as syn::ItemFn);
let sig = &input.sig;
let ident = &sig.ident;
let name = ident.to_string();
let block = &input.block;
match input.sig.asyncness {
Some(_) => todo!(),
None => {
if matches!(input.sig.output, ReturnType::Default) {
quote_with_return(ident, block, name)
} else {
quote_without_return(ident, block, name)
}
}
}
}