13
crates/crunch-traits/Cargo.toml
Normal file
13
crates/crunch-traits/Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "crunch-traits"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
tokio.workspace = true
|
||||
thiserror.workspace = true
|
||||
async-trait.workspace = true
|
||||
uuid.workspace = true
|
43
crates/crunch-traits/src/errors.rs
Normal file
43
crates/crunch-traits/src/errors.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum SerializeError {
|
||||
#[error("failed to serialize {0}")]
|
||||
FailedToSerialize(anyhow::Error),
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum DeserializeError {
|
||||
#[error("failed to serialize {0}")]
|
||||
FailedToDeserialize(anyhow::Error),
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum PublishError {
|
||||
#[error("failed to serialize {0}")]
|
||||
SerializeError(#[source] SerializeError),
|
||||
|
||||
#[error("failed to commit to database {0}")]
|
||||
DbError(#[source] anyhow::Error),
|
||||
|
||||
#[error("transaction failed {0}")]
|
||||
DbTxError(#[source] anyhow::Error),
|
||||
|
||||
#[error("failed to connect to database {0}")]
|
||||
ConnectionError(#[source] anyhow::Error),
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum TransportError {
|
||||
#[error("to publish to transport {0}")]
|
||||
Err(anyhow::Error),
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum PersistenceError {
|
||||
#[error("failed to get item {0}")]
|
||||
GetErr(anyhow::Error),
|
||||
|
||||
#[error("failed to publish item {0}")]
|
||||
UpdatePublished(anyhow::Error),
|
||||
}
|
46
crates/crunch-traits/src/lib.rs
Normal file
46
crates/crunch-traits/src/lib.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
use std::{fmt::Display, sync::Arc};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use errors::{DeserializeError, PersistenceError, SerializeError, TransportError};
|
||||
|
||||
#[async_trait]
|
||||
pub trait Persistence {
|
||||
async fn insert(&self, event_info: &EventInfo, content: Vec<u8>) -> anyhow::Result<()>;
|
||||
async fn next(&self) -> Option<String>;
|
||||
async fn get(&self, event_id: &str) -> Result<Option<(EventInfo, Vec<u8>)>, PersistenceError>;
|
||||
async fn update_published(&self, event_id: &str) -> Result<(), PersistenceError>;
|
||||
}
|
||||
|
||||
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,
|
||||
pub event_name: &'static str,
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
pub mod errors;
|
||||
mod transport;
|
||||
pub use transport::*;
|
12
crates/crunch-traits/src/transport.rs
Normal file
12
crates/crunch-traits/src/transport.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use async_trait::async_trait;
|
||||
|
||||
use crate::{errors::TransportError, EventInfo};
|
||||
|
||||
#[async_trait]
|
||||
pub trait Transport {
|
||||
async fn publish(&self, event_info: &EventInfo, content: Vec<u8>)
|
||||
-> Result<(), TransportError>;
|
||||
}
|
||||
pub type DynTransport = Arc<dyn Transport + Send + Sync + 'static>;
|
Reference in New Issue
Block a user