notion_flows_macros/
lib.rs

1use proc_macro::TokenStream;
2use quote::{quote, ToTokens};
3
4#[proc_macro_attribute]
5pub fn database_update_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 notion_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
18        fn __database_updated() -> Option<Page> {
19            unsafe {
20                let l = notion_flows_macros::get_event_body_length();
21                let mut event_body = Vec::<u8>::with_capacity(l as usize);
22                let c = notion_flows_macros::get_event_body(event_body.as_mut_ptr());
23                assert!(c == l);
24                event_body.set_len(c as usize);
25
26                match serde_json::from_slice::<Page>(&event_body) {
27                    Ok(e) => Some(e),
28                    Err(_) => None,
29                }
30            }
31        }
32
33        #[no_mangle]
34        #[tokio::main(flavor = "current_thread")]
35        pub async fn __notion__on_database_updated() {
36            if let Some(body) = __database_updated() {
37                #func_ident(body).await;
38            }
39        }
40    };
41
42    let ori_run_str = ast.to_token_stream().to_string();
43    let x = gen.to_string() + &ori_run_str;
44    x.parse().unwrap()
45}