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
//! Simple API for basic template rendering use cases
//!
//! Provides dead-simple functions for common template rendering scenarios:
//! - `render(template, vars)` - Basic string rendering
//! - `render_file(path, vars)` - File-based rendering
//! - `render_with_context(template, context)` - Advanced rendering with context
//! - `TemplateBuilder` - Fluent API for complex configurations

use crate::context::TemplateContext;
use crate::error::{Result, TemplateError};
use crate::renderer::{render_template, TemplateRenderer};
use serde_json::Value;
use std::collections::HashMap;
use std::path::Path;

/// Render template string with variables (simplest API)
///
/// # Arguments
/// * `template` - Template string with {{ variables }}
/// * `vars` - Variables as key-value pairs
///
/// # Example
/// ```rust
/// use clnrm_template::render;
/// use std::collections::HashMap;
///
/// let mut vars = HashMap::new();
/// vars.insert("name", "World");
/// vars.insert("count", "42");
///
/// let result = render("Hello {{ name }}! Count: {{ count }}", vars).unwrap();
/// assert_eq!(result, "Hello World! Count: 42");
/// ```
pub fn render(template: &str, vars: HashMap<&str, &str>) -> Result<String> {
    let mut json_vars = HashMap::new();
    for (key, value) in vars {
        json_vars.insert(key.to_string(), Value::String(value.to_string()));
    }
    render_template(template, json_vars)
}

/// Render template string with JSON values
///
/// # Arguments
/// * `template` - Template string with {{ variables }}
/// * `vars` - Variables as JSON values
///
/// # Example
/// ```rust
/// use clnrm_template::render_with_json;
/// use std::collections::HashMap;
/// use serde_json::Value;
///
/// let mut vars = HashMap::new();
/// vars.insert("items", Value::Array(vec![
///     Value::String("apple".to_string()),
///     Value::String("banana".to_string())
/// ]));
/// vars.insert("enabled", Value::Bool(true));
///
/// let result = render_with_json("Items: {{ items | join(', ') }}, Enabled: {{ enabled }}", vars).unwrap();
/// ```
pub fn render_with_json(template: &str, vars: HashMap<&str, Value>) -> Result<String> {
    let mut json_vars = HashMap::new();
    for (key, value) in vars {
        json_vars.insert(key.to_string(), value);
    }
    render_template(template, json_vars)
}

/// Render template file with variables
///
/// # Arguments
/// * `path` - Path to template file
/// * `vars` - Variables as key-value pairs
///
/// # Example
/// ```rust
/// use clnrm_template::render_file;
/// use std::collections::HashMap;
///
/// let mut vars = HashMap::new();
/// vars.insert("service", "my-service");
/// vars.insert("port", "8080");
///
/// let result = render_file("templates/config.toml", vars).unwrap();
/// ```
pub fn render_file<P: AsRef<Path>>(path: P, vars: HashMap<&str, &str>) -> Result<String> {
    let mut json_vars = HashMap::new();
    for (key, value) in vars {
        json_vars.insert(key.to_string(), Value::String(value.to_string()));
    }
    crate::renderer::render_template_file(path.as_ref(), json_vars)
}

/// Render template with pre-built context
///
/// # Arguments
/// * `template` - Template string
/// * `context` - Pre-configured template context
///
/// # Example
/// ```rust
/// use clnrm_template::{render_with_context, TemplateContext};
///
/// let context = TemplateContext::with_defaults()
///     .var("service", "my-service")
///     .var("environment", "production");
///
/// let result = render_with_context("Service: {{ service }}, Env: {{ environment }}", &context).unwrap();
/// ```
pub fn render_with_context(template: &str, _context: &TemplateContext) -> Result<String> {
    let mut renderer = TemplateRenderer::new()?;
    renderer.render_str(template, "template")
}

/// Render template to specific output format
///
/// # Arguments
/// * `template` - Template string
/// * `vars` - Variables as key-value pairs
/// * `format` - Desired output format
///
/// # Example
/// ```rust
/// use clnrm_template::{render_to_format, OutputFormat};
/// use std::collections::HashMap;
///
/// let mut vars = HashMap::new();
/// vars.insert("name", "test");
/// vars.insert("value", "123");
///
/// let result = render_to_format("Name: {{ name }}, Value: {{ value }}", vars, OutputFormat::Json).unwrap();
/// ```
pub fn render_to_format(
    template: &str,
    vars: HashMap<&str, &str>,
    format: OutputFormat,
) -> Result<String> {
    let mut json_vars = HashMap::new();
    for (key, value) in vars {
        json_vars.insert(key.to_string(), Value::String(value.to_string()));
    }

    let rendered = render_template(template, json_vars)?;

    match format {
        OutputFormat::Toml => Ok(rendered),
        OutputFormat::Json => convert_to_json(&rendered),
        OutputFormat::Yaml => convert_to_yaml(&rendered),
        OutputFormat::Plain => strip_template_syntax(&rendered),
    }
}

