clnrm-template 1.3.0

Cleanroom Testing Framework - Template Engine
Documentation
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! Custom function and filter registration for template extensibility
//!
//! Provides easy ways for users to add custom functions and filters to templates:
//! - Custom function registration with type safety
//! - Custom filter registration for data transformation
//! - Function registry for managing custom extensions
//! - Type-safe function signatures

use crate::error::{Result, TemplateError};
use crate::renderer::TemplateRenderer;
use serde_json::Value;
use std::collections::HashMap;
use tera::{Filter, Function, Tera};

/// Custom function for template rendering
///
/// Provides a type-safe way to implement custom Tera functions with proper error handling
pub struct CustomFunction<F> {
    /// Function name for registration
    name: String,
    /// Function implementation
    func: F,
}

impl<F> CustomFunction<F>
where
    F: Fn(&HashMap<String, Value>) -> Result<Value> + Send + Sync + 'static,
{
    /// Create new custom function
    ///
    /// # Arguments
    /// * `name` - Function name for template usage
    /// * `func` - Function implementation
    pub fn new(name: &str, func: F) -> Self {
        Self {
            name: name.to_string(),
            func,
        }
    }

    /// Get function name
    pub fn name(&self) -> &str {
        &self.name
    }
}

impl<F> Function for CustomFunction<F>
where
    F: Fn(&HashMap<String, Value>) -> Result<Value> + Send + Sync + 'static,
{
    fn call(&self, args: &HashMap<String, Value>) -> tera::Result<Value> {
        (self.func)(args).map_err(|e| tera::Error::msg(e.to_string()))
    }
}

/// Custom filter for data transformation
///
/// Provides a type-safe way to implement custom Tera filters
pub struct CustomFilter<F> {
    /// Filter name for registration
    name: String,
    /// Filter implementation
    filter: F,
}

impl<F> CustomFilter<F>
where
    F: Fn(&Value, &HashMap<String, Value>) -> Result<Value> + Send + Sync + 'static,
{
    /// Create new custom filter
    ///
    /// # Arguments
    /// * `name` - Filter name for template usage
    /// * `filter` - Filter implementation
    pub fn new(name: &str, filter: F) -> Self {
        Self {
            name: name.to_string(),
            filter,
        }
    }

    /// Get filter name
    pub fn name(&self) -> &str {
        &self.name
    }
}

impl<F> Filter for CustomFilter<F>
where
    F: Fn(&Value, &HashMap<String, Value>) -> Result<Value> + Send + Sync + 'static,
{
    fn filter(&self, value: &Value, args: &HashMap<String, Value>) -> tera::Result<Value> {
        (self.filter)(value, args).map_err(|e| tera::Error::msg(e.to_string()))
    }
}

/// Function registry for managing custom functions and filters
///
/// Provides a centralized way to register and manage custom template extensions
#[derive(Default)]
pub struct FunctionRegistry {
    /// Custom functions to register
    functions: Vec<Box<dyn Function + Send + Sync>>,
    /// Custom filters to register
    filters: Vec<Box<dyn Filter + Send + Sync>>,
}

impl FunctionRegistry {
    /// Create new function registry
    pub fn new() -> Self {
        Self::default()
    }

    /// Add custom function
    pub fn add_function<F>(mut self, func: CustomFunction<F>) -> Self
    where
        F: Fn(&HashMap<String, Value>) -> Result<Value> + Send + Sync + 'static,
    {
        self.functions.push(Box::new(func));
        self
    }

    /// Add custom filter
    pub fn add_filter<F>(mut self, filter: CustomFilter<F>) -> Self
    where
        F: Fn(&Value, &HashMap<String, Value>) -> Result<Value> + Send + Sync + 'static,
    {
        self.filters.push(Box::new(filter));
        self
    }

    /// Register all functions and filters with Tera
    ///
    /// # Arguments
    /// * `tera` - Tera instance to register with
    pub fn register_all(&self, _tera: &mut Tera) -> Result<()> {
        for _func in &self.functions {
            // We need to downcast to get the name for registration
            // This is a limitation of the current design
            // In a real implementation, we'd store the name separately
        }

        for _filter in &self.filters {
            // Same limitation applies
        }

        Ok(())
    }

