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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use crate::{
aggregate::{Aggregate, AggregateEvent, AggregateId},
types::{
CqrsError, EventNumber, Precondition, Since, SnapshotRecommendation, Version,
VersionedAggregate, VersionedEvent,
},
};
pub trait EventSource<A, E>
where
A: Aggregate,
E: AggregateEvent<A>,
{
type Events: IntoIterator<Item = VersionedEvent<E>>;
type Error: CqrsError;
fn read_events<I>(
&self,
id: &I,
since: Since,
max_count: Option<u64>,
) -> Result<Option<Self::Events>, Self::Error>
where
I: AggregateId<A>;
}
pub trait EventSink<A, E, M>
where
A: Aggregate,
E: AggregateEvent<A>,
{
type Error: CqrsError;
fn append_events<I>(
&self,
id: &I,
events: &[E],
precondition: Option<Precondition>,
metadata: M,
) -> Result<EventNumber, Self::Error>
where
I: AggregateId<A>;
}
pub trait SnapshotSource<A>
where
A: Aggregate,
{
type Error: CqrsError;
fn get_snapshot<I>(&self, id: &I) -> Result<Option<VersionedAggregate<A>>, Self::Error>
where
I: AggregateId<A>;
}
pub trait SnapshotSink<A>
where
A: Aggregate,
{
type Error: CqrsError;
fn persist_snapshot<I>(
&self,
id: &I,
aggregate: &A,
version: Version,
last_snapshot_version: Option<Version>,
) -> Result<Version, Self::Error>
where
I: AggregateId<A>;
}
pub trait SnapshotStrategy {
fn snapshot_recommendation(
&self,
version: Version,
last_snapshot_version: Option<Version>,
) -> SnapshotRecommendation;
}
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
pub struct NeverSnapshot;
impl SnapshotStrategy for NeverSnapshot {
fn snapshot_recommendation(&self, _: Version, _: Option<Version>) -> SnapshotRecommendation {
SnapshotRecommendation::DoNotSnapshot
}
}
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
pub struct AlwaysSnapshot;
impl SnapshotStrategy for AlwaysSnapshot {
fn snapshot_recommendation(&self, _: Version, _: Option<Version>) -> SnapshotRecommendation {
SnapshotRecommendation::ShouldSnapshot
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{AggregateCommand, AggregateEvent, Event};
use void::Void;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct TestAggregate;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct TestEvent;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct TestCommand;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct TestMetadata;
impl Aggregate for TestAggregate {
fn aggregate_type() -> &'static str {
"test"
}
}
impl AggregateEvent<TestAggregate> for TestEvent {
fn apply_to(self, _aggregate: &mut TestAggregate) {}
}
impl AggregateCommand<TestAggregate> for TestCommand {
type Error = Void;
type Event = TestEvent;
type Events = Vec<TestEvent>;
fn execute_on(self, _aggregate: &TestAggregate) -> Result<Self::Events, Self::Error> {
Ok(Vec::default())
}
}
impl Event for TestEvent {
fn event_type(&self) -> &'static str {
"test"
}
}
}