-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathengine.rs
More file actions
461 lines (386 loc) · 14.7 KB
/
Copy pathengine.rs
File metadata and controls
461 lines (386 loc) · 14.7 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! Template Engine Implementation
//!
//! Provides the `TemplateEngine` struct that handles template validation and rendering with Tera.
use serde::Serialize;
use std::error::Error as StdError;
use tera::Tera;
use thiserror::Error;
/// Extracts the full error chain from a `tera::Error` as a single string.
///
/// Tera errors have nested sources that are important for debugging (e.g., "Variable 'x' not found").
/// The standard Display trait only shows the outer message. This function traverses
/// the entire error chain and concatenates all messages.
fn tera_error_chain(err: &tera::Error) -> String {
let mut messages = vec![err.to_string()];
let mut current: Option<&(dyn StdError + 'static)> = err.source();
while let Some(source) = current {
messages.push(source.to_string());
current = source.source();
}
messages.join(" -> ")
}
/// Errors that can occur during template engine operations
#[derive(Debug, Error)]
pub enum TemplateEngineError {
#[error(
"Failed to parse template '{template_name}': {}",
tera_error_chain(source)
)]
TemplateParse {
template_name: String,
#[source]
source: tera::Error,
},
#[error("Failed to serialize template context: {}", tera_error_chain(source))]
ContextSerialization {
#[source]
source: tera::Error,
},
#[error(
"Failed to render template '{template_name}': {}",
tera_error_chain(source)
)]
TemplateRender {
template_name: String,
#[source]
source: tera::Error,
},
}
/// Template processing engine for validation and rendering
#[derive(Debug, Default)]
pub struct TemplateEngine {
tera: Tera,
}
impl TemplateEngine {
/// Creates a new `TemplateEngine` instance with an empty Tera engine
#[must_use]
pub fn new() -> Self {
Self {
tera: Tera::default(),
}
}
/// Creates a new `TemplateEngine` with template content and validates it with the given context
///
/// This method combines template creation and validation to ensure templates are always
/// instantiated in a valid state. It will fail if:
/// - Template has syntax errors
/// - Template references undefined variables
/// - Template cannot be rendered for any reason
///
/// # Errors
/// Returns an error if template content cannot be parsed or validation fails
pub fn render<T: Serialize>(
&mut self,
template_name: &str,
template_content: &str,
context: &T,
) -> Result<String, TemplateEngineError> {
// Add the template content to this validator instance
self.add_template(template_name, template_content)?;
// Validate the template by rendering it
let validated_content = self.render_template(template_name, context)?;
Ok(validated_content)
}
/// Adds template content to this validator instance
///
/// # Errors
/// Returns an error if the template content cannot be parsed
fn add_template(
&mut self,
template_name: &str,
template_content: &str,
) -> Result<(), TemplateEngineError> {
self.tera
.add_raw_template(template_name, template_content)
.map_err(|source| TemplateEngineError::TemplateParse {
template_name: template_name.to_string(),
source,
})?;
Ok(())
}
/// Renders a template by name with the given context and validates the result
///
/// # Errors
/// Returns an error if template rendering fails or variables cannot be substituted
fn render_template<T: Serialize>(
&self,
template_name: &str,
context: &T,
) -> Result<String, TemplateEngineError> {
let tera_context = tera::Context::from_serialize(context)
.map_err(|source| TemplateEngineError::ContextSerialization { source })?;
let rendered_content =
self.tera
.render(template_name, &tera_context)
.map_err(|source| TemplateEngineError::TemplateRender {
template_name: template_name.to_string(),
source,
})?;
Ok(rendered_content)
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde::Serialize;
#[derive(Serialize)]
struct TestContext {
name: String,
value: u32,
}
#[derive(Serialize)]
struct PartialContext {
name: String,
}
// Tests for TemplateEngine::new()
#[test]
fn it_should_create_new_validator_instance() {
let validator = TemplateEngine::new();
// Verify it was created successfully - we can't inspect internal state
// but we can verify the Debug trait works (indicating successful creation)
assert!(format!("{validator:?}").contains("TemplateEngine"));
}
#[test]
fn it_should_create_multiple_independent_validators() {
let validator1 = TemplateEngine::new();
let validator2 = TemplateEngine::new();
// Both should be successfully created
assert!(format!("{validator1:?}").contains("TemplateEngine"));
assert!(format!("{validator2:?}").contains("TemplateEngine"));
}
// Tests for TemplateEngine::render()
#[test]
fn it_should_render_simple_template_successfully() -> Result<(), TemplateEngineError> {
let mut validator = TemplateEngine::new();
let template_content = "Hello {{name}}! Value: {{value}}";
let context = TestContext {
name: "World".to_string(),
value: 42,
};
let rendered_content = validator.render("test_template", template_content, &context)?;
assert_eq!(rendered_content, "Hello World! Value: 42");
Ok(())
}
#[test]
fn it_should_render_template_with_no_variables() -> Result<(), TemplateEngineError> {
let mut validator = TemplateEngine::new();
let template_content = "This is a static template with no variables.";
let context = TestContext {
name: "unused".to_string(),
value: 0,
};
let rendered_content = validator.render("static_template", template_content, &context)?;
assert_eq!(
rendered_content,
"This is a static template with no variables."
);
Ok(())
}
#[test]
fn it_should_render_empty_template() -> Result<(), TemplateEngineError> {
let mut validator = TemplateEngine::new();
let template_content = "";
let context = TestContext {
name: "test".to_string(),
value: 42,
};
let rendered_content = validator.render("empty_template", template_content, &context)?;
assert_eq!(rendered_content, "");
Ok(())
}
#[test]
fn it_should_handle_empty_template_name() -> Result<(), TemplateEngineError> {
let mut validator = TemplateEngine::new();
let template_content = "Hello {{name}}!";
let context = TestContext {
name: "World".to_string(),
value: 42,
};
let rendered_content = validator.render("", template_content, &context)?;
assert_eq!(rendered_content, "Hello World!");
Ok(())
}
#[test]
fn it_should_fail_when_template_has_malformed_syntax() {
let mut validator = TemplateEngine::new();
let template_content = "Hello {{name! Invalid syntax";
let context = TestContext {
name: "World".to_string(),
value: 42,
};
let result = validator.render("malformed_template", template_content, &context);
assert!(result.is_err());
match result.unwrap_err() {
TemplateEngineError::TemplateParse { template_name, .. } => {
assert_eq!(template_name, "malformed_template");
}
other => panic!("Expected TemplateParse error, got: {other:?}"),
}
}
#[test]
fn it_should_fail_when_template_references_undefined_variable() {
let mut validator = TemplateEngine::new();
let template_content = "Hello {{name}}! Value: {{undefined_variable}}";
let context = PartialContext {
name: "World".to_string(),
};
let result = validator.render("undefined_var_template", template_content, &context);
assert!(result.is_err());
match result.unwrap_err() {
TemplateEngineError::TemplateRender { template_name, .. } => {
assert_eq!(template_name, "undefined_var_template");
}
other => panic!("Expected TemplateRender error, got: {other:?}"),
}
}
#[test]
fn it_should_fail_when_context_serialization_fails() {
struct FailingSerialize;
impl Serialize for FailingSerialize {
fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
Err(serde::ser::Error::custom(
"Intentional serialization failure",
))
}
}
let mut validator = TemplateEngine::new();
let template_content = "Hello {{name}}!";
let context = FailingSerialize;
let result = validator.render("serialize_fail_template", template_content, &context);
assert!(result.is_err());
match result.unwrap_err() {
TemplateEngineError::ContextSerialization { .. } => {
// Expected
}
other => panic!("Expected ContextSerialization error, got: {other:?}"),
}
}
#[test]
fn it_should_render_yaml_like_template() -> Result<(), TemplateEngineError> {
let mut validator = TemplateEngine::new();
let template_content = "name: {{name}}\nvalue: {{value}}\nstatic: true";
let context = TestContext {
name: "test".to_string(),
value: 42,
};
let rendered_content = validator.render("yaml_template", template_content, &context)?;
assert!(rendered_content.contains("name: test"));
assert!(rendered_content.contains("value: 42"));
assert!(rendered_content.contains("static: true"));
Ok(())
}
#[test]
fn it_should_render_different_templates_with_same_validator() -> Result<(), TemplateEngineError>
{
let mut validator = TemplateEngine::new();
let context = TestContext {
name: "Alice".to_string(),
value: 100,
};
// Render first template
let template1 = "Hello {{name}}!";
let result1 = validator.render("template1", template1, &context)?;
assert_eq!(result1, "Hello Alice!");
// Render second template with same validator
let template2 = "Value is: {{value}}";
let result2 = validator.render("template2", template2, &context)?;
assert_eq!(result2, "Value is: 100");
Ok(())
}
#[test]
fn it_should_handle_complex_template_with_multiple_variables() -> Result<(), TemplateEngineError>
{
let mut validator = TemplateEngine::new();
let template_content = r#"
# Configuration for {{name}}
version: "1.0"
settings:
port: {{value}}
enabled: true
name: "{{name}}"
"#;
let context = TestContext {
name: "MyApp".to_string(),
value: 8080,
};
let rendered_content = validator.render("config_template", template_content, &context)?;
assert!(rendered_content.contains("# Configuration for MyApp"));
assert!(rendered_content.contains("port: 8080"));
assert!(rendered_content.contains(r#"name: "MyApp""#));
Ok(())
}
#[test]
fn it_should_handle_special_characters_in_template_name() -> Result<(), TemplateEngineError> {
let mut validator = TemplateEngine::new();
let template_content = "Hello {{name}}!";
let context = TestContext {
name: "World".to_string(),
value: 42,
};
// Test with special characters in template name
let rendered_content =
validator.render("template-with_special.chars", template_content, &context)?;
assert_eq!(rendered_content, "Hello World!");
Ok(())
}
#[test]
fn it_should_allow_extra_variables_in_context() -> Result<(), TemplateEngineError> {
let mut validator = TemplateEngine::new();
let template_content = "Hello {{name}}!"; // Only uses 'name' variable
let context = TestContext {
name: "World".to_string(),
value: 42, // This variable is not used in the template but should be allowed
};
let rendered_content =
validator.render("extra_vars_template", template_content, &context)?;
// Should render successfully and ignore the extra 'value' variable
assert_eq!(rendered_content, "Hello World!");
Ok(())
}
#[test]
fn it_should_allow_tera_delimiters_in_rendered_output() -> Result<(), TemplateEngineError> {
let mut validator = TemplateEngine::new();
// Simple template that outputs content containing Tera-like delimiters
// We use raw blocks to prevent Tera from parsing the output delimiters
let template_content =
"Hello {{name}}! Use {% raw %}{{ variable }}{% endraw %} in your config.";
let context = TestContext {
name: "World".to_string(),
value: 42,
};
let rendered_content =
validator.render("delimiter_template", template_content, &context)?;
// Should render successfully and contain delimiters in the final output
assert_eq!(
rendered_content,
"Hello World! Use {{ variable }} in your config."
);
Ok(())
}
#[test]
fn it_should_allow_all_tera_delimiter_types_in_output() -> Result<(), TemplateEngineError> {
#[derive(Serialize)]
struct SimpleContext {
app_name: String,
}
let mut validator = TemplateEngine::new();
// Template demonstrating all Tera delimiter types can appear in final output
let template_content = r"App: {{app_name}}
Expression example: {% raw %}{{ expr }}{% endraw %}
Statement example: {% raw %}{% if condition %}{% endraw %}
Comment example: {% raw %}{# comment #}{% endraw %}";
let context = SimpleContext {
app_name: "MyApp".to_string(),
};
let rendered_content = validator.render("all_delimiters", template_content, &context)?;
// Should render successfully and preserve all delimiter types in output
assert!(rendered_content.contains("App: MyApp"));
assert!(rendered_content.contains("{{ expr }}"));
assert!(rendered_content.contains("{% if condition %}"));
assert!(rendered_content.contains("{# comment #}"));
Ok(())
}
}