eventually/event/
mod.rs

1//! Module `event` contains types and abstractions helpful for working
2//! with Domain Events.
3
4pub mod store;
5use std::fmt::Debug;
6
7use futures::stream::BoxStream;
8use serde::{Deserialize, Serialize};
9
10pub use crate::event::store::Store;
11use crate::{message, version};
12
13/// An Event is a [Message][message::Message] carring the information about a Domain Event,
14/// an occurrence in the system lifetime that is relevant for the Domain
15/// that is being implemented.
16pub type Envelope<T> = message::Envelope<T>;
17
18/// An [Event] that has been persisted to the Event [Store].
19#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
20pub struct Persisted<Id, Evt>
21where
22    Evt: message::Message,
23{
24    /// The id of the Event Stream the persisted Event belongs to.
25    pub stream_id: Id,
26
27    /// The version of the Event Stream when this Event has been recorded.
28    ///
29    /// This value is used for optimistic concurrency checks, to avoid
30    /// data races in parallel command evaluations.
31    ///
32    /// Check the [Version][version::Version] type and module documentation for more info.
33    pub version: version::Version,
34
35    /// The actual Domain Event carried by this envelope.
36    pub event: Envelope<Evt>,
37}
38
39/// Specifies the slice of the Event Stream to select when calling [`Store::stream`].
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub enum VersionSelect {
42    /// Selects all [Event][Envelope]s in the Event [Stream].
43    All,
44
45    /// Selects all [Event][Envelope]s in the Event [Stream] starting from the [Event]
46    /// with the specified [Version][version::Version].
47    From(version::Version),
48}
49
50/// Stream is a stream of [Persisted] Domain Events.
51pub type Stream<'a, Id, Evt, Err> = BoxStream<'a, Result<Persisted<Id, Evt>, Err>>;