kojin_macros/lib.rs
1//! Proc-macro support for the kojin task queue.
2//!
3//! Provides the `#[kojin::task]` attribute macro that generates a task struct
4//! and `Task` trait implementation from an async function. Most users should
5//! use this via the [`kojin`](https://crates.io/crates/kojin) facade crate.
6
7use proc_macro::TokenStream;
8use syn::parse_macro_input;
9
10mod codegen;
11mod task_attr;
12
13/// Derive a task struct and `Task` impl from an async function.
14///
15/// # Example
16/// ```ignore
17/// #[task(queue = "emails", max_retries = 5)]
18/// async fn send_email(ctx: &TaskContext, to: String, subject: String) -> TaskResult<()> {
19/// // ...
20/// Ok(())
21/// }
22/// ```
23///
24/// This generates:
25/// - `struct SendEmail { pub to: String, pub subject: String }`
26/// - `impl Task for SendEmail { ... }`
27/// - `SendEmail::new(to, subject)`
28#[proc_macro_attribute]
29pub fn task(attr: TokenStream, item: TokenStream) -> TokenStream {
30 let attr = parse_macro_input!(attr as task_attr::TaskAttr);
31 let func = parse_macro_input!(item as syn::ItemFn);
32
33 match codegen::generate_task(&attr, &func) {
34 Ok(tokens) => tokens.into(),
35 Err(err) => err.to_compile_error().into(),
36 }
37}