1use std::{ffi::OsStr, path::PathBuf};
2
3use serde::{Deserialize, Serialize};
4
5use crate::config::TemplateLang;
6
7#[derive(Debug, Serialize, Deserialize)]
8pub struct Template {
9 pub at_path: PathBuf,
10 pub contents: String,
11 pub template_language: TemplateLang,
12}
13
14impl Template {
15 pub fn new_from_path(path: PathBuf) -> Self {
16 let contents_result = std::fs::read_to_string(&path);
17
18 if contents_result.is_err() {
19 dbg!("error reading file: {}", contents_result.err());
20 panic!("failed to read '{}'", path.display());
21 }
22
23 Self {
24 at_path: path.clone(),
25 contents: contents_result.unwrap(),
26 template_language: match path.clone().extension().and_then(OsStr::to_str).unwrap() {
27 "liquid" => TemplateLang::Liquid,
28 _ => panic!(
29 "Not sure what templating engine to use for this file. {}",
30 path.display()
31 ),
32 },
33 }
34 }
35}