terarium 0.3.0

Wrapper for the Tera template system with template grouping and bulk rendering of templates.
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
use std::borrow::Borrow;
use std::collections::HashMap;
use std::hash::Hash;

use tera::{Context, Error as TeraError};
use tera::Tera;
use thiserror::Error;

use crate::Template;

/// Wrapper over the `Tera` templating engine with capability of template bulk rendering.
/// Each template can exists in more than one version (support for multi-language templates).
/// An instance of the `Terarium` is built with the `TerariumBuilder`.
#[derive(Clone, Default)]
pub struct Terarium {
    /// Internal Tera template
    tera: Tera,
    /// Template by template key lookup.
    template_map: HashMap<String, HashMap<String, String>>,
    /// Group by group key lookup.
    groups: HashMap<String, HashMap<String, String>>,
}

impl Terarium {
    /// Render single template identified by its key.
    /// The `Tera` context is accepted for rendering.
    pub fn render_template<K: ?Sized, LK: ?Sized>(
        &self,
        context: &Context,
        template_key: &K,
        language: &LK,
        fallback_language: Option<&LK>,
    ) -> Result<String, TerariumError>
        where
            String: Borrow<K>,
            String: Borrow<LK>,
            K: Hash + Eq,
            LK: Hash + Eq,
    {
        let template = self
            .template_map.get(template_key).ok_or_else(|| TerariumError::TemplateNotFound)?;
        let content_key = template
            .get(language)
            .or_else(|| {
                fallback_language.map(|k| template.get(k)).flatten()
            })
            .ok_or_else(|| TerariumError::LanguageNotFound)?;
        Ok(self.tera.render(content_key.as_str(), context)?)
    }

    /// Render template group.
    /// Result is HashMap where keys are member names and values are rendered templates.
    pub fn render_group<K: ?Sized, LK: ?Sized>(
        &self,
        context: &Context,
        group_key: &K,
        language: &LK,
        fallback_language: Option<&LK>,
    ) -> Result<HashMap<String, String>, TerariumError>
        where
            String: Borrow<K>,
            String: Borrow<LK>,
            K: Hash + Eq,
            LK: Hash + Eq,
    {
        let group = self.groups.get(group_key).ok_or_else(|| TerariumError::GroupNotFound)?;
        let mut result = HashMap::<String, String>::new();

        for (member_key, template_key) in group.iter() {
            let content = self.render_template(context, template_key, language, fallback_language)?;
            result.insert(member_key.clone(), content);
        }

        Ok(result)
    }
}


/// Errors returned by `Terarium` operations.
#[derive(Debug, Error)]
pub enum TerariumError {
    /// Requested template was not found.
    #[error("There is no template")]
    TemplateNotFound,
    /// Request language was not found for template.
    #[error("Language not found")]
    LanguageNotFound,
    /// Requested group was not found.
    #[error("There is no group")]
    GroupNotFound,

    /// Error propagated from underlying `Tera` instance.
    #[error("Error when rendering template")]
    RenderingFailed(TeraError),
}


impl From<TeraError> for TerariumError {
    fn from(value: TeraError) -> Self {
        Self::RenderingFailed(value)
    }
}


/// Build the `Terarium` instance.
#[derive(Default)]
pub struct TerariumBuilder {
    templates: HashMap<String, Template>,
    groups: HashMap<String, HashMap<String, String>>,
}


impl TerariumBuilder {
    /// Add new template to the new instance.
    /// If template exist, it will be replaced
    pub fn add_template(&mut self, key: String, template: Template) -> Result<(), TerariumBuilderError> {
        self.templates.insert(key.clone(), template);
        Ok(())
    }

    /// Add new group into new instance
    /// If group with same name exists, it is replaced.
    pub fn add_group(&mut self, key: String, group: HashMap<String, String>) -> Result<(), TerariumBuilderError> {
        // Check templates exist
        for (_, tpl_name) in group.iter() {
            if !self.templates.contains_key(tpl_name) {
                return Err(TerariumBuilderError::TemplateNotFound(tpl_name.to_owned()));
            }
        }

        // Add group to lookup
        self.groups.insert(key, group);
        Ok(())
    }

