Crate sourcerer_derive

Crate sourcerer_derive 

Source
Expand description

Procedural macros for the sourcerer event–sourcing framework.

§#[derive(Event)]

The Event derive automatically implements the sourcerer::Event trait for an enum. It generates:

  • event_type – returns the variant name as a &'static str.
  • event_version – configurable per–enum or per–variant (defaults to 1).
  • event_source – configurable per–enum or per–variant (defaults to "urn:sourcerer:event").

§Attribute syntax

#[derive(Event)]
// Enum-level defaults
#[event(version = 2, source = "urn:my-service")]
enum AccountEvent {
    // Inherits version = 2, source = "urn:my-service".
    Opened,

    // Override only the version; source inherits from the enum.
    #[event(version = 3)]
    Credited { amount: u64 },

    // Override both.
    #[event(version = 4, source = "urn:custom")]
    Debited(u64),
}

§Generated behaviour

assert_eq!(AccountEvent::Opened.event_type(), "Opened");
assert_eq!(AccountEvent::Opened.event_version(), 2);
assert_eq!(AccountEvent::Opened.event_source(), "urn:my-service");

assert_eq!(AccountEvent::Credited { amount: 10 }.event_version(), 3);
assert_eq!(AccountEvent::Credited { amount: 10 }.event_source(), "urn:my-service");

assert_eq!(AccountEvent::Debited(5).event_version(), 4);
assert_eq!(AccountEvent::Debited(5).event_source(), "urn:custom");

§Notes

  • The macro works for unit, tuple and struct variants.
  • Unknown keys in the event(...) attribute are ignored, which future-proofs the API for additional options.
  • All variants must implement Serialize and DeserializeOwned (the blanket #[derive(Serialize, Deserialize)] on the enum is usually sufficient).

Currently sourcerer-derive only provides the Event macro. More helpers may be added in the future.

Derive Macros§

Event
Derives the Event trait for an enum.