pub trait Mailable: Send + Sync {
fn subject(&self) -> &str;
fn body(&self) -> String;
fn html_body(&self) -> Option<String> {
None
}
}
#[cfg(feature = "markdown")]
pub struct MarkdownMailable {
subject: String,
markdown: String,
}
#[cfg(feature = "markdown")]
impl MarkdownMailable {
pub fn new(subject: impl Into<String>, markdown: impl Into<String>) -> Self {
Self {
subject: subject.into(),
markdown: markdown.into(),
}
}
pub fn subject(mut self, subject: impl Into<String>) -> Self {
self.subject = subject.into();
self
}
pub fn body(mut self, markdown: impl Into<String>) -> Self {
self.markdown = markdown.into();
self
}
}
#[cfg(feature = "markdown")]
impl Mailable for MarkdownMailable {
fn subject(&self) -> &str {
&self.subject
}
fn body(&self) -> String {
self.markdown.clone()
}
fn html_body(&self) -> Option<String> {
let parser = pulldown_cmark::Parser::new(&self.markdown);
let mut html = String::new();
pulldown_cmark::html::push_html(&mut html, parser);
Some(html)
}
}