synapse-macros 0.0.1

Procedural macros for Synapse RPC (derive RpcError, arc_wrap)
Documentation
use proc_macro::TokenStream;
mod arc_wrap;
mod rpc_error;

/// Automatically wraps a struct with an `Inner` postfix in an `Arc`
///
/// This macro generates:
/// 1. A wrapper struct that contains `Arc<OriginalStructInner>`
/// 2. A `Clone` implementation for the wrapper
/// 3. A `Deref` implementation that derefs to the inner struct
/// 4. A `new` function that takes the inner struct and wraps it in Arc
///
/// # Example
///
/// ```ignore
/// use synapse_macros::arc_wrap;
///
/// #[arc_wrap]
/// pub struct MyService {
///     pub data: String,
///     pub count: u32,
/// }
///
/// // This generates:
/// // pub struct MyServiceInner { pub data: String, pub count: u32 }
/// // pub struct MyService(Arc<MyServiceInner>);
/// // impl Clone for MyService { ... }
/// // impl Deref for MyService { ... }
/// // impl MyService { pub fn new(inner: MyServiceInner) -> Self { ... } }
/// ```
#[proc_macro_attribute]
pub fn arc_wrap(attr: TokenStream, item: TokenStream) -> TokenStream {
    arc_wrap::arc_wrap_inner(attr, item)
}

/// Derive macro for RPC errors
///
/// Generates `From<YourError> for ServiceError` and `IntoServiceError` implementations.
///
/// Use `#[rpc(status = "...", code = ...)]` attributes on variants to customize
/// the RPC status and error code.
///
/// # Example
///
/// ```ignore
/// use synapse_macros::RpcError;
///
/// #[derive(Debug, thiserror::Error, RpcError)]
/// pub enum UserError {
///     #[error("user not found: {0}")]
///     #[rpc(status = "Error", code = 404)]
///     NotFound(String),
///
///     #[error("invalid email format")]
///     #[rpc(status = "InvalidRequest", code = 400)]
///     InvalidEmail,
///
///     #[error("user is banned")]
///     #[rpc(code = 3001)]  // defaults to Error status
///     Banned,
/// }
/// ```
///
/// # Available statuses
///
/// - `Ok` - Success
/// - `Error` - Generic error (default)
/// - `Timeout` - Request timed out
/// - `InterfaceNotFound` - Interface not found
/// - `MethodNotFound` - Method not found
/// - `Unavailable` - Service unavailable
/// - `InvalidRequest` - Bad request
/// - `UnsupportedVersion` - Unsupported protocol version
/// - `ServiceDraining` - Service is draining
/// - `ServiceFrozen` - Service is frozen
/// - `MessageTooLarge` - Message too large
#[proc_macro_derive(RpcError, attributes(rpc))]
pub fn derive_rpc_error(input: TokenStream) -> TokenStream {
    rpc_error::derive_rpc_error_inner(input)
}