Skip to main content

roam_service_macros/
lib.rs

1//! Procedural macros for roam RPC service definitions.
2//!
3//! The `#[service]` macro generates everything needed for a roam RPC service.
4//! All generation logic lives in `roam-macros-core` for testability.
5
6use proc_macro::TokenStream;
7use proc_macro2::TokenStream as TokenStream2;
8
9// r[service-macro.is-source-of-truth]
10/// Marks a trait as a roam RPC service and generates all service code.
11///
12/// # Generated Items
13///
14/// For a trait named `Calculator`, this generates:
15/// - `mod calculator` containing:
16///   - `pub use` of common types (Tx, Rx, RoamError, etc.)
17///   - `mod method_id` with lazy method ID functions
18///   - `trait Calculator` - the service trait
19///   - `struct CalculatorDispatcher<H>` - server-side dispatcher
20///   - `struct CalculatorClient` - client for making calls
21///
22/// # Example
23///
24/// ```ignore
25/// #[roam::service]
26/// trait Calculator {
27///     async fn add(&self, a: i32, b: i32) -> i32;
28/// }
29/// ```
30#[proc_macro_attribute]
31pub fn service(_attr: TokenStream, item: TokenStream) -> TokenStream {
32    let input = TokenStream2::from(item);
33
34    let parsed = match roam_macros_core::parse(&input) {
35        Ok(p) => p,
36        Err(e) => return e.to_compile_error().into(),
37    };
38
39    match roam_macros_core::generate_service(&parsed, &roam_macros_core::roam_crate()) {
40        Ok(tokens) => tokens.into(),
41        Err(e) => e.to_compile_error().into(),
42    }
43}