Skip to main content

Event

Derive Macro Event 

#[derive(Event)]
{
    // Attributes available to this derive:
    #[subject]
}
Expand description

Derive macro that implements arminius_lib_message_bus::Event.

§Attributes

#[subject(...)] — the NATS subject the event is published on. Supported forms:

  1. #[subject("MyEvent.v1")]
  2. #[subject(name = "MyEvent", version = "1")]
  3. #[subject(name = "MyEvent", version("1"))]
  4. #[subject(name = "MyEvent", version = env!("CARGO_PKG_VERSION_MAJOR"))]
  5. #[subject(name = "MyEvent", version(env!("CARGO_PKG_VERSION_MAJOR")))]
  6. #[subject(name = "MyEvent", version = "2", compatible = [v0::MyEvent])]
  7. #[subject(version = env!("CARGO_PKG_VERSION_MAJOR"))]
  8. #[subject(version = "2", compatible = [v1::MyEvent, v0::MyEvent])]

§Subject rules

  • Struct-name fallback. When name is omitted the struct name is used.
  • compatible list. Names older event types whose subjects the subscriber will also consume, converting each to Self via the required From<OldType> for Self implementation.

§Dynamic (per-instance) subjects

The subject template (the string literal, or the name value) may embed {field} placeholders naming struct fields. The generated Event::SUBJECT constant replaces each placeholder with a * wildcard (used to subscribe / as the stream filter), while the generated subject(&self) method interpolates each field’s Display value to build the concrete subject the instance publishes to. Multiple placeholders map to one * each, in order; with no placeholders subject(&self) equals SUBJECT.

ⓘ
// SUBJECT == "rooms.*.events.v1"
// RoomCreated { room_id: 7, .. }.subject() == "rooms.7.events.v1"
#[derive(Event)]
#[subject(name = "rooms.{room_id}.events", version = 1)]
struct RoomCreated {
    room_id: u64,
}