-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlib.rs
50 lines (40 loc) · 1.04 KB
/
lib.rs
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
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 = ();
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;
}
}