swadhinbiswas 📧 p4: mail template engine + background queue · 7/25/2026
db618f3
48 lines · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use std::collections::HashMap;
use std::sync::Arc;

use minijinja::Environment;
use tokio::sync::RwLock;

/// Renders email templates using MiniJinja.
pub struct EmailTemplates {
    env: Arc<RwLock<Environment<'static>>>,
}

impl std::fmt::Debug for EmailTemplates {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EmailTemplates").finish()
    }
}

impl EmailTemplates {
    pub fn new() -> Self {
        Self { env: Arc::new(RwLock::new(Environment::new())) }
    }

    /// Add a template from a string.
    pub async fn add_template(&self, name: &str, template: &str) -> Result<(), anyhow::Error> {
        let mut env = self.env.write().await;
        env.add_template_owned(name.to_string(), template.to_string())?;
        Ok(())
    }

    /// Render a named template with the given context.
    pub async fn render(
        &self,
        name: &str,
        ctx: &HashMap<String, serde_json::Value>,
    ) -> Result<String, anyhow::Error> {
        let env = self.env.read().await;
        let tmpl = env.get_template(name)?;
        let result = tmpl.render(ctx)?;
        Ok(result)
    }
}

impl Default for EmailTemplates {
    fn default() -> Self {
        Self::new()
    }
}