use thiserror::Error;
pub type Result<T> = std::result::Result<T, MailError>;
#[derive(Debug, Error)]
pub enum MailError {
#[error("SMTP error: {0}")]
Smtp(String),
#[error("Invalid email address: {0}")]
InvalidAddress(String),
#[error("Missing required field: {0}")]
MissingField(&'static str),
#[error("Template error: {0}")]
Template(String),
#[error("Template not found: {0}")]
TemplateNotFound(String),
#[error("Attachment error: {0}")]
Attachment(String),
#[error("Configuration error: {0}")]
Config(String),
#[error("Provider error: {0}")]
Provider(String),
#[error("Authentication failed: {0}")]
Auth(String),
#[error("Rate limited, retry after {0} seconds")]
RateLimited(u64),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("Serialization error: {0}")]
Serialization(String),
#[error("Network error: {0}")]
Network(String),
#[error("Operation timed out")]
Timeout,
#[error("Queue error: {0}")]
Queue(String),
}
impl MailError {
pub fn is_retryable(&self) -> bool {
matches!(
self,
Self::Smtp(_) | Self::Network(_) | Self::Timeout | Self::RateLimited(_)
)
}
pub fn retry_after(&self) -> Option<std::time::Duration> {
if let Self::RateLimited(secs) = self {
Some(std::time::Duration::from_secs(*secs))
} else {
None
}
}
}
impl From<lettre::transport::smtp::Error> for MailError {
fn from(err: lettre::transport::smtp::Error) -> Self {
Self::Smtp(err.to_string())
}
}
impl From<lettre::address::AddressError> for MailError {
fn from(err: lettre::address::AddressError) -> Self {
Self::InvalidAddress(err.to_string())
}
}
impl From<lettre::error::Error> for MailError {
fn from(err: lettre::error::Error) -> Self {
Self::Smtp(err.to_string())
}
}
impl From<serde_json::Error> for MailError {
fn from(err: serde_json::Error) -> Self {
Self::Serialization(err.to_string())
}
}
#[cfg(feature = "handlebars")]
impl From<handlebars::RenderError> for MailError {
fn from(err: handlebars::RenderError) -> Self {
Self::Template(err.to_string())
}
}
#[cfg(feature = "handlebars")]
impl From<handlebars::TemplateError> for MailError {
fn from(err: handlebars::TemplateError) -> Self {
Self::Template(err.to_string())
}
}
#[cfg(feature = "tera")]
impl From<tera::Error> for MailError {
fn from(err: tera::Error) -> Self {
Self::Template(err.to_string())
}
}