use std::collections::BTreeMap;
use crate::template::{Content, DocumentFormat, DocumentSettings, MailSettings, Margins, Orientation, Settings};
#[derive(Debug, Clone, PartialEq)]
pub struct DefaultTemplate {
pub name: String,
pub content: BTreeMap<String, Content>,
pub settings: Settings,
pub attach_documents: Vec<String>,
}
impl DefaultTemplate {
#[must_use]
pub fn mail(name: impl Into<String>) -> Self {
Self::new(name, Settings::Mail(MailSettings::default()))
}
#[must_use]
pub fn document(name: impl Into<String>) -> Self {
Self::new(name, Settings::Document(DocumentSettings::default()))
}
fn new(name: impl Into<String>, settings: Settings) -> Self {
Self {
name: name.into(),
content: BTreeMap::new(),
settings,
attach_documents: Vec::new(),
}
}
#[must_use]
pub fn content(mut self, locale: impl Into<String>, subject: impl Into<String>, body: impl Into<String>) -> Self {
self.content.insert(locale.into(), Content::new(subject, body));
self
}
#[must_use]
pub fn to<S: Into<String>>(self, to: impl IntoIterator<Item = S>) -> Self {
self.mail_settings(|m| m.to = to.into_iter().map(Into::into).collect())
}
#[must_use]
pub fn cc<S: Into<String>>(self, cc: impl IntoIterator<Item = S>) -> Self {
self.mail_settings(|m| m.cc = cc.into_iter().map(Into::into).collect())
}
#[must_use]
pub fn bcc<S: Into<String>>(self, bcc: impl IntoIterator<Item = S>) -> Self {
self.mail_settings(|m| m.bcc = bcc.into_iter().map(Into::into).collect())
}
#[must_use]
pub fn from(self, from: impl Into<String>) -> Self {
let from = from.into();
self.mail_settings(|m| m.from = Some(from))
}
#[must_use]
pub fn attach_document(mut self, name: impl Into<String>) -> Self {
self.attach_documents.push(name.into());
self
}
#[must_use]
pub fn filename(self, filename: impl Into<String>) -> Self {
let filename = filename.into();
self.document_settings(|d| d.filename = filename)
}
#[must_use]
pub fn html(self) -> Self {
self.document_settings(|d| d.format = DocumentFormat::Html)
}
#[must_use]
pub fn paper(self, paper: impl Into<String>) -> Self {
let paper = paper.into();
self.document_settings(|d| d.paper = paper)
}
#[must_use]
pub fn landscape(self) -> Self {
self.document_settings(|d| d.orientation = Orientation::Landscape)
}
#[must_use]
pub fn layout(mut self, layout: impl Into<String>) -> Self {
let layout = Some(layout.into());
match &mut self.settings {
Settings::Mail(settings) => settings.layout = layout,
Settings::Document(settings) => settings.layout = layout,
}
self
}
#[must_use]
pub fn margins(self, margins: Margins) -> Self {
self.document_settings(|d| d.margins = margins)
}
#[must_use]
pub fn font(self, family: impl Into<String>, size: f32) -> Self {
let family = family.into();
self.document_settings(|d| {
d.font_family = Some(family);
d.font_size = Some(size);
})
}
#[must_use]
pub fn line_height(self, height: f32) -> Self {
self.document_settings(|d| d.line_height = Some(height))
}
fn mail_settings(mut self, change: impl FnOnce(&mut MailSettings)) -> Self {
if let Settings::Mail(settings) = &mut self.settings {
change(settings);
}
self
}
fn document_settings(mut self, change: impl FnOnce(&mut DocumentSettings)) -> Self {
if let Settings::Document(settings) = &mut self.settings {
change(settings);
}
self
}
}