Skip to main content

durable_lambda_macro/
lib.rs

1//! Proc-macro approach for durable Lambda handlers.
2//!
3//! Provides the `#[durable_execution]` attribute macro (FR32).
4//!
5//! # Usage
6//!
7//! Annotate an async handler function with `#[durable_execution]` to generate
8//! all Lambda runtime boilerplate. The macro creates a `main()` function that
9//! wires up AWS config, Lambda client, `RealBackend`, and `lambda_runtime`.
10//!
11//! ```ignore
12//! use durable_lambda_macro::durable_execution;
13//! use durable_lambda_core::context::DurableContext;
14//! use durable_lambda_core::error::DurableError;
15//!
16//! #[durable_execution]
17//! async fn handler(event: serde_json::Value, mut ctx: DurableContext) -> Result<serde_json::Value, DurableError> {
18//!     Ok(event)
19//! }
20//! ```
21
22mod expand;
23
24use proc_macro::TokenStream;
25use syn::parse_macro_input;
26
27/// Attribute macro that transforms an async handler into a complete durable Lambda binary.
28///
29/// The annotated function must:
30/// - Be `async`
31/// - Have exactly 2 parameters: `(event: serde_json::Value, ctx: DurableContext)`
32/// - Return `Result<serde_json::Value, DurableError>`
33///
34/// The macro preserves the original function and generates a `#[tokio::main] async fn main()`
35/// that sets up the Lambda runtime, AWS backend, and event parsing — mirroring the
36/// `#[tokio::main]` ergonomic pattern.
37#[proc_macro_attribute]
38pub fn durable_execution(_attr: TokenStream, item: TokenStream) -> TokenStream {
39    let func = parse_macro_input!(item as syn::ItemFn);
40
41    match expand::expand_durable_execution(func) {
42        Ok(tokens) => tokens.into(),
43        Err(err) => err.to_compile_error().into(),
44    }
45}