eventful 0.2.1

A library for event sourcing in Rust
Documentation
pub mod event_sourced_system;
pub mod patterns;
pub mod read_projections;

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::fmt::{Debug, Display};

#[macro_export]
macro_rules! no_events {
    () => {{
        vec![]
    }};
}

#[macro_export]
macro_rules! events {
    ($($x:expr),+) => {{
        vec![$($x),+]
    }};
}

pub trait EntityName {
    fn entity_name(&self) -> &'static str;
}

pub trait EventName {
    fn event_name(&self) -> &'static str;
}

pub trait DefaultWithPersistenceId<PersistenceId> {
    fn default_with_persistence_id(persistence_id: &PersistenceId) -> Self;
}

pub trait NamedEntity {
    fn entity_name() -> &'static str;
}

/*
   This trait is used to indicate whether a command will fully populate the entity.
   This can be used internally by the event system as an optimisation to bypass loading the entity from the cache or persistence store.
*/
pub trait IsFullPopulationCommand {
    fn is_full_population_command(&self) -> bool;
}

pub trait EventSourcedEntity: Send + Sync + Debug + 'static
where
    Self::Command: Debug + Send + Sync + IsFullPopulationCommand,
    Self::Event: Clone + Debug + Send + Sync,
    Self::PersistenceId: Display,
{
    type Command;
    type Event;
    type PersistenceId;
    fn persistence_id(&self) -> Self::PersistenceId;
    fn handle_command(&self, command: Self::Command) -> anyhow::Result<Vec<Self::Event>>;
    fn apply_event(&mut self, event: Self::Event);
}

#[derive(Default)]
pub struct Snapshot<E> {
    pub offset: u64,
    pub value: E,
}

#[async_trait]
pub trait SnapshotStore {
    async fn read_snapshot<'de, S>(&self, name: &str) -> anyhow::Result<Option<Snapshot<S>>>
    where
        S: DeserializeOwned;

    async fn write_snapshot<S>(&self, name: &str, offset: u64, value: &S) -> anyhow::Result<()>
    where
        S: Debug + Serialize + Sync;
}

#[async_trait]
pub trait JournalStore {
    async fn get_initial_offset(&self) -> anyhow::Result<u64>;
    async fn get_events_from_journal(&self, offset: u64) -> anyhow::Result<Vec<(u64, Vec<u8>)>>;
    async fn load_entity_events(&self, entity_type_name: &str, persistence_id: &str, offset: u64) -> anyhow::Result<Vec<Vec<u8>>>;
    async fn persist_event_to_journal(&self, entity_type_name: &str, event_type: &str, event_date: &DateTime<Utc>, bytes: &[u8], persistence_id: &str, offset: u64) -> anyhow::Result<()>;
}

#[cfg(test)]
mod tests {}