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
use crateEvent;
use Default;
use ;
/// The `State` trait is used to represent the aggregated state of a domain
/// entity after a sequence of events has been applied to it.
///
/// Each `State` will usually have its own `Aggregate` that manages the events
/// that are applied to it.
///
/// It requires a number of other traits to be implemented, notably `Serialize`
/// and `DeserializeOwned` to allow optimized reading from a `Store`, as well as
/// `Clone` to allow for introspection from outside an `Aggregate`.
///
/// Note that `State` is also explicitly marked as `Send + Sync` due to usages
/// of the `async-trait` crate on the `Store` trait.
///
/// Implementations of `State` must specify the type of `Identifier` used as
/// well as the type of `Event` that will be used to aggregate state.
///
/// An example implementation of `State` for a fictional "Order" entity is
/// provided below. This example aligns with the examples given for the other
/// core traits.
///
/// ```
/// #[derive(Clone, Default, Serialize, Deserialize)]
/// struct OrderState {
/// id: u128,
/// product_name: String,
/// balance_owing: f64
/// }
///
/// impl ljprs_es::State for OrderState {
/// type Identifier = u128;
/// type Event = Event;
///
/// fn id(&self) -> Self::Identifier {
/// self.id
/// }
///
/// fn logical_version() -> u32 {
/// 0
/// }
///
/// fn apply(&mut self, event: &Self::Event) {
/// match event {
/// Event::OrderCreated(e) => self.apply_order_created(e),
/// Event::OrderPayment(e) => self.apply_order_payment(e)
/// }
/// }
/// }
///
/// impl OrderState {
/// fn apply_order_created(&mut self, event: &OrderCreatedEvent) {
/// self.id = event.id;
/// self.product_name = event.product_name.clone();
/// self.balance_owing = event.balance_owing;
/// self.is_fully_paid = false;
/// }
///
/// fn apply_order_payment(&mut self, event: &OrderPaymentEvent) {
/// self.balance_owing -= event.amount;
/// }
/// }
/// ```