    /// Build new `Terarium` instance based on stored templates and groups.
    pub fn build(self) -> Result<Terarium, TerariumBuilderError> {
        let mut instance = Terarium::default();
        let mut tera_template_id: u32 = 1;

        // build templates
        self.templates.into_iter().try_for_each(|(template_key, template)| {
            template.collect_contents().into_iter().try_for_each(|content| {
                let template_name = content.name.unwrap_or_else(|| format!("template#{}", tera_template_id));
                tera_template_id += 1;
                instance.tera.add_raw_template(&template_name, &content.content)?;

                content.languages.into_iter().for_each(|language_key| {
                    instance
                        .template_map
                        .entry(template_key.clone())
                        .or_default()
                        .insert(language_key.clone(), template_name.clone());
                });

                Ok::<_, TerariumBuilderError>(())
            })?;
            Ok::<_, TerariumBuilderError>(())
        })?;

        instance.groups = self.groups;
        Ok(instance)
    }
}


/// Simplify building template groups.
#[derive(Clone, Default)]
pub struct TemplateGroupBuilder {
    group: HashMap<String, String>,
}

impl TemplateGroupBuilder {
    /// Add new member to group.
    pub fn add_member(mut self, member_key: String, template_key: String) -> Self {
        self.group.insert(member_key, template_key);
        self
    }

    /// Build the group spec.
    pub fn build(self) -> HashMap<String, String> {
        self.group
    }
}


/// Errors returned by `TerariumBuilder` struct.
#[derive(Debug, Error)]
pub enum TerariumBuilderError {
    /// Cannot build template in underlying `Tera` instance.
    #[error("Unable to build template")]
    TemplateBuildingError(TeraError),
    /// Template was not found (when building group).
    #[error("Cannot build template groups - some templates are missing")]
    TemplateNotFound(String),
}


impl From<TeraError> for TerariumBuilderError {
    fn from(value: TeraError) -> Self {
        Self::TemplateBuildingError(value)
    }
}


/// Additional methods for testing
#[cfg(test)]
impl TerariumBuilder {
    /// Get template defined by its `key`.
    /// If no template defined by given `key` exist, return `None`.
    pub fn get_template(&mut self, key: &String) -> Option<&mut Template> {
        self.templates.get_mut(key)
    }

    /// Remove template defined by the `key` from the builder and return it.
    /// Returns `None` if no template with given `key` is defined.
    pub fn remove_template(&mut self, key: &String) -> Option<Template> {
        self.templates.remove(key)
    }

    /// Get group defined by the `key`.
    /// Return `None` if no group defined by the `key` is found.
    pub fn get_group(&mut self, key: &String) -> Option<&mut HashMap<String, String>> {
        self.groups.get_mut(key)
    }

