camel-bean-macros
Proc-macros for the camel-bean crate that enable ergonomic bean definition and automatic BeanProcessor trait implementation.
Overview
This crate provides three proc-macros:
#[bean_impl]- Main macro for generatingBeanProcessorimpl from impl blocks#[handler]- Marker attribute for bean handler methods#[derive(Bean)]- Placeholder derive macro (not recommended)
Macros
#[bean_impl] - Generate BeanProcessor Implementation
The primary macro that transforms an impl block into a BeanProcessor implementation with automatic parameter binding.
use ;
;
What it generates:
#[handler] - Mark Handler Methods
Marker attribute that identifies which methods should be exposed as bean handlers.
Rules:
- Must be
async - First parameter must be
&self - Supported parameter types:
body: T- Extracted from exchange body (requiresTryFrom<Body>)headers: Headers- Cloned from exchange headersexchange: &mut Exchange- Full exchange access
- Return type must be
Result<T, E>whereE: Display
#[derive(Bean)] - Placeholder (Not Recommended)
Derive macro that generates a placeholder BeanProcessor implementation.
⚠️ Warning: This macro is incomplete and requires manual impl block with #[handler] methods. Use #[bean_impl] instead.
// NOT RECOMMENDED
;
// You still need to write impl block manually
Parameter Binding
The #[bean_impl] macro automatically extracts parameters by name:
Parameter name inference:
body→ Extracted fromexchange.input.bodyviaTryFrom<Body>headers→ Cloned fromexchange.input.headersexchange→ Mutable reference to the full exchange
Return Type Handling
Return types are automatically handled:
Result handling:
Ok(T)→ Body set to JSON-serializedT(ifTis not())Err(E)→exchange.set_error()called with error message
Error Handling
The macro generates proper error handling:
Generated error handling:
- Parameter extraction failures →
CamelError::ProcessorError - Method not found →
BeanError::MethodNotFound - Handler errors → Set on exchange via
set_error()
Integration with camel-bean
This crate is a dependency of camel-bean and is re-exported for convenience:
// Usually you don't depend on camel-bean-macros directly
use ;
// camel-bean re-exports the macros
Examples
See the bean-demo example for a complete working example.
Implementation Details
Code Generation
The #[bean_impl] macro:
- Parses the impl block to find
#[handler]methods - Validates method signatures (async, &self, valid parameters)
- Generates parameter extraction code for each handler
- Generates method dispatch via
matchon method name - Generates result handling (set body or error)
- Preserves original impl block unchanged
Supported Types
Parameter types:
body: TwhereT: TryFrom<Body>headers: Headers(cloned)exchange: &mut Exchange
Return types:
Result<T, E>whereE: DisplayTis serialized to JSON on success()is allowed (no body set on success)
See Also
- camel-bean - Main bean registry and usage examples
- examples/bean-demo - Complete working example
- Apache Camel Bean Binding - Inspiration
License
Apache-2.0