mod simple;
use crate::eml::{Body, Email, Header};
pub use simple::{SimpleFormatter, SimpleHtmlFormatter};
pub trait Formatter {
fn format(&self, email: &Email) -> String {
let header = self.format_header(&email.header);
let body = self.format_bodies(&email.body, email);
format!("{}\n\n{}", header, body)
}
fn format_header(&self, header: &Header) -> String;
fn format_bodies(&self, bodies: &[Body], email: &Email) -> String {
bodies
.iter()
.filter(|body| self.is_supported_content(body))
.map(|body| self.format_body(body, email))
.collect::<Vec<_>>()
.join("\n")
}
fn format_body(&self, body: &Body, email: &Email) -> String;
fn is_supported_content(&self, body: &Body) -> bool;
}
pub fn create_formatter(name: &str) -> Box<dyn Formatter> {
match name {
"html" => Box::new(SimpleHtmlFormatter),
_ => Box::new(SimpleFormatter),
}
}
pub fn format_markdown(email: &Email, formatter_name: &str) -> String {
let formatter = create_formatter(formatter_name);
formatter.format(email)
}