intrepid-model 0.3.0

Manage complex async business logic with ease
Documentation
use super::{EventKind, EventLog, EventRecord, IntoEvent};

/// Errors that can occur when interacting with an event repository.
#[derive(Debug, thiserror::Error)]
pub enum EventRepoError {
    /// An error occurred while trying to publish an event record.
    #[error("failed to publish event")]
    PublishFailed,
}

/// The trait for an event repository. Implement this trait to provide an event
/// repository interface for any given event storage implementation.
#[async_trait::async_trait]
pub trait EventRepo {
    /// Get all entries in the stream since the given position.
    async fn entries_since_position(
        &self,
        stream_name: impl AsRef<str> + Send,
        position: i64,
    ) -> Result<EventLog, EventRepoError>;

    /// Publish an event to the target stream.
    async fn publish<EventCandidate>(&self, event: EventCandidate) -> Result<(), EventRepoError>
    where
        EventCandidate: IntoEvent + Send;

    /// Get the last event in the stream.
    async fn last(
        &self,
        stream_name: impl AsRef<str> + Send,
        kind: EventKind,
    ) -> Option<EventRecord>;
}