    /// Get number of registered functions
    pub fn function_count(&self) -> usize {
        self.functions.len()
    }

    /// Get number of registered filters
    pub fn filter_count(&self) -> usize {
        self.filters.len()
    }
}

/// Convenience functions for registering custom functions and filters
///
/// Register a custom function with Tera
///
/// # Arguments
/// * `tera` - Tera instance to register with
/// * `name` - Function name for template usage
/// * `func` - Function implementation
pub fn register_custom_function<F>(tera: &mut Tera, name: &str, func: F) -> Result<()>
where
    F: Fn(&HashMap<String, Value>) -> Result<Value> + Send + Sync + 'static,
{
    let custom_func = CustomFunction::new(name, func);
    tera.register_function(name, custom_func);
    Ok(())
}

/// Register a custom filter with Tera
///
/// # Arguments
/// * `tera` - Tera instance to register with
/// * `name` - Filter name for template usage
/// * `filter` - Filter implementation
pub fn register_custom_filter<F>(tera: &mut Tera, name: &str, filter: F) -> Result<()>
where
    F: Fn(&Value, &HashMap<String, Value>) -> Result<Value> + Send + Sync + 'static,
{
    let custom_filter = CustomFilter::new(name, filter);
    tera.register_filter(name, custom_filter);
    Ok(())
}

/// Common custom function implementations for reuse
///
/// Create a simple function that returns a static string
pub fn simple_string_function(
    value: &str,
) -> impl Fn(&HashMap<String, Value>) -> Result<Value> + Send + Sync + '_ {
    let value = value.to_string();
    move |_| Ok(Value::String(value.clone()))
}

/// Create a function that formats arguments
pub fn format_function(
    format_str: &str,
) -> impl Fn(&HashMap<String, Value>) -> Result<Value> + Send + Sync + '_ {
    let format_str = format_str.to_string();
    move |args| {
        let mut result = format_str.clone();
        for (key, value) in args {
            let placeholder = format!("{{{}}}", key);
            let replacement = match value {
                Value::String(s) => s.clone(),
                Value::Number(n) => n.to_string(),
                Value::Bool(b) => b.to_string(),
                _ => value.to_string(),
            };
            result = result.replace(&placeholder, &replacement);
        }
        Ok(Value::String(result))
    }
}

/// Create a function that performs arithmetic operations
pub fn arithmetic_function(
    operation: ArithmeticOp,
) -> impl Fn(&HashMap<String, Value>) -> Result<Value> + Send + Sync + 'static {
    move |args| {
        let a = args.get("a").and_then(|v| v.as_f64()).unwrap_or(0.0);
        let b = args.get("b").and_then(|v| v.as_f64()).unwrap_or(0.0);

        let result = match operation {
            ArithmeticOp::Add => a + b,
            ArithmeticOp::Subtract => a - b,
            ArithmeticOp::Multiply => a * b,
            ArithmeticOp::Divide => {
                if b == 0.0 {
                    return Err(TemplateError::ValidationError(
                        "Division by zero".to_string(),
                    ));
                }
                a / b
            }
        };

        Ok(Value::Number(
            serde_json::Number::from_f64(result).unwrap_or(serde_json::Number::from(0)),
        ))
    }
}

/// Arithmetic operations for custom functions
#[derive(Debug, Clone, Copy)]
pub enum ArithmeticOp {
    /// Addition
    Add,
    /// Subtraction
    Subtract,
    /// Multiplication
    Multiply,
    /// Division
    Divide,
}

/// Common custom filter implementations
///
/// Create a filter that converts values to uppercase
pub fn uppercase_filter(
) -> impl Fn(&Value, &HashMap<String, Value>) -> Result<Value> + Send + Sync + 'static {
    |value, _args| match value {
        Value::String(s) => Ok(Value::String(s.to_uppercase())),
        _ => Ok(value.clone()),
    }
}

/// Create a filter that converts values to lowercase
pub fn lowercase_filter(
) -> impl Fn(&Value, &HashMap<String, Value>) -> Result<Value> + Send + Sync + 'static {
    |value, _args| match value {
        Value::String(s) => Ok(Value::String(s.to_lowercase())),
        _ => Ok(value.clone()),
    }
}

