use crate::{domain_id::DomainIdSet, Identifier, IdentifierType};
use std::ops::Deref;
pub trait EventId:
Default + Copy + Clone + PartialEq + Eq + Ord + PartialOrd + Send + Sync + 'static
{
}
impl<Id> EventId for Id where
Id: Default + Copy + Clone + PartialEq + Eq + Ord + PartialOrd + Send + Sync + 'static
{
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct EventInfo {
pub name: &'static str,
pub domain_ids: &'static [&'static Identifier],
}
impl EventInfo {
pub fn has_domain_id(&self, ident: &Identifier) -> bool {
self.domain_ids.contains(&ident)
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct DomainIdInfo {
pub ident: Identifier,
pub type_info: IdentifierType,
}
#[derive(Debug, Clone)]
pub struct EventSchema {
pub events: &'static [&'static str],
pub events_info: &'static [&'static EventInfo],
pub domain_ids: &'static [&'static DomainIdInfo],
}
impl EventSchema {
pub fn event_info(&self, name: &str) -> Option<&EventInfo> {
self.events_info
.iter()
.find(|info| info.name == name)
.copied()
}
}
pub trait Event {
const SCHEMA: EventSchema;
fn domain_ids(&self) -> DomainIdSet;
fn name(&self) -> &'static str;
}
#[derive(Debug, Clone)]
pub struct PersistedEvent<ID: EventId, E: Event> {
pub(crate) id: ID,
pub(crate) event: E,
}
impl<ID: EventId, E: Event> PersistedEvent<ID, E> {
pub fn new(id: ID, event: E) -> Self {
Self { id, event }
}
pub fn into_inner(self) -> E {
self.event
}
pub fn id(&self) -> ID {
self.id
}
}
impl<ID: EventId, E: Event> Deref for PersistedEvent<ID, E> {
type Target = E;
fn deref(&self) -> &Self::Target {
&self.event
}
}