laravel-template-engine 0.2.0

Templates for emails, documents and notifications that your team edits, stored in the database
Documentation
//! Templates a type comes with.

use std::collections::BTreeMap;

use crate::template::{Content, DocumentFormat, DocumentSettings, MailSettings, Margins, Orientation, Settings};

/// A template a type comes with. `laravel template-engine:install-defaults` creates it when the
/// type has no template of that name, so the changes people make later are never overwritten.
///
/// ```
/// use laravel_template_engine::DefaultTemplate;
///
/// let confirmation = DefaultTemplate::mail("Confirmation")
///     .content("en", "Order {{ order.number }}", "<p>Thank you, {{ customer.name }}!</p>")
///     .to(["customer"])
///     .attach_document("Invoice");
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct DefaultTemplate {
    /// The name, unique within the type.
    pub name: String,
    /// The text, by language.
    pub content: BTreeMap<String, Content>,
    /// The settings.
    pub settings: Settings,
    /// Names of the type's default document templates to attach to an email.
    pub attach_documents: Vec<String>,
}

impl DefaultTemplate {
    /// An email template.
    #[must_use]
    pub fn mail(name: impl Into<String>) -> Self {
        Self::new(name, Settings::Mail(MailSettings::default()))
    }

    /// A document template.
    #[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(),
        }
    }

    /// Adds the text in `locale`. Documents leave `subject` empty.
    #[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
    }

    /// An email's recipients: recipient variables or addresses.
    #[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())
    }

    /// An email's copies.
    #[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())
    }

    /// An email's hidden copies.
    #[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())
    }

    /// An email's sender.
    #[must_use]
    pub fn from(self, from: impl Into<String>) -> Self {
        let from = from.into();
        self.mail_settings(|m| m.from = Some(from))
    }

    /// Attaches the type's default document template `name` to an email.
    #[must_use]
    pub fn attach_document(mut self, name: impl Into<String>) -> Self {
        self.attach_documents.push(name.into());
        self
    }

    /// A document's file name, a template itself.
    #[must_use]
    pub fn filename(self, filename: impl Into<String>) -> Self {
        let filename = filename.into();
        self.document_settings(|d| d.filename = filename)
    }

    /// Makes a document an HTML page instead of a PDF.
    #[must_use]
    pub fn html(self) -> Self {
        self.document_settings(|d| d.format = DocumentFormat::Html)
    }

    /// A document's paper size, such as `a5` or `letter`.
    #[must_use]
    pub fn paper(self, paper: impl Into<String>) -> Self {
        let paper = paper.into();
        self.document_settings(|d| d.paper = paper)
    }

    /// Turns a document's paper sideways.
    #[must_use]
    pub fn landscape(self) -> Self {
        self.document_settings(|d| d.orientation = Orientation::Landscape)
    }

    /// The layout the text goes into, by the name it was registered under.
    #[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
    }

    /// A PDF document's margins.
    #[must_use]
    pub fn margins(self, margins: Margins) -> Self {
        self.document_settings(|d| d.margins = margins)
    }

    /// A document's font and its size in points.
    #[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);
        })
    }

    /// A document's line spacing, as a multiple of the font 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
    }
}