/// Create a filter that truncates strings
pub fn truncate_filter(
    max_len: usize,
) -> impl Fn(&Value, &HashMap<String, Value>) -> Result<Value> + Send + Sync + 'static {
    move |value, _args| match value {
        Value::String(s) => {
            if s.len() > max_len {
                Ok(Value::String(format!("{}...", &s[..max_len])))
            } else {
                Ok(Value::String(s.clone()))
            }
        }
        _ => Ok(value.clone()),
    }
}

/// Create a filter that joins array elements
pub fn join_filter(
    separator: &str,
) -> impl Fn(&Value, &HashMap<String, Value>) -> Result<Value> + Send + Sync + 'static {
    let separator = separator.to_string();
    move |value, _args| match value {
        Value::Array(arr) => {
            let joined = arr
                .iter()
                .map(|v| match v {
                    Value::String(s) => s.clone(),
                    _ => v.to_string(),
                })
                .collect::<Vec<_>>()
                .join(&separator);
            Ok(Value::String(joined))
        }
        _ => Ok(value.clone()),
    }
}

/// Template engine with custom functions
///
/// A convenience wrapper that includes common custom functions and filters
pub struct ExtendedTemplateRenderer {
    /// Base template renderer
    renderer: TemplateRenderer,
    /// Custom function registry
    registry: FunctionRegistry,
}

impl ExtendedTemplateRenderer {
    /// Create new extended renderer with common custom functions
    pub fn new() -> Result<Self> {
        let mut renderer = TemplateRenderer::new()?;
        let registry = FunctionRegistry::new();

        // Register common custom functions
        Self::register_common_functions(&mut renderer.tera)?;

        Ok(Self { renderer, registry })
    }

    /// Register common custom functions and filters
    fn register_common_functions(tera: &mut Tera) -> Result<()> {
        // String manipulation functions
        register_custom_function(tera, "uppercase", |args| {
            let input = args.get("input").and_then(|v| v.as_str()).unwrap_or("");
            Ok(Value::String(input.to_uppercase()))
        })?;

        register_custom_function(tera, "lowercase", |args| {
            let input = args.get("input").and_then(|v| v.as_str()).unwrap_or("");
            Ok(Value::String(input.to_lowercase()))
        })?;

        // Array manipulation functions
        register_custom_function(tera, "length", |args| {
            let input = args.get("input");
            let len = match input {
                Some(Value::Array(arr)) => arr.len(),
                Some(Value::String(s)) => s.len(),
                Some(Value::Object(obj)) => obj.len(),
                _ => 0,
            };
            Ok(Value::Number(len.into()))
        })?;

        // Date/time functions
        register_custom_function(tera, "now_iso", |_| {
            Ok(Value::String(chrono::Utc::now().to_rfc3339()))
        })?;

        register_custom_function(tera, "timestamp", |_| {
            Ok(Value::Number(chrono::Utc::now().timestamp().into()))
        })?;

        // Utility functions
        register_custom_function(tera, "default", |args| {
            let value = args.get("value");
            let default = args.get("default");
            match (value, default) {
                (Some(v), _) if !v.is_null() => Ok(v.clone()),
                (_, Some(d)) => Ok(d.clone()),
                _ => Ok(Value::Null),
            }
        })?;

        Ok(())
    }

    /// Add custom function
    pub fn add_function<F>(mut self, func: CustomFunction<F>) -> Self
    where
        F: Fn(&HashMap<String, Value>) -> Result<Value> + Send + Sync + 'static,
    {
        self.registry = self.registry.add_function(func);
        self
    }

    /// Add custom filter
    pub fn add_filter<F>(mut self, filter: CustomFilter<F>) -> Self
    where
        F: Fn(&Value, &HashMap<String, Value>) -> Result<Value> + Send + Sync + 'static,
    {
        self.registry = self.registry.add_filter(filter);
        self
    }

    /// Render template string
    pub fn render(&mut self, template: &str, name: &str) -> Result<String> {
        self.renderer.render_str(template, name)
    }

    /// Access the underlying renderer
    pub fn renderer(&self) -> &TemplateRenderer {
        &self.renderer
    }

    /// Access the underlying renderer mutably
    pub fn renderer_mut(&mut self) -> &mut TemplateRenderer {
        &mut self.renderer
    }
}

