schedule_flows_macros/
lib.rs

1use proc_macro::TokenStream;
2use quote::{quote, ToTokens};
3
4#[proc_macro_attribute]
5pub fn schedule_handler(_: TokenStream, item: TokenStream) -> TokenStream {
6    let ast: syn::ItemFn = syn::parse(item).unwrap();
7    let func_ident = ast.sig.ident.clone();
8
9    let gen = quote! {
10        mod schedule_flows_macros {
11            extern "C" {
12                pub fn get_event_body_length() -> i32;
13                pub fn get_event_body(p: *mut u8) -> i32;
14            }
15        }
16
17        fn __scheduled() -> Option<Vec<u8>> {
18            unsafe {
19                let l = schedule_flows_macros::get_event_body_length();
20                let mut event_body = Vec::<u8>::with_capacity(l as usize);
21                let c = schedule_flows_macros::get_event_body(event_body.as_mut_ptr());
22                assert!(c == l);
23                event_body.set_len(c as usize);
24
25                Some(event_body)
26            }
27        }
28
29        #[no_mangle]
30        #[tokio::main(flavor = "current_thread")]
31        pub async fn __schedule__on_triggered() {
32            if let Some(body) = __scheduled() {
33                #func_ident(body).await;
34            }
35        }
36    };
37
38    let ori_run_str = ast.to_token_stream().to_string();
39    let x = gen.to_string() + &ori_run_str;
40    x.parse().unwrap()
41}