use crate::email::{Action, Email, GoToAction, Greeting, Table};
#[derive(Clone, Default)]
pub struct EmailBuilder<'a> {
summary: Option<&'a str>,
greeting: Option<Greeting<'a>>,
intros: Option<Vec<&'a str>>,
highlight: Option<&'a str>,
dictionary: Option<Vec<(&'a str, &'a str)>>,
tables: Option<Vec<Table<'a>>>,
actions: Option<Vec<Action<'a>>>,
outros: Option<Vec<&'a str>>,
signature: Option<&'a str>,
go_to_action: Option<GoToAction<'a>>,
}
impl<'a> EmailBuilder<'a> {
#[must_use]
pub fn new() -> Self {
Self {
greeting: Some(Greeting::Custom("Hey")),
signature: Some("Yours truly,"),
..Default::default()
}
}
#[must_use]
pub fn summary(mut self, v: &'a str) -> Self {
self.summary = Some(v);
self
}
#[must_use]
pub fn greeting(mut self, v: Greeting<'a>) -> Self {
self.greeting = Some(v);
self
}
#[must_use]
pub fn intro(mut self, v: &'a str) -> Self {
match &mut self.intros {
Some(intros) => intros.push(v),
None => self.intros = Some(vec![v]),
}
self
}
#[must_use]
pub fn set_intros(mut self, intros: Vec<&'a str>) -> Self {
self.intros = Some(intros);
self
}
#[must_use]
pub fn highlight(mut self, v: &'a str) -> Self {
self.highlight = Some(v);
self
}
#[must_use]
pub fn dictionary(mut self, key: &'a str, value: &'a str) -> Self {
match &mut self.dictionary {
Some(dictionary) => dictionary.push((key, value)),
None => self.dictionary = Some(vec![(key, value)]),
}
self
}
#[must_use]
pub fn set_dictionary(mut self, dictionary: Vec<(&'a str, &'a str)>) -> Self {
self.dictionary = Some(dictionary);
self
}
#[must_use]
pub fn action(mut self, action: Action<'a>) -> Self {
match &mut self.actions {
Some(actions) => actions.push(action),
None => self.actions = Some(vec![action]),
}
self
}
#[must_use]
pub fn set_actions(mut self, actions: Vec<Action<'a>>) -> Self {
self.actions = Some(actions);
self
}
#[must_use]
pub fn outro(mut self, outro: &'a str) -> Self {
match &mut self.outros {
Some(outros) => outros.push(outro),
None => self.outros = Some(vec![outro]),
}
self
}
#[must_use]
pub fn set_outros(mut self, outros: Vec<&'a str>) -> Self {
self.outros = Some(outros);
self
}
#[must_use]
pub fn signature(mut self, signature: &'a str) -> Self {
self.signature = Some(signature);
self
}
#[must_use]
pub fn table(mut self, table: Table<'a>) -> Self {
match &mut self.tables {
Some(tables) => tables.push(table),
None => self.tables = Some(vec![table]),
}
self
}
#[must_use]
pub fn set_tables(mut self, tables: Vec<Table<'a>>) -> Self {
self.tables = Some(tables);
self
}
#[must_use]
pub fn go_to_action(mut self, text: &'a str, link: &'a str, description: &'a str) -> Self {
self.go_to_action = Some(GoToAction {
text,
link,
description,
});
self
}
#[must_use]
pub fn set_go_to_action(mut self, go_to_action: GoToAction<'a>) -> Self {
self.go_to_action = Some(go_to_action);
self
}
#[must_use]
pub fn build(self) -> Email<'a> {
Email {
summary: self.summary,
greeting: self.greeting,
intros: self.intros,
highlight: self.highlight,
dictionary: self.dictionary,
tables: self.tables,
actions: self.actions,
outros: self.outros,
signature: self.signature,
go_to_action: self.go_to_action,
}
}
}
#[cfg(test)]
mod tests {
use crate::email::Greeting;
use crate::EmailBuilder;
#[test]
fn usage() {
let _email = EmailBuilder::new()
.greeting(Greeting::Custom("custom greeting"))
.intro("test")
.build();
let mut email = EmailBuilder::new()
.intro("test intro")
.dictionary("test key", "test value")
.dictionary("test key 2", "test value 2");
{
let greeting = Greeting::Name("Test greeting");
email = email.greeting(greeting);
}
let _email = email.build();
}
}