1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//! Templater management.
//!
//! This can use Handlebars, Liquid, Tera.
//!
//! This can be expanded for potential future formats.
use std::path::PathBuf;
use crate::app::args::Args;
use crate::errors::*;
use crate::types::*;
pub trait TemplaterTrait {
// Create a new templater.
//
// Example:
//
// ```
// let templater: Templater = TemplaterWithHandlebars::new();
// ```
//
fn new() -> Self where Self: Sized;
// Create a new templater with args.
//
// Example:
//
// ```
// let args = Args::default();
// let templater: Templater = TemplaterWithHandlebars::new_with_args(&args);
// ```
//
fn new_with_args(args: &Args) -> Self where Self: Sized;
// Get the template name default e.g. "default".
//
// ```
// let name = template_name_default();
// assert_eq!(name, "default");
// ```
//
fn template_name_default(&self) -> String;
// Get the template content text e.g. "{{ content }}".
//
// ```
// let content_text = template_content_text_default();
// assert_eq!(content_text, "{{ content }}");
// ```
//
fn template_content_text_default(&self) -> String;
// Add a default template.
//
// Example:
//
// ```
// let templater: Templater = TemplaterWithHandlebars::new();
// register_template_via_default(templater);
// //-> Handlebars now has a template name "default" with content "{{ content }}"
// ```
//
fn register_template_via_default(&mut self) -> Result<()> where Self: Sized {
let name = &self.template_name_default();
let content = &self.template_content_text_default();
self.register_template_via_name_and_content_text(&name, &content)
}
// Add a template via template name (i.e. key) and template text (i.e. value).
//
// Example:
//
// ```
// let templater: Templater = TemplaterWithHandlebars::new();
// let name = "alpha";
// let content_text = "{{ bravo }}";
// templater.register_template_via_name_and_content_text(&name, &content_text);
// ```
//
fn register_template_via_name_and_content_text(&mut self, name: &str, content_text: &str) -> Result<()>;
// Add a template via template name (i.e. key) and template file (i.e. value).
//
// Example:
//
// ```
// let mut templater: Templater = TemplaterWithHandlebars::new();
// let name = "alpha";
// let file = PathBuf::from("template.html")
// register_template_via_name_and_content_file(&name, &content_file);
// ```
//
fn register_template_via_name_and_content_file(&mut self, name: &str, content_file: &PathBuf) -> Result<()>;
// Does the templater contain any template?
//
// Example:
//
// ```
// let mut templater: Templater = TemplaterWithHandlebars::new();
// templater.register_template_via_name_and_content_text("alpha", "bravo");
// let flag = templater.contains_any_template();
// assert_eq!(flag, true);
// ```
//
fn contains_any_template(&self) -> bool;
// Does the templater contain a specific template key i.e. template name?
//
// Example:
//
// ```
// let mut templater: Templater = TemplaterWithHandlebars::new();
// templater.register_template_via_name_and_content_text("alpha", "bravo");
// let flag = templater.contains_template_name("alpha");
// assert_eq!(flag, true);
// ```
//
fn contains_template_name(&self, name: &str) -> bool;
// Get the template names.
//
// Example:
//
// ```
// let mut templater: Templater = TemplaterWithHandlebars::new();
// register_template_via_name_and_content_text("alpha", "alpha text".into());
// register_template_via_name_and_content_text("bravo", "bravo text".into());
// let template_names: Set<&str> = template_names_as_set_str(&templater);
// assert_eq!(template_names, set!["alpha", "bravo"]);
// ```
//
fn template_names_as_set_str(&self) -> Set<&str>;
// Register a helper via helper name (i.e. key) and helper text (i.e. value).
//
// Example:
//
// ```
// let templater: Templater = TemplaterWithHandlebars::new();
// let name = "alpha";
// let content_text = "{{ bravo }}";
// templater.register_helper_via_name_and_content_text(&name, &content_text);
// ```
//
fn register_helper_via_name_and_content_text(&mut self, name: &str, content_text: &str) -> Result<()>;
// Register a helper via helper name (i.e. key) and helper file (i.e. value).
//
// Example:
//
// ```
// let mut templater: Templater = TemplaterWithHandlebars::new();
// let name = "alpha";
// let file = PathBuf::from("helper.html")
// register_helper_via_name_and_content_file(&name, &content_file);
// ```
//
fn register_helper_via_name_and_content_file(&mut self, name: &str, content_file: &PathBuf) -> Result<()>;
// Render a template name with the state.
//
// ```
// let templater: Templater = TemplaterWithHandlebars::new();
// templater.register_template_via_name_and_content_text("alpha", "<p>{{ content }}</p>");
// let name = template_name_default();
// let json: ::serde_json::Value = ::serde_json::from_str(indoc!{r#"{"content": "bravo"}"#}).unwrap();
// let state_enum: StateEnum::JSON(json);
// let html = templater.render(&name, &state_enum);
// assert_eq!(html, "<p>bravo</p>");
// ```
//
fn render_template_with_state_enum(&self, template_name: &str, state_enum: &crate::state::state_enum::StateEnum) -> Result<HtmlString>;
}
#[cfg(test)]
mod tests {
use super::*;
use crate::app::args::Args;
use crate::templater::templater_with_handlebars::TemplaterWithHandlebars;
const FAB_OUTPUT_HTML: &str = "my content";
type TemplaterX<'templater> = TemplaterWithHandlebars<'templater>;
#[test]
fn test_new() {
let _templater = TemplaterX::new();
//TODO
}
#[test]
fn test_new_with_args() {
let args = Args::default();
let _templater = TemplaterX::new_with_args(&args);
//TODO
}
#[test]
fn test_register_template_via_name_and_content_text() {
let mut templater = TemplaterX::new();
let name = "alpha";
let content_text = "{{ bravo }}";
assert!(!templater.contains_any_template());
let result = templater.register_template_via_name_and_content_text(&name, &content_text);
assert!(result.is_ok());
assert!(templater.contains_any_template());
}
#[test]
fn test_register_template_via_name_and_content_file() {
let mut templater = TemplaterX::new();
let name = "alpha";
let content_file = crate::test::TESTS_DIR
.join("src")
.join("templater")
.join("templater_trait")
.join("register_template_via_name_and_content_file")
.join("template.html");
assert!(content_file.exists(), "content_file: {:?}", content_file);
assert!(!templater.contains_any_template());
let result = templater.register_template_via_name_and_content_file(&name, &content_file);
assert!(result.is_ok());
assert!(templater.contains_any_template());
}
#[test]
fn test_contains_any_template_x_true() {
let mut templater = TemplaterX::new();
templater.register_template_via_name_and_content_text("my-name", "my-content").expect("register_template_via_name_and_content_text");
let flag = templater.contains_any_template();
assert_eq!(flag, true);
}
#[test]
fn test_contains_any_template_x_false() {
let templater = TemplaterX::new();
let flag = templater.contains_any_template();
assert_eq!(flag, false);
}
#[test]
fn test_template_names_as_set_str() {
let mut templater = TemplaterX::new();
let name_0: &str = "my-name-0";
let name_1: &str = "my-name-1";
templater.register_template_via_name_and_content_text(&name_0, "my text 0").expect("register_template_via_name_and_content_text");
templater.register_template_via_name_and_content_text(&name_1, "my text 1").expect("register_template_via_name_and_content_text");
let actual: Set<&str> = templater.template_names_as_set_str();
let expect: Set<&str> = set!(name_0, name_1);
assert_eq!(actual, expect);
}
}