Skip to main content

actions_templates/
template.rs

1use anyhow::Result;
2use serde::{Deserialize, Serialize};
3use std::{ops::Deref, path::Path};
4
5#[derive(Debug, Serialize, Deserialize, Clone)]
6pub struct Template(pub String);
7
8impl From<String> for Template {
9    fn from(value: String) -> Self {
10        Self(value)
11    }
12}
13
14impl Template {
15    pub fn from_file<P>(path: P) -> Result<Self>
16    where
17        P: AsRef<Path>,
18    {
19        let file = std::fs::read_to_string(path)?;
20
21        Ok(Self(file))
22    }
23
24    pub fn render_to_string<D: Serialize>(&self, data: &D) -> Result<String> {
25        let handlebars = handlebars::Handlebars::new();
26        Ok(handlebars.render_template(self, data)?)
27    }
28}
29
30impl Deref for Template {
31    type Target = String;
32    fn deref(&self) -> &Self::Target {
33        &self.0
34    }
35}