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
49
50
51
52
53
54
55
56
57
58
59
use crate::{builder::metadata::BODY_META, *};
use anyhow::{Context as _, Result};
use std::sync::Arc;
use tera::Tera;

/// Template engine (uses [`tera`])
pub struct TemplateEngine {
    tera: Tera,
}
impl TemplateEngine {
    /// Load templates and create
    /// template engine instance
    pub fn new(template_dir: impl AsRef<str>) -> Result<Self> {
        let tera = tera::Tera::new(template_dir.as_ref()).context("Template error")?;
        Ok(Self { tera })
    }

    /// Get `Arc<TemplateEngine>` for sharing template engine for multiple tasks
    pub fn get(self) -> Arc<Self> {
        Arc::new(self)
    }

    /// Render HTML using specified template and metadata
    pub fn render(&self, template: impl AsRef<str>, metadata: &Metadata) -> Result<String> {
        let tera_ctx =
            tera::Context::from_serialize(metadata).context("Context serialization error")?;
        let out = self
            .tera
            .render(template.as_ref(), &tera_ctx)
            .context("Tera rendering error")?;
        Ok(out)
    }
}

/// Template renderer renders HTML using specified template and
/// compiling metadata.
pub struct TemplateRenderer {
    engine: Arc<TemplateEngine>,
    template: String,
}
impl TemplateRenderer {
    pub fn new(engine: Arc<TemplateEngine>, template: impl ToString) -> Self {
        let template = template.to_string();
        Self { engine, template }
    }
}
impl Compiler for TemplateRenderer {
    fn compile(&self, ctx: Context) -> CompilerReturn {
        let engine = self.engine.clone();
        let template = self.template.clone();
        Box::new(compile!({
            let mut ctx = ctx;
            let metadata = ctx.metadata().await;
            let body = engine.render(&template, &metadata)?;
            ctx.insert_compiling_metadata(BODY_META, body)?;
            Ok(ctx)
        }))
    }
}