templates.rs

 1use std::sync::Arc;
 2
 3use anyhow::Result;
 4use handlebars::Handlebars;
 5use rust_embed::RustEmbed;
 6use serde::Serialize;
 7
 8#[derive(RustEmbed)]
 9#[folder = "src/templates"]
10#[include = "*.hbs"]
11struct Assets;
12
13pub struct Templates(Handlebars<'static>);
14
15impl Templates {
16    pub fn new() -> Arc<Self> {
17        let mut handlebars = Handlebars::new();
18        handlebars.register_embed_templates::<Assets>().unwrap();
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}
33
34#[derive(Serialize)]
35pub struct BaseTemplate {
36    pub os: String,
37    pub shell: String,
38    pub worktrees: Vec<WorktreeData>,
39}
40
41impl Template for BaseTemplate {
42    const TEMPLATE_NAME: &'static str = "base.hbs";
43}
44
45#[derive(Serialize)]
46pub struct WorktreeData {
47    pub root_name: String,
48}
49
50#[derive(Serialize)]
51pub struct GlobTemplate {
52    pub project_roots: String,
53}
54
55impl Template for GlobTemplate {
56    const TEMPLATE_NAME: &'static str = "glob.hbs";
57}