use std::fmt;
use std::sync::Arc;
use id_effect::{Effect, HCons, HList, HNil, Layer, Selector, hlist, need};
use crate::Message;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Sent {
pub id: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum MailError {
InvalidMessage(String),
Config(String),
Transport(String),
Unauthorized(String),
Rejected {
code: u16,
message: String,
},
Temporary {
code: u16,
message: String,
},
}
impl MailError {
pub fn is_transient(&self) -> bool {
matches!(self, MailError::Temporary { .. } | MailError::Transport(_))
}
}
impl fmt::Display for MailError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MailError::InvalidMessage(m) => write!(f, "invalid mail message: {m}"),
MailError::Config(m) => write!(f, "mail transport configuration: {m}"),
MailError::Transport(m) => write!(f, "mail transport failed: {m}"),
MailError::Unauthorized(m) => write!(f, "mail credentials refused: {m}"),
MailError::Rejected { code, message } => write!(f, "mail rejected ({code}): {message}"),
MailError::Temporary { code, message } => {
write!(f, "mail temporarily refused ({code}): {message}")
}
}
}
}
impl std::error::Error for MailError {}
pub trait Mailer: Send + Sync + 'static {
fn send(&self, message: Message) -> Effect<Sent, MailError>;
}
#[derive(Clone)]
pub struct MailerService(pub Arc<dyn Mailer>);
impl MailerService {
pub fn new(mailer: impl Mailer) -> Self {
Self(Arc::new(mailer))
}
}
impl std::ops::Deref for MailerService {
type Target = dyn Mailer;
fn deref(&self) -> &Self::Target {
&*self.0
}
}
impl fmt::Debug for MailerService {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("MailerService")
.field(&"<dyn Mailer>")
.finish()
}
}
pub type MailerEnv = HCons<MailerService, HNil>;
pub fn mailer_env(mailer: impl Mailer) -> MailerEnv {
hlist![MailerService::new(mailer)]
}
pub fn provide_mailer<E: Send + 'static>(mailer: impl Mailer) -> Layer<MailerEnv, E> {
Layer::succeed(mailer_env(mailer))
}
pub fn deliver<R, I>(message: Message) -> Effect<Sent, MailError, R>
where
R: Selector<MailerService, I> + HList,
{
need::<MailerService>().flat_map(move |mailer| mailer.send(message).narrow())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn transient_errors_are_temporary_and_transport_failures() {
let temporary = MailError::Temporary {
code: 451,
message: "try later".into(),
};
assert!(temporary.is_transient());
assert!(MailError::Transport("reset".into()).is_transient());
let rejected = MailError::Rejected {
code: 550,
message: "no such user".into(),
};
assert!(!rejected.is_transient());
assert!(!MailError::Unauthorized("bad key".into()).is_transient());
assert!(!MailError::InvalidMessage("no body".into()).is_transient());
}
#[test]
fn errors_display_their_code_and_reason() {
let e = MailError::Rejected {
code: 550,
message: "no such user".into(),
};
assert_eq!(e.to_string(), "mail rejected (550): no such user");
assert!(
MailError::Config("empty host".into())
.to_string()
.contains("empty host")
);
}
}