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
#[macro_use]
extern crate log;
pub use entity::{Aggregate, Entity, Query, StoreRef, Sys, CQRS, ES};
use riker::actors::ChannelRef;
pub use riker_patterns::ask::ask;
use std::fmt;
pub use store::{Commit, Store, StoreMsg};
use uuid::Uuid;
pub type EventBus<T> = ChannelRef<Event<T>>;
mod entity;
mod store;
#[derive(Clone, Debug)]
pub enum Event<T: Aggregate> {
Create(T),
Update(EntityId, T::Update),
}
impl<T: Aggregate> Event<T> {
pub fn entity_id(&self) -> EntityId {
match self {
Event::Create(e) => e.id(),
Event::Update(id, _) => *id,
}
}
}
impl<T: Aggregate> From<(EntityId, T::Update)> for Event<T> {
fn from((id, data): (EntityId, T::Update)) -> Self {
Event::Update(id, data)
}
}
impl<T: Aggregate> From<T> for Event<T> {
fn from(data: T) -> Self {
Event::Create(data)
}
}
#[derive(Clone, Debug, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct EntityId(Uuid);
impl EntityId {
pub fn new() -> Self {
Default::default()
}
}
impl From<&str> for EntityId {
fn from(id: &str) -> Self {
EntityId(Uuid::new_v5(&Uuid::NAMESPACE_URL, id.as_bytes()))
}
}
impl From<Uuid> for EntityId {
fn from(uuid: Uuid) -> Self {
EntityId(uuid)
}
}
impl fmt::Display for EntityId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl Default for EntityId {
fn default() -> Self {
Uuid::new_v4().into()
}
}