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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//! [Thalo] is an event sourcing runtime that leverages the power of WebAssembly (wasm) through [wasmtime], combined with [sled] as an embedded event store.
//! It is designed to handle commands using compiled aggregate wasm components and to persist the resulting events, efficiently managing the rebuilding of aggregate states from previous events.
//!
//! [thalo]: https://github.com/thalo-rs/thalo
//! [wasmtime]: https://wasmtime.dev/
//! [sled]: https://sled.rs/
//!
//! # Counter Aggregate Example
//!
//! This example shows a basic counter aggregate, allowing the count to be incremented by an amount.
//!
//! ```
//! # use std::convert::Infallible;
//! #
//! use serde::{Deserialize, Serialize};
//! use thalo::{events, export_aggregate, Aggregate, Apply, Command, Event, Handle};
//!
//! export_aggregate!(Counter);
//!
//! pub struct Counter {
//! count: u64,
//! }
//!
//! impl Aggregate for Counter {
//! type Command = CounterCommand;
//! type Event = CounterEvent;
//!
//! fn init(_id: String) -> Self {
//! Counter { count: 0 }
//! }
//! }
//!
//! #[derive(Command, Deserialize)]
//! pub enum CounterCommand {
//! Increment { amount: u64 },
//! }
//!
//! impl Handle<CounterCommand> for Counter {
//! type Error = Infallible;
//!
//! fn handle(&self, cmd: CounterCommand) -> Result<Vec<CounterEvent>, Self::Error> {
//! match cmd {
//! CounterCommand::Increment { amount } => events![Incremented { amount }],
//! }
//! }
//! }
//!
//! #[derive(Event, Serialize, Deserialize)]
//! pub enum CounterEvent {
//! Incremented(Incremented),
//! }
//!
//! #[derive(Serialize, Deserialize)]
//! pub struct Incremented {
//! pub amount: u64,
//! }
//!
//! impl Apply<Incremented> for Counter {
//! fn apply(&mut self, event: Incremented) {
//! self.count += event.amount;
//! }
//! }
//! ```
use fmt;
pub use *;
/// Represents an aggregate root in an event-sourced system.
///
/// An aggregate root is the entry-point for the cluster of entities and that are changed
/// together in response to commands.
///
/// *See the [crate level docs](crate#counter-aggregate-example) for an example aggregate.*
/// Handles a command, returning events.
///
/// Commands use the aggregates state to validate business rules, and returns
/// events which are later used to update the aggregate state.
///
/// # Example
///
/// ```
/// use thalo::{events, Handle};
///
/// pub struct Increment {
/// pub amount: u64,
/// }
///
/// impl Handle<Increment> for Counter {
/// type Error = &'static str;
///
/// fn handle(&self, cmd: Increment) -> Result<Vec<CounterEvent>, Self::Error> {
/// if self.count + cmd.amount > 100_000 {
/// return Err("count would be too high");
/// }
///
/// events![ Incremented { amount: cmd.amount } ]
/// }
/// }
/// ```
/// Applies an event, updating the aggregate state.
///
/// Events modify aggregate state, and are emitted as the result of commands.
///
/// # Example
///
/// ```
/// use thalo::Apply;
///
/// pub struct Increment {
/// pub amount: u64,
/// }
///
/// impl Apply<Incremented> for Counter {
/// fn apply(&mut self, event: Incremented) {
/// self.count += self.amount;
/// }
/// }
/// ```
;