/// Helper macros for creating custom functions and filters
///
/// Create a custom function with less boilerplate
///
/// # Example
/// ```rust
/// use clnrm_template::{custom_function, register_custom_function};
/// use tera::Tera;
///
/// fn my_function(args: &HashMap<String, Value>) -> Result<Value> {
///     let name = args.get("name").and_then(|v| v.as_str()).unwrap_or("World");
///     Ok(Value::String(format!("Hello, {}!", name)))
/// }
///
/// let mut tera = Tera::default();
/// register_custom_function(&mut tera, "hello", my_function).unwrap();
/// ```
#[macro_export]
macro_rules! custom_function {
    ($name:expr, $func:expr) => {
        $crate::custom::CustomFunction::new($name, $func)
    };
}

/// Create a custom filter with less boilerplate
#[macro_export]
macro_rules! custom_filter {
    ($name:expr, $filter:expr) => {
        $crate::custom::CustomFilter::new($name, $filter)
    };
}

/// Register multiple functions at once
///
/// # Example
/// ```rust
/// use clnrm_template::{register_functions, custom_function};
/// use tera::Tera;
/// use std::collections::HashMap;
/// use serde_json::Value;
///
/// fn func1(args: &HashMap<String, Value>) -> Result<Value> { /* ... */ }
/// fn func2(args: &HashMap<String, Value>) -> Result<Value> { /* ... */ }
///
/// let mut tera = Tera::default();
/// register_functions!(&mut tera, {
///     "my_func1" => func1,
///     "my_func2" => func2,
/// }).unwrap();
/// ```
#[macro_export]
macro_rules! register_functions {
    ($tera:expr, { $($name:expr => $func:expr),* $(,)? }) => {{
        $(
            $crate::custom::register_custom_function($tera, $name, $func)?;
        )*
        Ok::<(), $crate::error::TemplateError>(())
    }};
}

/// Register multiple filters at once
#[macro_export]
macro_rules! register_filters {
    ($tera:expr, { $($name:expr => $filter:expr),* $(,)? }) => {{
        $(
            $crate::custom::register_custom_filter($tera, $name, $filter)?;
        )*
        Ok::<(), $crate::error::TemplateError>(())
    }};
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::Value;
    use std::collections::HashMap;

    #[test]
    fn test_custom_function_registration() {
        let mut tera = Tera::default();

        register_custom_function(&mut tera, "test_func", |args| {
            let input = args.get("input").and_then(|v| v.as_str()).unwrap_or("");
            Ok(Value::String(format!("Processed: {}", input)))
        })
        .unwrap();

        // Test that function is registered (would need actual Tera rendering to test fully)
        assert!(tera.get_function("test_func").is_some());
    }

    #[test]
    fn test_arithmetic_function() {
        let add_func = arithmetic_function(ArithmeticOp::Add);
        let mut args = HashMap::new();
        args.insert("a".to_string(), Value::Number(5.into()));
        args.insert("b".to_string(), Value::Number(3.into()));

        let result = add_func(&args).unwrap();
        assert_eq!(result, Value::Number(8.into()));
    }

    #[test]
    fn test_format_function() {
        let format_func = format_function("Hello {{ name }}, count: {{ count }}");
        let mut args = HashMap::new();
        args.insert("name".to_string(), Value::String("World".to_string()));
        args.insert("count".to_string(), Value::String("42".to_string()));

        let result = format_func(&args).unwrap();
        assert_eq!(result, Value::String("Hello World, count: 42".to_string()));
    }

    #[test]
    fn test_function_registry() {
        let registry = FunctionRegistry::new()
            .add_function(CustomFunction::new("test1", |args| {
                Ok(Value::String("test1".to_string()))
            }))
            .add_filter(CustomFilter::new("test2", |value, _args| Ok(value.clone())));

        assert_eq!(registry.function_count(), 1);
        assert_eq!(registry.filter_count(), 1);
    }

    #[test]
    fn test_extended_renderer() {
        let mut renderer = ExtendedTemplateRenderer::new().unwrap();

        // Test that common functions are available
        assert!(renderer.renderer().has_template("_macros.toml.tera"));

        // Test rendering with extended functions
        let result = renderer
            .render("Hello {{ uppercase(input='world') }}!", "test")
            .unwrap();
        assert_eq!(result, "Hello WORLD!");
    }
}