1use anyhow::Result;
2use handlebars::Handlebars;
3use rust_embed::RustEmbed;
4use serde::Serialize;
5use std::sync::Arc;
6
7#[derive(RustEmbed)]
8#[folder = "src/templates"]
9#[include = "*.hbs"]
10struct Assets;
11
12pub struct Templates(Handlebars<'static>);
13
14impl Templates {
15 pub fn new() -> Arc<Self> {
16 let mut handlebars = Handlebars::new();
17 handlebars.register_embed_templates::<Assets>().unwrap();
18 handlebars.register_escape_fn(|text| text.into());
19 Arc::new(Self(handlebars))
20 }
21}
22
23pub trait Template: Sized {
24 const TEMPLATE_NAME: &'static str;
25
26 fn render(&self, templates: &Templates) -> Result<String>
27 where
28 Self: Serialize + Sized,
29 {
30 Ok(templates.0.render(Self::TEMPLATE_NAME, self)?)
31 }
32}