use anyhow::Result; use handlebars::Handlebars; use rust_embed::RustEmbed; use serde::Serialize; use std::sync::Arc; #[derive(RustEmbed)] #[folder = "src/templates"] #[include = "*.hbs"] struct Assets; pub struct Templates(Handlebars<'static>); impl Templates { pub fn new() -> Arc { let mut handlebars = Handlebars::new(); handlebars.register_embed_templates::().unwrap(); handlebars.register_escape_fn(|text| text.into()); Arc::new(Self(handlebars)) } } pub trait Template: Sized { const TEMPLATE_NAME: &'static str; fn render(&self, templates: &Templates) -> Result where Self: Serialize + Sized, { Ok(templates.0.render(Self::TEMPLATE_NAME, self)?) } }