use async_trait::async_trait;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ChannelKind {
WhatsApp,
Email,
Sms,
Push,
}
impl std::fmt::Display for ChannelKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ChannelKind::WhatsApp => write!(f, "whatsapp"),
ChannelKind::Email => write!(f, "email"),
ChannelKind::Sms => write!(f, "sms"),
ChannelKind::Push => write!(f, "push"),
}
}
}
#[derive(Debug)]
pub enum ChannelError {
InvalidRecipient(String),
TemplateNotFound(String),
ProviderError(String),
RateLimited {
retry_after_secs: u64,
},
}
impl std::fmt::Display for ChannelError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ChannelError::InvalidRecipient(r) => write!(f, "Invalid recipient: {}", r),
ChannelError::TemplateNotFound(t) => write!(f, "Template not found: {}", t),
ChannelError::ProviderError(e) => write!(f, "Provider error: {}", e),
ChannelError::RateLimited { retry_after_secs } => {
write!(f, "Rate limited, retry after {}s", retry_after_secs)
}
}
}
}
impl std::error::Error for ChannelError {}
#[allow(clippy::double_must_use)]
#[async_trait]
pub trait NotifyChannel: Send + Sync {
async fn send(
&self,
recipient: &str,
template: &str,
params: &serde_json::Value,
) -> Result<(), ChannelError>;
fn kind(&self) -> ChannelKind;
}