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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use crate::types::CqrsError;
pub trait Aggregate: Default {
fn aggregate_type() -> &'static str;
fn apply<E>(&mut self, event: E)
where
E: AggregateEvent<Self>,
{
event.apply_to(self);
}
fn execute<C>(&self, command: C) -> Result<C::Events, C::Error>
where
C: AggregateCommand<Self>,
{
command.execute_on(self)
}
}
pub trait AggregateId<A>
where
A: Aggregate,
{
fn as_str(&self) -> &str;
}
pub trait AggregateCommand<A: Aggregate> {
type Event: AggregateEvent<A>;
type Events: Events<ProducedEvent<A, Self>>;
type Error: CqrsError;
fn execute_on(self, aggregate: &A) -> Result<Self::Events, Self::Error>;
}
pub type ProducedEvent<A, C> = <C as AggregateCommand<A>>::Event;
pub type ProducedEvents<A, C> = <C as AggregateCommand<A>>::Events;
pub type CommandError<A, C> = <C as AggregateCommand<A>>::Error;
pub trait Event {
fn event_type(&self) -> &'static str;
}
pub trait AggregateEvent<A: Aggregate>: Event {
fn apply_to(self, aggregate: &mut A);
}
pub trait Events<E>: IntoIterator<Item = E> + AsRef<[E]>
where
E: Event,
{
}
impl<T, E> Events<E> for T
where
T: IntoIterator<Item = E> + AsRef<[E]>,
E: Event,
{
}
pub trait SerializableEvent: Event {
type Error: CqrsError;
fn serialize_event_to_buffer(&self, buffer: &mut Vec<u8>) -> Result<(), Self::Error>;
}
pub trait DeserializableEvent: Event + Sized {
type Error: CqrsError;
fn deserialize_event_from_buffer(
data: &[u8],
event_type: &str,
) -> Result<Option<Self>, Self::Error>;
}