/// Output format for template rendering
#[derive(Debug, Clone, Copy)]
pub enum OutputFormat {
    /// TOML format (default)
    Toml,
    /// JSON format
    Json,
    /// YAML format
    Yaml,
    /// Plain text (remove template syntax)
    Plain,
}

/// Convert TOML to JSON format
pub fn convert_to_json(toml_content: &str) -> Result<String> {
    let parsed: Value = toml::from_str(toml_content).map_err(|e| {
        TemplateError::ValidationError(format!("Failed to parse TOML for JSON conversion: {}", e))
    })?;

    serde_json::to_string_pretty(&parsed)
        .map_err(|e| TemplateError::ValidationError(format!("Failed to serialize to JSON: {}", e)))
}

/// Convert TOML to YAML format
pub fn convert_to_yaml(toml_content: &str) -> Result<String> {
    let parsed: Value = toml::from_str(toml_content).map_err(|e| {
        TemplateError::ValidationError(format!("Failed to parse TOML for YAML conversion: {}", e))
    })?;

    serde_yaml::to_string(&parsed)
        .map_err(|e| TemplateError::ValidationError(format!("Failed to serialize to YAML: {}", e)))
}

/// Strip template syntax to get plain text
pub fn strip_template_syntax(content: &str) -> Result<String> {
    // Simple implementation - remove {{ }} and {% %} blocks
    let mut result = String::new();
    let mut in_braces = false;
    let mut brace_depth = 0;

    for ch in content.chars() {
        match ch {
            '{' => {
                if let Some(next) = content.chars().nth(result.len() + 1) {
                    if next == '{' || next == '%' {
                        in_braces = true;
                        brace_depth = 1;
                        continue;
                    }
                }
            }
            '}' => {
                if in_braces {
                    if let Some(prev) = content.chars().nth(result.len() - 1) {
                        if prev == '}' || prev == '%' {
                            brace_depth -= 1;
                            if brace_depth == 0 {
                                in_braces = false;
                            }
                            continue;
                        }
                    }
                }
            }
            _ => {
                if !in_braces {
                    result.push(ch);
                }
            }
        }
    }

    Ok(result)
}

/// Template builder for fluent configuration
///
/// Provides a simple, chainable API for template rendering:
///
/// ```rust
/// use clnrm_template::TemplateBuilder;
///
/// let result = TemplateBuilder::new()
///     .template("Hello {{ name }}!")
///     .variable("name", "World")
///     .variable("count", "42")
///     .format(OutputFormat::Plain)
///     .render()
///     .unwrap();
/// ```
pub struct TemplateBuilder {
    template: Option<String>,
    variables: HashMap<String, Value>,
    format: OutputFormat,
    context: Option<TemplateContext>,
}

impl Default for TemplateBuilder {
    fn default() -> Self {
        Self {
            template: None,
            variables: HashMap::new(),
            format: OutputFormat::Toml,
            context: None,
        }
    }
}

impl TemplateBuilder {
    /// Create new template builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Set template content
    pub fn template<S: Into<String>>(mut self, template: S) -> Self {
        self.template = Some(template.into());
        self
    }

    /// Add string variable
    pub fn variable<K: Into<String>, V: Into<String>>(mut self, key: K, value: V) -> Self {
        self.variables
            .insert(key.into(), Value::String(value.into()));
        self
    }

    /// Add JSON variable
    pub fn json_variable<K: Into<String>>(mut self, key: K, value: Value) -> Self {
        self.variables.insert(key.into(), value);
        self
    }

    /// Add multiple variables at once
    pub fn variables<I, K, V>(mut self, vars: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: Into<String>,
        V: Into<String>,
    {
        for (key, value) in vars {
            self.variables
                .insert(key.into(), Value::String(value.into()));
        }
        self
    }

    /// Set output format
    pub fn format(mut self, format: OutputFormat) -> Self {
        self.format = format;
        self
    }

    /// Set custom context
    pub fn context(mut self, context: TemplateContext) -> Self {
        self.context = Some(context);
        self
    }

    /// Render template
    pub fn render(self) -> Result<String> {
        let template = self
            .template
            .ok_or_else(|| TemplateError::ValidationError("No template provided".to_string()))?;

        if let Some(context) = self.context {
            render_with_context(&template, &context)
        } else {
            let rendered = render_template(&template, self.variables)?;
            match self.format {
                OutputFormat::Toml => Ok(rendered),
                OutputFormat::Json => convert_to_json(&rendered),
                OutputFormat::Yaml => convert_to_yaml(&rendered),
                OutputFormat::Plain => strip_template_syntax(&rendered),
            }
        }
    }

    /// Render template file
    pub fn render_file<P: AsRef<Path>>(self, path: P) -> Result<String> {
        let _template = self
            .template
            .ok_or_else(|| TemplateError::ValidationError("No template provided".to_string()))?;

        let mut json_vars = HashMap::new();
        for (key, value) in self.variables {
            json_vars.insert(key, value);
        }

        let result = crate::renderer::render_template_file(path.as_ref(), json_vars)?;

        match self.format {
            OutputFormat::Toml => Ok(result),
            OutputFormat::Json => convert_to_json(&result),
            OutputFormat::Yaml => convert_to_yaml(&result),
            OutputFormat::Plain => strip_template_syntax(&result),
        }
    }
}

/// Quick template rendering functions for common patterns
pub mod quick {
    use super::*;

