Skip to main content

serenade_event/
subscriber.rs

1//! Event and subscriber contracts.
2
3use crate::EventError;
4
5/// Synchronous application or infrastructure event.
6pub trait Event: Send + Sync {
7    /// Stable event name used for subscriber matching (for example `cart.updated`).
8    fn name(&self) -> &'static str;
9}
10
11/// Handles events of one name. Higher [`Self::priority`] runs first.
12pub trait EventSubscriber: Send + Sync {
13    /// Event name this subscriber listens to.
14    fn event_name(&self) -> &'static str;
15
16    /// Dispatch order. Higher values run first. Default is `0`.
17    fn priority(&self) -> i32 {
18        0
19    }
20
21    /// Handles a matching event.
22    ///
23    /// # Errors
24    ///
25    /// Return [`EventError`] when handling fails. Remaining subscribers still run.
26    fn handle(&self, event: &dyn Event) -> Result<(), EventError>;
27}