lib_humus/
template_loader.rs1
2use lib_humus_configuration::ErrorCause;
3use log::{info,error};
4use tera::Tera;
5use serde::Deserialize;
6use lib_humus_configuration::HumusConfigError;
7use lib_humus_configuration::read_from_toml_file;
8
9use std::error::Error;
10use std::fmt;
11
12use crate::HumusProtoEngine;
13
14#[derive(Debug, Clone, Deserialize)]
45pub struct TemplateEngineLoader {
46 pub template_location: String,
48 pub extra_config_location: Option<String>,
51}
52
53impl TemplateEngineLoader {
54
55 pub fn new(
57 template_location: String,
58 extra_config_location: Option<String>
59 ) -> Self {
60 Self {
61 template_location: template_location,
62 extra_config_location: extra_config_location,
63 }
64 }
65
66 pub fn cli_template_location(mut self, location: Option<String>) -> Self {
70 if let Some(location) = location {
71 self.template_location = location;
72 }
73 self
74 }
75
76 pub fn cli_extra_config_location(mut self, location: Option<String>) -> Self {
80 if let Some(location) = location {
81 self.extra_config_location = Some(location);
82 }
83 self
84 }
85
86 pub fn base_dir(&self) -> String {
90 if self.template_location.ends_with("/") {
91 self.template_location.clone()
92 } else {
93 self.template_location.clone()+"/"
94 }
95 }
96
97 pub fn load_templates(
109 &self
110 ) -> Result<HumusProtoEngine,TemplateEngineLoaderError> {
111 let template_base_dir = self.base_dir();
112 let template_extra_config_res = match &self.extra_config_location {
113 Some(path) => read_from_toml_file(path),
114 None => {
115 read_from_toml_file(&(template_base_dir.clone()+"extra.toml"))
116 }
117 };
118 let template_extra_config = match template_extra_config_res {
119 Ok(c) => Some(c),
120 Err(e) => match &e.cause {
121 ErrorCause::FileRead{..} => {
122 if self.extra_config_location.is_some() {
126 return Err(TemplateEngineLoaderError::ConfigurationError(e));
127 }
128 None
129 },
130 _ => {
131 return Err(TemplateEngineLoaderError::ConfigurationError(e));
132 }
133 },
134 };
135 let template_glob = template_base_dir.clone()+"*";
136 info!("Parsing Templates from '{}' ...", &template_glob);
137 let res = Tera::new((template_glob).as_str());
138 let tera = match res {
139 Ok(t) => t,
140 Err(e) => {
141 error!("Error Parsing Template: {e}");
142 return Err(TemplateEngineLoaderError::TemplateParseError{
143 path: template_glob,
144 tera_error: e,
145 });
146 }
147 };
148 Ok(HumusProtoEngine {
149 tera: tera,
150 template_config: template_extra_config,
151 })
152 }
153}
154
155#[derive(Debug)]
159pub enum TemplateEngineLoaderError {
160 ConfigurationError(HumusConfigError),
162 TemplateParseError{
164 path: String,
166 tera_error: tera::Error
168 },
169}
170
171impl fmt::Display for TemplateEngineLoaderError {
172 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
173 match self {
174 Self::ConfigurationError(e) =>
175 write!(f,"Error with template extra configuration:\n{e}"),
176 Self::TemplateParseError{path, tera_error} =>
177 write!(f,"Error parsing template '{path}':\n{tera_error}"),
178 }
179 }
180}
181
182impl Error for TemplateEngineLoaderError {
183 fn source(&self) -> Option<&(dyn Error + 'static)> {
184 match self {
185 Self::ConfigurationError(error) =>
186 Some(error),
187 Self::TemplateParseError{tera_error, ..} =>
188 Some(tera_error),
189 }
190 }
191}