Skip to main content

aggregator

Attribute Macro aggregator 

Source
#[aggregator]
Expand description

Transforms an enum into individual event structs with trait implementations.

This macro takes an enum where each variant represents an event type and generates:

  • Individual public structs for each variant
  • Aggregator trait implementation (provides aggregator_type())
  • AggregatorEvent trait implementation (provides event_name())
  • A unit struct with the enum name implementing Aggregator
  • Automatic derives: Debug, Clone, PartialEq, Default, and bitcode serialization

§Aggregator Type Format

The aggregator type is formatted as "{package_name}/{enum_name}", e.g., "bank/BankAccount".

§Example

#[evento::aggregator]
pub enum BankAccount {
    /// Event raised when account is opened
    AccountOpened {
        owner_id: String,
        owner_name: String,
        initial_balance: i64,
    },

    MoneyDeposited {
        amount: i64,
        transaction_id: String,
    },
}

// Generated structs can be used directly:
let event = AccountOpened {
    owner_id: "user123".into(),
    owner_name: "John".into(),
    initial_balance: 1000,
};

§Additional Derives

Pass additional derives as arguments:

#[evento::aggregator(serde::Serialize, serde::Deserialize)]
pub enum MyEvents {
    // variants...
}

§Variant Types

Supports all enum variant types:

  • Named fields: Variant { field: Type }
  • Tuple fields: Variant(Type1, Type2)
  • Unit variants: Variant