    /// Remove group defined by the `key` from the builder and return it.
    /// Returns `None` if no group with given `key` is defined.
    pub fn remove_group(&mut self, key: &String) -> Option<HashMap<String, String>> {
        self.groups.remove(key)
    }
}


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

    mod terarium_builder {
        use crate::Content;

        use super::*;

        #[test]
        fn add_template() {
            let mut instance = make_instance();

            let mut tpl = Template::default();
            tpl.add_content(Content::new("foo".to_string(), vec!["1".to_owned(), "2".to_owned()])).unwrap();

            instance.add_template(
                "1".to_owned(),
                tpl,
            ).unwrap();

            assert_eq!(instance.templates.len(), 1);
            let template = instance.templates["1"].clone();
            let contents = template.collect_contents();
            assert_eq!(contents.len(), 1);
        }

        #[test]
        fn group_manipulation() {
            let mut instance = make_instance();
            instance.add_template("1".to_owned(), Template::default()).unwrap();
            instance.add_group("1".to_owned(), TemplateGroupBuilder::default().add_member("1".to_owned(), "1".to_owned()).build()).unwrap();
            let grp = instance.get_group(&"1".to_owned());
            assert!(grp.is_some());
            let grp = grp.unwrap();
            assert_eq!(grp.clone(), HashMap::<String, String>::from([("1".to_owned(), "1".to_owned())]));

            instance.remove_group(&"1".to_owned());
            assert!(instance.get_group(&"1".to_owned()).is_none())
        }

        #[test]
        fn check_group_configuration() {
            let mut instance = make_instance();
            instance.add_template("1".to_owned(), Template::default()).unwrap();
            instance.add_template("2".to_owned(), Template::default()).unwrap();
            let result = instance.add_group(
                "100".to_owned(),
                TemplateGroupBuilder::default()
                    .add_member("10".to_owned(), "1".to_owned())
                    .add_member("20".to_owned(), "2".to_owned())
                    .add_member("30".to_owned(), "3".to_owned())
                    .build(),
            );
            assert!(result.is_err())
        }

        fn make_instance() -> TerariumBuilder {
            TerariumBuilder::default()
        }
    }

    mod terarium {
        use crate::Content;

        use super::*;

        #[test]
        fn render_template() {
            let instance = make_instance();
            let ctx = make_context();
            let result_a = instance.render_template(&ctx, "template_a", "cs", None).unwrap();
            assert_eq!(result_a, "template_a cs john");
        }

        #[test]
        fn render_template_with_fallback() {
            let instance = make_instance();
            let ctx = make_context();
            let result_a = instance.render_template(&ctx, "template_a", "de", Some("en")).unwrap();
            assert_eq!(result_a, "template_a en john");
        }

        #[test]
        fn render_template_without_matching_language() {
            let instance = make_instance();
            let ctx = make_context();
            let result = instance.render_template(&ctx, "template_a", "de", Some("fr"));

            assert!(match result.unwrap_err() {
                TerariumError::LanguageNotFound => true,
                _ => false
            })
        }

        #[test]
        fn render_group() {
            let instance = make_instance();
            let context = make_context();
            let group_result = instance.render_group(&context, "group_a", "en", None);
            assert!(group_result.is_ok());
            let group_result = group_result.unwrap();
            assert_eq!(group_result.get("A").unwrap(), "template_a en john");
            assert_eq!(group_result.get("B").unwrap(), "template_b en doe");
        }

        #[test]
        fn render_group_with_fallback() {
            let instance = make_instance();
            let context = make_context();
            let group_result = instance.render_group(&context, "group_a", "cs", Some("en"));
            assert!(group_result.is_ok());
            let group_result = group_result.unwrap();
            assert_eq!(group_result.get("A").unwrap(), "template_a cs john");
            assert_eq!(group_result.get("B").unwrap(), "template_b en doe");
        }

        #[test]
        fn render_group_when_invalid_language() {
            let instance = make_instance();
            let context = make_context();
            let group_result = instance.render_group(&context, "group_a", "cs", Some("fr"));
            assert!(group_result.is_err());
            assert!(match group_result.unwrap_err() {
                TerariumError::LanguageNotFound => true,
                _ => false
            })
        }

        #[test]
        fn render_nested_templates() {
            let mut builder = TerariumBuilder::default();
            let mut tpl_a = Template::default();
            tpl_a.add_content(
                Content::new("This is content {{value_1}} {% include 'tpl_b_cs' %}".to_owned(), vec!["cs".to_owned()])
            ).unwrap();
            builder.add_template("tpl_a".to_owned(), tpl_a).unwrap();

            let mut tpl_b = Template::default();
            tpl_b.add_content(
                Content::new_named("This is nested {{value_2}}".to_owned(), vec!["cs".to_owned()], "tpl_b_cs".to_owned())
            ).unwrap();
            builder.add_template("tpl_b".to_owned(), tpl_b).unwrap();

            let instance = builder.build().unwrap();
            let mut ctx = Context::default();
            ctx.insert("value_1", "foo");
            ctx.insert("value_2", "bar");

            let result = instance.render_template(&ctx, "tpl_a", "cs", None).unwrap();
            assert_eq!(result.as_str(), "This is content foo This is nested bar");
        }

        fn make_instance() -> Terarium {
            let mut builder = TerariumBuilder::default();

            let mut tpl_a = Template::default();
            tpl_a.add_content(Content::new("template_a cs {{name}}".to_owned(), vec!["cs".to_owned()])).unwrap();
            tpl_a.add_content(Content::new("template_a en {{name}}".to_owned(), vec!["en".to_owned()])).unwrap();

            let mut tpl_b = Template::default();
            tpl_b.add_content(Content::new("template_b en {{surname}}".to_owned(), vec!["en".to_owned()])).unwrap();

            builder.add_template("template_a".to_owned(), tpl_a).unwrap();
            builder.add_template("template_b".to_owned(), tpl_b).unwrap();

            builder.add_group(
                "group_a".to_owned(),
                TemplateGroupBuilder::default()
                    .add_member("A".to_owned(), "template_a".to_owned())
                    .add_member("B".to_owned(), "template_b".to_owned())
                    .build(),
            ).unwrap();
            builder.build().unwrap()
        }

        fn make_context() -> Context {
            let mut ctx = Context::default();
            ctx.insert("name", "john");
            ctx.insert("surname", "doe");
            ctx
        }
    }
}