1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use crate::aggregate::{AggregateIdOf, WithAggregateId};
use crate::event::{DomainEvent, EventType, Sequence};
use crate::query::ReceiveEvent;

pub trait EventSink<E, A>
where
    E: EventType,
    A: WithAggregateId,
{
    type Error: std::error::Error;

    fn append(&self, event: DomainEvent<E, A>) -> Result<(), Self::Error>;

    fn append_batch(
        &self,
        events: impl IntoIterator<Item = DomainEvent<E, A>>,
    ) -> Result<(), Self::Error>;
}

pub type EventSinkError<S, E, A> = <S as EventSink<E, A>>::Error;

pub trait EventSource<E, A>
where
    E: EventType,
    A: WithAggregateId,
{
    type Error: std::error::Error;

    fn read<R>(
        &self,
        aggregate_id: &AggregateIdOf<A>,
        subscriber: &mut R,
    ) -> Result<(), Self::Error>
    where
        R: ReceiveEvent<E, A>;

    fn read_from_offset<R>(
        &self,
        aggregate_id: &AggregateIdOf<A>,
        offset: Sequence,
        subscriber: &mut R,
    ) -> Result<(), Self::Error>
    where
        R: ReceiveEvent<E, A>;
}

pub type EventSourceError<S, E, A> = <S as EventSource<E, A>>::Error;