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
#[macro_use]
extern crate log;
use riker::actors::ChannelRef;
use std::fmt;
use uuid::Uuid;
pub use entity::{Entity, EntityName, Model, Query, Result, CQRS, ES};
pub use entity_manager::Manager;
pub use riker_es_macros as macros;
pub use store::*;
pub type EventBus<T> = ChannelRef<Event<T>>;
mod entity;
mod entity_manager;
mod store;
#[derive(Clone, Debug)]
pub enum Event<T: Model> {
Create(T),
Change(EntityId, T::Change),
}
impl<T: Model> Event<T> {
pub fn entity_id(&self) -> EntityId {
match self {
Event::Create(e) => e.id(),
Event::Change(id, _) => *id,
}
}
pub fn entity(&self) -> Option<T> {
match self {
Event::Create(e) => Some(e.clone()),
Event::Change(_, _) => None,
}
}
pub fn change(&self) -> Option<T::Change> {
match self {
Event::Create(_) => None,
Event::Change(_, c) => Some(c.clone()),
}
}
}
impl<T: Model> From<(EntityId, T::Change)> for Event<T> {
fn from((id, data): (EntityId, T::Change)) -> Self {
Event::Change(id, data)
}
}
impl<T: Model> 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<String> for EntityId {
fn from(id: String) -> Self {
id.as_str().into()
}
}
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()
}
}