1use std::{ffi::OsStr, path::PathBuf};
2
3use serde::{Deserialize, Serialize};
4
5use crate::{config::TemplateLang, normalize_line_endings};
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 let parseable = normalize_line_endings(contents_result.as_ref().unwrap().as_bytes());
24
25 Self {
26 at_path: path.clone(),
27 contents: parseable,
28 template_language: match path.clone().extension().and_then(OsStr::to_str).unwrap() {
29 "liquid" => TemplateLang::Liquid,
30 _ => panic!(
31 "Not sure what templating engine to use for this file. {}",
32 path.display()
33 ),
34 },
35 }
36 }
37
38 pub fn new_from_string(contents: String, template_language: TemplateLang) -> Self {
39 Self {
40 at_path: "".into(),
41 contents,
42 template_language,
43 }
44 }
45}