momento_functions/macros/
function_spawn.rs

1/// Create a handler for a momento::host::spawn::spawn_function.
2///
3/// You can use raw bytes, or json-marshalled types.
4///
5/// **Raw:**
6/// ```rust
7/// momento_functions::spawn!(triggered);
8/// fn triggered(payload: Vec<u8>) {
9///     ()
10/// }
11/// ```
12///
13/// **Typed JSON:**
14/// ```rust
15/// #[derive(serde::Deserialize)]
16/// struct Request {
17///     name: String,
18/// }
19///
20/// momento_functions::spawn!(greet, Request);
21/// fn greet(request: Request) -> () {
22///     Ok(())
23/// }
24/// ```
25#[macro_export]
26macro_rules! spawn {
27    ($spawn_handler: ident) => {
28        struct SpawnFunction;
29        momento_functions_wit::function_spawn::export_spawn_function!(SpawnFunction);
30
31        #[automatically_derived]
32        impl momento_functions_wit::function_spawn::exports::momento::functions::guest_function_spawn::Guest for SpawnFunction {
33            fn spawned(payload: Vec<u8>) {
34                $spawn_handler(payload)
35            }
36        }
37    };
38
39    ($post_handler: ident, $request: ident) => {
40        struct SpawnFunction;
41        momento_functions_wit::function_spawn::export_spawn_function!(SpawnFunction);
42
43        #[automatically_derived]
44        impl momento_functions_wit::function_spawn::exports::momento::functions::guest_function_spawn::Guest for SpawnFunction {
45            fn spawned(payload: Vec<u8>) {
46                let payload: $request = serde_json::from_slice(&payload).expect("payload is not valid json");
47                $post_handler(payload)
48            }
49        }
50    }
51}