evento-macro
Procedural macros for the Evento event sourcing framework.
Overview
This crate provides macros that eliminate boilerplate when building event-sourced applications. It generates trait implementations, handler structs, and serialization code automatically.
Installation
This crate is typically used through the main evento crate with the macro feature enabled (on by default):
[]
= "2"
Or use this crate directly:
[]
= "2"
Macros
| Macro | Type | Purpose |
|---|---|---|
#[evento::aggregator] |
Attribute | Transform enum into event structs |
#[evento::handler] |
Attribute | Create event handler from async function |
#[evento::snapshot] |
Attribute | Implement snapshot restoration |
#[evento::debug_handler] |
Attribute | Like handler with debug output |
#[evento::debug_snapshot] |
Attribute | Like snapshot with debug output |
Usage
Defining Events with #[evento::aggregator]
Transform an enum into individual event structs with all required trait implementations:
This generates:
- Individual structs:
AccountOpened,MoneyDeposited,MoneyWithdrawn,AccountClosed Aggregatortrait implementation (providesaggregator_type())Eventtrait implementation (providesevent_name())- Automatic derives:
Debug,Clone,PartialEq,Default, and bitcode serialization
The aggregator type is formatted as "{package_name}/{enum_name}", e.g., "bank/BankAccount".
Additional Derives
Pass additional derives as arguments:
Creating Handlers with #[evento::handler]
Create event handlers for projections:
use ;
use Action;
async
// Register with a projection
let projection = new
.handler
.handler
.handler;
The macro generates:
HandleMoneyDepositedHandlerstructhandle_money_deposited()constructor functionHandler<AccountBalanceView, E>trait implementation
Snapshot Restoration with #[evento::snapshot]
Implement snapshot restoration for projections:
use LoadResult;
use RwContext;
async
Debug Macros
Use #[evento::debug_handler] or #[evento::debug_snapshot] to output the generated code to a file for inspection:
async
// Generated code written to: target/evento_debug_handler_macro.rs
Requirements
When using these macros, your types must meet certain requirements:
- Events (from
#[aggregator]): Traits are automatically derived - Projections: Must implement
Default,Send,Sync,Clone - Handler functions: Must be
asyncand returnanyhow::Result<()>
Serialization
Events are serialized using bitcode for compact binary representation. The #[aggregator] macro automatically adds the required bitcode derives:
bitcode::Encodebitcode::Decode
Minimum Supported Rust Version
Rust 1.75 or later.
Safety
This crate uses #![forbid(unsafe_code)] to ensure everything is implemented in 100% safe Rust.
License
See the LICENSE file in the repository root.