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
use std::convert::TryInto;

use tera::{Context, Tera};

use crate::{
    errors::{DevrcError, DevrcResult},
    scope::Scope,
};

pub fn render_string(name: &str, template: &str, scope: &Scope) -> DevrcResult<String> {
    let context: Context = scope.try_into()?;

    let _autoescape = true;

    // TODO: pass tera as input parameter
    let mut tera = Tera::default();

    // if autoescape {
    //     tera.autoescape_on(vec![ONE_OFF_TEMPLATE_NAME]);
    // }

    tera.add_raw_template(name, template)?;
    let result = tera.render(name, &context);
    tera.templates.remove(name);

    match result {
        Ok(value) => Ok(value),
        Err(error) => Err(error.into()), // Err(value) => {
                                         //     // TODO: wrap Tera error
                                         //     // println!("Render template error: {:}", value);
                                         //     Err(DevrcError::RenderError(value))
                                         // }
    }
}

pub fn render_multiple(
    input: indexmap::IndexMap<String, String>,
    scope: &Scope,
) -> DevrcResult<indexmap::IndexMap<String, String>> {
    let mut result = indexmap::IndexMap::new();
    let _autoescape = true;
    let mut context: Context = scope.try_into()?;

    let mut tera = Tera::default();

    for (template_name, template_value) in input.iter() {
        tera.add_raw_template(template_name, template_value)?;
        let rendered_template = tera
            .render(template_name, &context)
            .map_err(DevrcError::RenderError)?;
        tera.templates.remove(template_name);

        context
            .try_insert(template_name, &rendered_template)
            .map_err(DevrcError::RenderError)?;

        result.insert(template_name.to_string(), rendered_template.to_string());
    }
    Ok(result)
}

#[cfg(test)]
mod tests {
    use std::convert::TryFrom;

    use super::*;
    use tera::{Error as TeraError, ErrorKind as TerraErrorKind};

    use crate::{
        errors::DevrcError,
        scope::Scope,
        variables::{VariableKey, VariableValue},
    };

    #[test]
    fn test_render_string() {
        let mut scope = Scope::default();
        scope.insert_var(
            VariableKey::try_from("name".to_string()).unwrap(),
            VariableValue::new("name", "username")
                .with_render_value(&scope)
                .unwrap(),
        );
        let rendered_template =
            render_string("var_name", "some template string: {{ name }}", &scope).unwrap();

        dbg!(&rendered_template);

        assert_eq!(
            rendered_template,
            "some template string: username".to_owned()
        );
    }

    #[test]
    fn test_render_invalid_template() {
        let rendered_template =
            render_string("var_name", "some template {{ } string", &Scope::default());

        assert!(rendered_template.is_err());

        match rendered_template.err().unwrap() {
            DevrcError::RenderError(TeraError {
                kind: TerraErrorKind::Msg(kind),
                ..
            }) => {
                assert_eq!(kind, "Failed to parse \'var_name\'");
            }
            _ => unreachable!(),
        }
    }
}