2023-09-24 21:57:24 +02:00
|
|
|
use std::fmt::Display;
|
2023-09-22 21:31:32 +02:00
|
|
|
|
|
|
|
use async_trait::async_trait;
|
2023-09-24 01:14:50 +02:00
|
|
|
use errors::{DeserializeError, PersistenceError, SerializeError};
|
2023-09-22 21:31:32 +02:00
|
|
|
|
2023-10-03 23:08:12 +02:00
|
|
|
pub trait Tx: Send + Sync {}
|
|
|
|
|
|
|
|
pub type DynTx = Box<dyn Tx>;
|
|
|
|
|
2023-09-22 21:31:32 +02:00
|
|
|
#[async_trait]
|
|
|
|
pub trait Persistence {
|
|
|
|
async fn insert(&self, event_info: &EventInfo, content: Vec<u8>) -> anyhow::Result<()>;
|
2023-10-03 23:08:12 +02:00
|
|
|
async fn next(&self) -> Result<Option<(String, DynTx)>, PersistenceError>;
|
2023-09-23 18:21:39 +02:00
|
|
|
async fn get(&self, event_id: &str) -> Result<Option<(EventInfo, Vec<u8>)>, PersistenceError>;
|
|
|
|
async fn update_published(&self, event_id: &str) -> Result<(), PersistenceError>;
|
2023-09-22 21:31:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Serializer {
|
|
|
|
fn serialize(&self) -> Result<Vec<u8>, SerializeError>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Deserializer {
|
|
|
|
fn deserialize(raw: Vec<u8>) -> Result<Self, DeserializeError>
|
|
|
|
where
|
|
|
|
Self: Sized;
|
|
|
|
}
|
|
|
|
|
2023-10-05 22:09:56 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2023-09-22 21:31:32 +02:00
|
|
|
pub struct EventInfo {
|
2023-10-05 22:09:56 +02:00
|
|
|
pub domain: String,
|
|
|
|
pub entity_type: String,
|
|
|
|
pub event_name: String,
|
2023-09-22 21:31:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for EventInfo {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.write_str(&format!(
|
|
|
|
"domain: {}, entity_type: {}",
|
|
|
|
self.domain, self.entity_type
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Event: Serializer + Deserializer {
|
2023-09-23 22:38:12 +02:00
|
|
|
fn event_info() -> EventInfo;
|
|
|
|
|
|
|
|
fn int_event_info(&self) -> EventInfo {
|
|
|
|
Self::event_info()
|
|
|
|
}
|
2023-09-22 21:31:32 +02:00
|
|
|
}
|
2023-09-23 18:21:39 +02:00
|
|
|
|
|
|
|
pub mod errors;
|
|
|
|
mod transport;
|
|
|
|
pub use transport::*;
|