Skip to main content

intrepid_model/events/
event_repo.rs

1use super::{EventKind, EventLog, EventRecord, IntoEvent};
2
3/// Errors that can occur when interacting with an event repository.
4#[derive(Debug, thiserror::Error)]
5pub enum EventRepoError {
6    /// An error occurred while trying to publish an event record.
7    #[error("failed to publish event")]
8    PublishFailed,
9}
10
11/// The trait for an event repository. Implement this trait to provide an event
12/// repository interface for any given event storage implementation.
13#[async_trait::async_trait]
14pub trait EventRepo {
15    /// Get all entries in the stream since the given position.
16    async fn entries_since_position(
17        &self,
18        stream_name: impl AsRef<str> + Send,
19        position: i64,
20    ) -> Result<EventLog, EventRepoError>;
21
22    /// Publish an event to the target stream.
23    async fn publish<EventCandidate>(&self, event: EventCandidate) -> Result<(), EventRepoError>
24    where
25        EventCandidate: IntoEvent + Send;
26
27    /// Get the last event in the stream.
28    async fn last(
29        &self,
30        stream_name: impl AsRef<str> + Send,
31        kind: EventKind,
32    ) -> Option<EventRecord>;
33}