synapse_macros/lib.rs
1use proc_macro::TokenStream;
2mod arc_wrap;
3mod rpc_error;
4
5/// Automatically wraps a struct with an `Inner` postfix in an `Arc`
6///
7/// This macro generates:
8/// 1. A wrapper struct that contains `Arc<OriginalStructInner>`
9/// 2. A `Clone` implementation for the wrapper
10/// 3. A `Deref` implementation that derefs to the inner struct
11/// 4. A `new` function that takes the inner struct and wraps it in Arc
12///
13/// # Example
14///
15/// ```ignore
16/// use synapse_macros::arc_wrap;
17///
18/// #[arc_wrap]
19/// pub struct MyService {
20/// pub data: String,
21/// pub count: u32,
22/// }
23///
24/// // This generates:
25/// // pub struct MyServiceInner { pub data: String, pub count: u32 }
26/// // pub struct MyService(Arc<MyServiceInner>);
27/// // impl Clone for MyService { ... }
28/// // impl Deref for MyService { ... }
29/// // impl MyService { pub fn new(inner: MyServiceInner) -> Self { ... } }
30/// ```
31#[proc_macro_attribute]
32pub fn arc_wrap(attr: TokenStream, item: TokenStream) -> TokenStream {
33 arc_wrap::arc_wrap_inner(attr, item)
34}
35
36/// Derive macro for RPC errors
37///
38/// Generates `From<YourError> for ServiceError` and `IntoServiceError` implementations.
39///
40/// Use `#[rpc(status = "...", code = ...)]` attributes on variants to customize
41/// the RPC status and error code.
42///
43/// # Example
44///
45/// ```ignore
46/// use synapse_macros::RpcError;
47///
48/// #[derive(Debug, thiserror::Error, RpcError)]
49/// pub enum UserError {
50/// #[error("user not found: {0}")]
51/// #[rpc(status = "Error", code = 404)]
52/// NotFound(String),
53///
54/// #[error("invalid email format")]
55/// #[rpc(status = "InvalidRequest", code = 400)]
56/// InvalidEmail,
57///
58/// #[error("user is banned")]
59/// #[rpc(code = 3001)] // defaults to Error status
60/// Banned,
61/// }
62/// ```
63///
64/// # Available statuses
65///
66/// - `Ok` - Success
67/// - `Error` - Generic error (default)
68/// - `Timeout` - Request timed out
69/// - `InterfaceNotFound` - Interface not found
70/// - `MethodNotFound` - Method not found
71/// - `Unavailable` - Service unavailable
72/// - `InvalidRequest` - Bad request
73/// - `UnsupportedVersion` - Unsupported protocol version
74/// - `ServiceDraining` - Service is draining
75/// - `ServiceFrozen` - Service is frozen
76/// - `MessageTooLarge` - Message too large
77#[proc_macro_derive(RpcError, attributes(rpc))]
78pub fn derive_rpc_error(input: TokenStream) -> TokenStream {
79 rpc_error::derive_rpc_error_inner(input)
80}