2023-09-23 18:21:39 +02:00
|
|
|
use std::{fmt::Display, sync::Arc};
|
2023-09-22 21:31:32 +02:00
|
|
|
|
|
|
|
use async_trait::async_trait;
|
2023-09-23 18:21:39 +02:00
|
|
|
use errors::{DeserializeError, PersistenceError, SerializeError, TransportError};
|
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-09-23 00:22:18 +02:00
|
|
|
async fn next(&self) -> Option<String>;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct EventInfo {
|
|
|
|
pub domain: &'static str,
|
|
|
|
pub entity_type: &'static str,
|
2023-09-23 18:21:39 +02:00
|
|
|
pub event_name: &'static str,
|
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 {
|
|
|
|
fn event_info(&self) -> EventInfo;
|
|
|
|
}
|
2023-09-23 18:21:39 +02:00
|
|
|
|
|
|
|
pub mod errors;
|
|
|
|
mod transport;
|
|
|
|
pub use transport::*;
|