use async_trait::async_trait;
use uuid::Uuid;
use super::event_error::EventError;
#[derive(Debug, Clone)]
pub struct RenderContext {
pub event_id: Uuid,
pub event_name: String,
pub event_date_begin: chrono::DateTime<chrono::Utc>,
pub event_date_end: chrono::DateTime<chrono::Utc>,
pub event_date_tz: String,
pub registration_id: Uuid,
pub attendee_name: String,
pub attendee_email: String,
pub attendee_phone: Option<String>,
pub registration_barcode: String,
}
#[derive(Debug, Clone)]
pub struct RenderedMail {
pub subject: String,
pub body_html: String,
pub body_text: String,
}
#[derive(Debug, Clone)]
pub enum RenderFailure {
TemplateUnresolved,
RendererNotComposed,
RenderFailed(String),
}
#[async_trait]
pub trait EventTemplateRenderer: Send + Sync {
async fn render(
&self,
template_ref: Option<Uuid>,
template_kind: Option<&str>,
ctx: &RenderContext,
) -> Result<RenderedMail, RenderFailure>;
}
pub struct RefusingTemplateRenderer;
#[async_trait]
impl EventTemplateRenderer for RefusingTemplateRenderer {
async fn render(
&self,
template_ref: Option<Uuid>,
_template_kind: Option<&str>,
_ctx: &RenderContext,
) -> Result<RenderedMail, RenderFailure> {
if template_ref.is_none() {
return Err(RenderFailure::TemplateUnresolved);
}
Err(RenderFailure::RendererNotComposed)
}
}
impl RenderFailure {
pub fn into_error(self) -> EventError {
match self {
Self::TemplateUnresolved => EventError::TemplateUnresolved,
Self::RendererNotComposed => EventError::TemplateRendererNotComposed,
Self::RenderFailed(d) => EventError::RenderFailed(d),
}
}
}
#[async_trait]
pub trait EventMailQueue: Send + Sync {
async fn enqueue(&self, recipient: &str, mail: &RenderedMail) -> Result<(), EventError>;
}
pub struct RefusingMailQueue;
#[async_trait]
impl EventMailQueue for RefusingMailQueue {
async fn enqueue(&self, _recipient: &str, _mail: &RenderedMail) -> Result<(), EventError> {
Err(EventError::EnqueueRefused(
"no host mail queue composed for the event module".to_string(),
))
}
}