use std::{error::Error, fmt::Display, future::Future, pin::Pin};
pub type HandlerResult<T> = Result<T, HandlerError>;
pub type HandlerFuture<'a> = Pin<Box<dyn Future<Output = HandlerResult<()>> + Send + 'a>>;
#[derive(Debug)]
pub enum HandlerError {
Storage(String),
Connection(String),
Serialization(String),
}
impl Display for HandlerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
HandlerError::Storage(msg) => write!(f, "Storage error: {msg}"),
HandlerError::Connection(msg) => write!(f, "Connection error: {msg}"),
HandlerError::Serialization(msg) => write!(f, "Serialization error: {msg}"),
}
}
}
impl Error for HandlerError {}
pub trait MessageHandler: Send + Sync {
fn handle<'a>(&'a self, message: &'a crate::EmailMessage) -> HandlerFuture<'a>;
fn name(&self) -> &str;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_handler_error_display() {
assert_eq!(
HandlerError::Storage("test".to_string()).to_string(),
"Storage error: test"
);
assert_eq!(
HandlerError::Connection("test".to_string()).to_string(),
"Connection error: test"
);
assert_eq!(
HandlerError::Serialization("test".to_string()).to_string(),
"Serialization error: test"
);
}
}