use crate::{events::EntityEvents, traits::EsEvent};
mod sealed {
pub trait Sealed {}
}
pub trait IdSource: sealed::Sealed {
type Slot<Id>;
}
pub struct FromRepo;
impl sealed::Sealed for FromRepo {}
impl IdSource for FromRepo {
type Slot<Id> = Id;
}
pub struct FromCaller;
impl sealed::Sealed for FromCaller {}
impl IdSource for FromCaller {
type Slot<Id> = ();
}
pub struct EntityEventsBuilder<E: EsEvent, S: IdSource> {
id: S::Slot<E::EntityId>,
}
impl<E: EsEvent> EntityEventsBuilder<E, FromRepo> {
#[doc(hidden)]
pub fn __from_repo(id: E::EntityId) -> Self {
Self { id }
}
pub fn id(&self) -> E::EntityId {
self.id.clone()
}
pub fn initial_events(self, events: impl IntoIterator<Item = E>) -> EntityEvents<E> {
EntityEvents::init(self.id, events)
}
}
impl<E: EsEvent> EntityEventsBuilder<E, FromCaller> {
#[doc(hidden)]
pub fn __from_caller() -> Self {
Self { id: () }
}
pub fn initial_events(
self,
id: E::EntityId,
events: impl IntoIterator<Item = E>,
) -> EntityEvents<E> {
EntityEvents::init(id, events)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Ensured<E> {
Created(E),
Found(E),
}
impl<E> Ensured<E> {
pub fn into_inner(self) -> E {
match self {
Self::Created(entity) | Self::Found(entity) => entity,
}
}
pub fn is_created(&self) -> bool {
matches!(self, Self::Created(_))
}
}