    /// Render a simple greeting template
    pub fn greeting(name: &str) -> String {
        render(
            "Hello {{ name }}!",
            [("name", name)].iter().cloned().collect(),
        )
        .unwrap_or_default()
    }

    /// Render a configuration template
    pub fn config(service: &str, port: u16) -> String {
        render(
            "[service]\nname = \"{{ service }}\"\nport = {{ port }}",
            [("service", service), ("port", &port.to_string())]
                .iter()
                .cloned()
                .collect(),
        )
        .unwrap_or_default()
    }

    /// Render a JSON template
    pub fn json_template(name: &str, value: &str) -> String {
        render_to_format(
            "{\"name\": \"{{ name }}\", \"value\": \"{{ value }}\"}",
            [("name", name), ("value", value)].iter().cloned().collect(),
            OutputFormat::Json,
        )
        .unwrap_or_default()
    }

    /// Render a YAML template
    pub fn yaml_template(title: &str, items: Vec<&str>) -> String {
        let _items_str = items.join("\", \"");
        render_to_format(
            "title: {{ title }}\nitems:\n  - \"{{ items | join('\",\n  - \"') }}\"",
            [("title", title)].iter().cloned().collect(),
            OutputFormat::Yaml,
        )
        .unwrap_or_default()
    }
}

/// Template macros for compile-time template rendering
///
/// These macros allow embedding template rendering at compile time:
///
/// ```rust
/// const CONFIG: &str = template!("service = \"{{ name }}\"", name = "my-service");
/// ```
#[macro_export]
macro_rules! template {
    ($template:expr) => {
        $template
    };

    ($template:expr, $($key:ident = $value:expr),* $(,)?) => {{
        let mut vars = std::collections::HashMap::new();
        $(
            vars.insert(stringify!($key).to_string(), serde_json::Value::String($value.to_string()));
        )*
        $crate::render_template($template, vars).unwrap_or_else(|_| $template.to_string())
    }};
}

/// Template literals for embedded templates
///
/// ```rust
/// use clnrm_template::template_literal;
///
/// const TEMPLATE: &str = template_literal!("Hello {{ name }}!");
/// ```
#[macro_export]
macro_rules! template_literal {
    ($template:expr) => {
        $template
    };
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_simple_render() {
        let result = render(
            "Hello {{ name }}!",
            [("name", "World")].iter().cloned().collect(),
        )
        .unwrap();
        assert_eq!(result, "Hello World!");
    }

    #[test]
    fn test_render_with_json() {
        let mut vars = HashMap::new();
        vars.insert(
            "items",
            Value::Array(vec![
                Value::String("apple".to_string()),
                Value::String("banana".to_string()),
            ]),
        );

        let result = render_with_json("Items: {{ items | length }}", vars).unwrap();
        // Note: This would need the length filter to be implemented
        assert!(result.contains("Items:"));
    }

    #[test]
    fn test_template_builder() {
        let result = TemplateBuilder::new()
            .template("Service: {{ service }}, Port: {{ port }}")
            .variable("service", "my-service")
            .variable("port", "8080")
            .render()
            .unwrap();

        assert_eq!(result, "Service: my-service, Port: 8080");
    }

    #[test]
    fn test_output_formats() {
        let toml_result = render_to_format(
            "name = \"{{ name }}\"",
            [("name", "test")].iter().cloned().collect(),
            OutputFormat::Toml,
        )
        .unwrap();
        assert_eq!(toml_result, "name = \"test\"");

        let json_result = render_to_format(
            "{\"name\": \"{{ name }}\"}",
            [("name", "test")].iter().cloned().collect(),
            OutputFormat::Json,
        )
        .unwrap();
        assert!(json_result.contains("\"name\""));
        assert!(json_result.contains("\"test\""));
    }

    #[test]
    fn test_quick_templates() {
        let greeting = quick::greeting("Alice");
        assert_eq!(greeting, "Hello Alice!");

        let config = quick::config("web-server", 3000);
        assert!(config.contains("web-server"));
        assert!(config.contains("3000"));
    }
}