-
Notifications
You must be signed in to change notification settings - Fork 116
/
lib.rs
137 lines (115 loc) · 3.44 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
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
pub trait EventSourced<Ev: ?Sized> {
fn apply(&mut self, event: &Ev);
}
pub mod user {
use std::time::SystemTime;
use super::{event, EventSourced};
#[derive(Debug)]
pub struct User {
pub id: Id,
pub name: Option<Name>,
pub online_since: Option<SystemTime>,
pub created_at: CreationDateTime,
pub last_activity_at: LastActivityDateTime,
pub deleted_at: Option<DeletionDateTime>,
}
impl EventSourced<event::UserCreated> for User {
fn apply(&mut self, ev: &event::UserCreated) {
self.id = ev.user_id;
self.created_at = ev.at;
self.last_activity_at = LastActivityDateTime(ev.at.0);
}
}
impl EventSourced<event::UserNameUpdated> for User {
fn apply(&mut self, ev: &event::UserNameUpdated) {
self.name = ev.name.clone();
}
}
impl EventSourced<event::UserBecameOnline> for User {
fn apply(&mut self, ev: &event::UserBecameOnline) {
self.online_since = Some(ev.at);
}
}
impl EventSourced<event::UserBecameOffline> for User {
fn apply(&mut self, ev: &event::UserBecameOffline) {
self.online_since = None;
self.last_activity_at = LastActivityDateTime(ev.at);
}
}
impl EventSourced<event::UserDeleted> for User {
fn apply(&mut self, ev: &event::UserDeleted) {
self.deleted_at = Some(ev.at);
self.last_activity_at = LastActivityDateTime(ev.at.0);
}
}
#[derive(Debug)]
pub enum Event {
Created(event::UserCreated),
NameUpdated(event::UserNameUpdated),
Online(event::UserBecameOnline),
Offline(event::UserBecameOffline),
Deleted(event::UserDeleted),
}
impl EventSourced<Event> for User {
fn apply(&mut self, ev: &Event) {
// Creation
if let Event::Created(ev) = ev {
self.apply(ev);
return;
}
// Online/Offline
if let Event::Online(ev) = ev {
self.apply(ev);
return;
}
if let Event::Offline(ev) = ev {
self.apply(ev);
return;
}
// Deletion
if let Event::Deleted(ev) = ev {
self.apply(ev);
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct Id(pub u64);
#[derive(Clone, Debug)]
pub struct Name(pub Box<str>);
#[derive(Clone, Copy, Debug)]
pub struct CreationDateTime(pub SystemTime);
#[derive(Clone, Copy, Debug)]
pub struct LastActivityDateTime(pub SystemTime);
#[derive(Clone, Copy, Debug)]
pub struct DeletionDateTime(pub SystemTime);
}
pub mod event {
use std::time::SystemTime;
use super::user;
#[derive(Debug)]
pub struct UserCreated {
pub user_id: user::Id,
pub at: user::CreationDateTime,
}
#[derive(Debug)]
pub struct UserNameUpdated {
pub user_id: user::Id,
pub name: Option<user::Name>,
pub at: SystemTime,
}
#[derive(Debug)]
pub struct UserBecameOnline {
pub user_id: user::Id,
pub at: SystemTime,
}
#[derive(Debug)]
pub struct UserBecameOffline {
pub user_id: user::Id,
pub at: SystemTime,
}
#[derive(Debug)]
pub struct UserDeleted {
pub user_id: user::Id,
pub at: user::DeletionDateTime,
}
}