horfimbor_eventsource/
lib.rs1#![deny(missing_docs)]
2#![doc = include_str!("../README.md")]
3
4use std::error::Error;
5use std::fmt::{Debug, Display, Formatter};
6
7pub use horfimbor_eventsource_derive;
9use kurrentdb::Error as EventStoreError;
10use serde::Serialize;
11use serde::de::DeserializeOwned;
12use serde_json::Error as SerdeError;
13use thiserror::Error;
14use uuid::{Error as UuidError, Uuid};
15
16use crate::cache_db::DbError;
17use crate::model_key::ModelKey;
18
19pub mod cache_db;
20pub mod helper;
21pub mod metadata;
22pub mod model_key;
23pub mod repository;
24
25pub type StreamName = &'static str;
27
28pub enum Stream {
30 Model(ModelKey),
32 Stream(StreamName),
34 Event(EventName),
36 Correlation(Uuid),
38}
39
40impl Display for Stream {
41 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
42 match self {
43 Self::Model(m) => {
44 write!(f, "{}", m.format())
45 }
46 Self::Stream(stream_name) => {
47 let n = stream_name.replace('-', "_");
48 write!(f, "$ce-{n}")
49 }
50 Self::Event(e) => {
51 write!(f, "$et-{e}")
52 }
53 Self::Correlation(u) => {
54 write!(f, "bc-{u}")
55 }
56 }
57 }
58}
59
60#[derive(Error, Debug)]
62pub enum EventSourceError {
63 #[error("Cache error")]
65 CacheDbError(#[from] DbError),
66
67 #[error("Event store error")]
69 EventStore(#[from] EventStoreError),
70
71 #[error("Event store position error : {0}")]
74 Position(String),
75
76 #[error("Serde error")]
78 Serde(#[from] SerdeError),
79
80 #[error("Uuid error")]
82 Uuid(#[from] UuidError),
83}
84
85#[derive(Error, Debug)]
87pub enum EventSourceStateError {
88 #[error("Event source error")]
90 EventSourceError(#[from] EventSourceError),
91
92 #[error("State error : {0}")]
94 State(String),
95}
96
97pub type CommandName = &'static str;
99pub type EventName = &'static str;
101pub type StateName = &'static str;
103
104pub trait Command: Serialize + DeserializeOwned + Debug + Send + Clone {
106 fn command_name(&self) -> CommandName;
108}
109
110pub trait Event: Serialize + DeserializeOwned + Debug + Send + Clone {
112 fn event_name(&self) -> EventName;
114}
115
116pub trait Dto: Default + Serialize + DeserializeOwned + Debug + Send + Clone + Sync {
118 type Event: Event + Sync + Send;
120
121 fn play_event(&mut self, event: &Self::Event);
123}
124
125pub trait StateNamed {
127 fn state_name() -> StateName;
129}
130
131pub trait State: Dto + StateNamed {
133 type Command: Command + Sync + Send;
135
136 type Error: Error + Sync + Send;
138
139 fn try_command(&self, command: Self::Command) -> Result<Vec<Self::Event>, Self::Error>;
145}