tera 0.1.1

Jinja2/Django templates in pure Rust
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
use std::f32::EPSILON;
use serde_json::value::{Value as Json, from_value, to_value};

use context::{Context, JsonRender, JsonNumber, JsonTruthy};
use lexer::TokenType;
use nodes::Node;
use nodes::SpecificNode::*;
use template::Template;
use errors::{TeraResult, field_not_found, not_a_number, not_an_array};


// we need to have some data in the renderer for when we are in a ForLoop
// For example, accessing the local variable would fail when
// looking it up in the context
#[derive(Debug)]
struct ForLoop {
    variable_name: String,
    current: usize,
    values: Vec<Json>
}
impl ForLoop {
    pub fn new(local: String, values: Vec<Json>) -> ForLoop {
        ForLoop {
            variable_name: local,
            current: 0,
            values: values
        }
    }

    pub fn increment(&mut self) {
        self.current += 1;
    }

    pub fn get(&self) -> Option<&Json> {
        self.values.get(self.current)
    }

    pub fn len(&self) -> usize {
        self.values.len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

#[derive(Debug)]
pub struct Renderer<'a> {
    context: Json,
    current: &'a Template,
    parent: Option<&'a Template>,
    for_loops: Vec<ForLoop>
}

impl<'a> Renderer<'a> {
    pub fn new(current: &'a Template, parent: Option<&'a Template>, context: Context) -> Renderer<'a> {
        Renderer {
            current: current,
            parent: parent,
            context: context.as_json(),
            for_loops: vec![],
        }
    }

    // Lookup a variable name from the context and takes into
    // account for loops variables
    fn lookup_variable(&self, key: &str) -> TeraResult<Json> {
        // Look in the plain context if we aren't in a for loop
        if self.for_loops.is_empty() {
            return self.context.lookup(key).cloned().ok_or_else(|| field_not_found(key));
        }

        for for_loop in self.for_loops.iter().rev() {
            if key.starts_with(&for_loop.variable_name) {
                let value = match for_loop.get() {
                    Some(f) => f,
                    None => { return Ok(to_value(&"")); }
                };

                // might be a struct or some nested structure
                if key.contains('.') {
                    let new_key = key.split_terminator('.').skip(1).collect::<Vec<&str>>().join(".");
                    return value.lookup(&new_key).cloned().ok_or_else(|| field_not_found(key));
                } else {
                    return Ok(value.clone());
                }
            } else {
                match key {
                    "loop.index" => { return Ok(to_value(&(for_loop.current + 1))); },
                    "loop.index0" => { return Ok(to_value(&for_loop.current)); },
                    "loop.first" => { return Ok(to_value(&(for_loop.current == 0))); },
                    "loop.last" => { return Ok(to_value(&(for_loop.current == for_loop.len() - 1))); },
                    _ => ()
                };
            }
        }

        // dummy statement to satisfy the compiler
        // TODO: make it so that's not needed
        self.context.lookup(key).cloned().ok_or_else(|| field_not_found(key))
    }

    fn eval_math(&self, node: &Node) -> TeraResult<f32> {
        match node.specific {
            Identifier(ref s) => {
                let value = try!(self.lookup_variable(s));
                match value.to_number() {
                    Ok(v) =>  Ok(v),
                    Err(_) => Err(not_a_number(s))
                }
            },
            Int(s) => Ok(s as f32),
            Float(s) => Ok(s),
            Math { ref lhs, ref rhs, ref operator } => {
                let l = try!(self.eval_math(lhs));
                let r = try!(self.eval_math(rhs));
                let mut result = match *operator {
                    TokenType::Multiply => l * r,
                    TokenType::Divide => l / r,
                    TokenType::Add => l + r,
                    TokenType::Substract => l - r,
                    _ => unreachable!()
                };
                // TODO: fix properly
                // TODO: add tests for float maths arithmetics
                if result.fract() < 0.01 {
                    result = result.round();
                }
                Ok(result)
            }
            _ => unreachable!()
        }
    }

    // TODO: clean up this, too ugly right now for the == and != nodes
    fn eval_condition(&self, node: &Node) -> TeraResult<bool> {
        match node.specific {
            // Simple truthiness check
            Identifier(ref n) => {
                let value = try!(self.lookup_variable(n));
                Ok(value.is_truthy())
            },
            Logic { ref lhs, ref rhs, ref operator } => {
                match *operator {
                    TokenType::Or => {
                        let result = try!(self.eval_condition(lhs)) || try!(self.eval_condition(rhs));
                        return Ok(result);
                    },
                    TokenType::And => {
                        let result = try!(self.eval_condition(lhs)) && try!(self.eval_condition(rhs));
                        return Ok(result);
                    },
                    TokenType::GreaterOrEqual | TokenType::Greater
                    | TokenType::LowerOrEqual | TokenType::Lower => {
                        let l = try!(self.eval_math(lhs));
                        let r = try!(self.eval_math(rhs));
                        let result = match *operator {
                            TokenType::GreaterOrEqual => l >= r,
                            TokenType::Greater => l > r,
                            TokenType::LowerOrEqual => l <= r,
                            TokenType::Lower => l < r,
                            _ => unreachable!()
                        };
                        return Ok(result);
                    },
                    // This is quite different from the other operators
                    // TODO: clean this up, this is ugly
                    TokenType::Equal | TokenType::NotEqual => {
                        match lhs.specific {
                            Logic { .. } => {
                                // let l = self.eval_condition(lhs);
                                // TODO: rhs MUST be bool like
                                panic!("Unimplemented");
                            },
                            Identifier(ref n) => {
                                let l = try!(self.lookup_variable(n));
                                // who knows what rhs is
                                // Here goes a whole new level of ugliness
                                match rhs.specific {
                                    Identifier(ref i) => {
                                        let r = try!(self.lookup_variable(i));
                                        let result = match *operator {
                                            TokenType::Equal => l == r,
                                            TokenType::NotEqual => l != r,
                                            _ => unreachable!()
                                        };
                                        return Ok(result);
                                    },
                                    Int(r) => {
                                        let l2: i32 = match from_value(l.clone()) {
                                            Ok(k) => k,
                                            Err(_) => { return Err(not_a_number(n)); }
                                        };
                                        let result = match *operator {
                                            TokenType::Equal => l2 == r,
                                            TokenType::NotEqual => l2 != r,
                                            _ => unreachable!()
                                        };
                                        return Ok(result);
                                    },
                                    Float(r) => {
                                        let l2: f32 = match from_value(l.clone()) {
                                            Ok(k) => k,
                                            Err(_) => { return Err(not_a_number(n)); }
                                        };
                                        let result = match *operator {
                                            TokenType::Equal => (l2 - r).abs() < EPSILON,
                                            TokenType::NotEqual => (l2 - r).abs() > EPSILON,
                                            _ => unreachable!()
                                        };
                                        return Ok(result);
                                    },
                                    _ => unreachable!()
                                }
                            },
                            Int(n) => {
                                // rhs MUST be a number
                                let l = n as f32; // TODO: that's going to cause issues
                                let r = try!(self.eval_math(rhs));
                                let result = match *operator {
                                    TokenType::Equal => (l - r).abs() < EPSILON,
                                    TokenType::NotEqual => (l - r).abs() > EPSILON,
                                    _ => unreachable!()
                                };
                                return Ok(result);
                            },
                            Float(l) => {
                                // rhs MUST be a number
                                let r = try!(self.eval_math(rhs));
                                let result = match *operator {
                                    TokenType::Equal => (l - r).abs() < EPSILON,
                                    TokenType::NotEqual => (l - r).abs() > EPSILON,
                                    _ => unreachable!()
                                };
                                return Ok(result);
                            },
                            Math { .. } => {
                                // rhs MUST be a number
                                let l = try!(self.eval_math(lhs));
                                let r = try!(self.eval_math(rhs));
                                let result = match *operator {
                                    TokenType::Equal => (l - r).abs() < EPSILON,
                                    TokenType::NotEqual => (l - r).abs() > EPSILON,
                                    _ => unreachable!()
                                };
                                return Ok(result);
                            },
                            _ => unreachable!()
                        }
                    },
                    _ => unreachable!()
                }
                Ok(false)
            },
            _ => unreachable!()
        }
    }

    // eval all the values in a  {{ }} block
    fn render_variable_block(&mut self, node: Node) -> TeraResult<String>  {
        match node.specific {
            Identifier(ref s) => {
                let value = try!(self.lookup_variable(s));
                Ok(value.render())
            },
            Math { .. } => {
                let result = try!(self.eval_math(&node));
                Ok(result.to_string())
            }
            _ => unreachable!()
        }
    }

    // evaluates conditions and render bodies accordingly
    fn render_if(&mut self, condition_nodes: Vec<Box<Node>>, else_node: Option<Box<Node>>) -> TeraResult<String> {
        let mut skip_else = false;
        let mut output = String::new();
        for node in condition_nodes {
            match node.specific {
                Conditional {ref condition, ref body } => {
                    if try!(self.eval_condition(condition)) {
                        skip_else = true;
                        output.push_str(&&try!(self.render_node(*body.clone())));
                    }
                },
                _ => unreachable!()
            }
        }


        if skip_else {
            return Ok(output);
        }

        if let Some(e) = else_node {
            output.push_str(&&try!(self.render_node(*e)));
        };

        Ok(output)
    }

    fn render_for(&mut self, local: Node, array: Node, body: Box<Node>) -> TeraResult<String> {
        let local_name = match local.specific {
            Identifier(s) => s,
            _ => unreachable!()
        };
        let array_name = match array.specific {
            Identifier(s) => s,
            _ => unreachable!()
        };

        let list = try!(self.lookup_variable(&array_name));

        if !list.is_array() {
            return Err(not_an_array(&array_name));
        }

        // Safe unwrap
        let deserialized = list.as_array().unwrap();
        let length = deserialized.len();
        self.for_loops.push(ForLoop::new(local_name, deserialized.clone()));
        let mut i = 0;
        let mut output = String::new();
        loop {
            output.push_str(&&try!(self.render_node(*body.clone())));
            // Safe unwrap
            self.for_loops.last_mut().unwrap().increment();
            if length == 0 || i == length - 1 {
                break;
            }
            i += 1;
        }
        // Trim right at the end of the loop.
        // Can't be done in the parser as it would remove all newlines between
        // loops
        output = output.trim_right().to_owned();

        Ok(output)
    }

    pub fn render_node(&mut self, node: Node) -> TeraResult<String> {
        match node.specific {
            Text(s) => Ok(s),
            VariableBlock(s) => self.render_variable_block(*s),
            If {ref condition_nodes, ref else_node} => {
                self.render_if(condition_nodes.clone(), else_node.clone())
            },
            List(body) => {
                let mut output = String::new();
                for n in body {
                    output.push_str(&&try!(self.render_node(*n)));
                }
                Ok(output)
            },
            For {local, array, body} => {
                self.render_for(*local, *array, body)
            },
            Block {ref name, ref body} => {
                match self.current.blocks.get(name) {
                    Some(b) => {
                        match b.specific {
                            Block {ref body, ..} => {
                                return self.render_node(*body.clone());
                            },
                            _ => unreachable!()
                        }
                    },
                    None => {
                        return self.render_node(*body.clone());
                    }
                };
            },
            _ => unreachable!()
        }
    }

    pub fn render(&mut self) -> TeraResult<String> {
        let children = if self.parent.is_none() {
            self.current.ast.get_children()
        } else {
            // unwrap is safe here as we checked the template exists beforehand
            self.parent.unwrap().ast.get_children()
        };

        let mut output = String::new();
        for node in children {
            // TODO: not entirely sure why i need to && instead of &
            output.push_str(&&try!(self.render_node(*node)));
        }

        Ok(output)
    }
}

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

    use template::Template;
    use context::Context;

    #[test]
    fn test_render_simple_string() {
        let result = Template::new("", "<h1>Hello world</h1>").render(Context::new(), HashMap::new());
        assert_eq!(result.unwrap(), "<h1>Hello world</h1>".to_owned());
    }

    #[test]
    fn test_render_math() {
        let result = Template::new("", "This is {{ 2000 + 16 }}.").render(Context::new(), HashMap::new());
        assert_eq!(result.unwrap(), "This is 2016.".to_owned());
    }

    #[test]
    fn test_render_basic_variable() {
        let mut context = Context::new();
        context.add("name", &"Vincent");

        let result = Template::new("", "My name is {{ name }}.").render(context, HashMap::new());
        assert_eq!(result.unwrap(), "My name is Vincent.".to_owned());
    }

    #[test]
    fn test_render_math_with_variable() {
        let mut context = Context::new();
        context.add("vat_rate", &0.20);

        let result = Template::new("", "Vat: £{{ 100 * vat_rate }}.").render(context, HashMap::new());
        assert_eq!(result.unwrap(), "Vat: £20.".to_owned());
    }

    #[test]
    fn test_render_if_simple() {
        let mut context = Context::new();
        context.add("is_admin", &true);

        let result = Template::new("", "{% if is_admin %}Admin{% endif %}").render(context, HashMap::new());
        assert_eq!(result.unwrap(), "Admin".to_owned());
    }

    #[test]
    fn test_render_if_or_conditions() {
        let mut context = Context::new();
        context.add("is_adult", &false);
        context.add("age", &18);

        let result = Template::new(
            "",
            "{% if is_adult || age + 1 > 18 %}Adult{% endif %}"
        ).render(context, HashMap::new());
        assert_eq!(result.unwrap(), "Adult".to_owned());
    }

    #[test]
    fn test_render_if_and_conditions_with_equality() {
        let mut context = Context::new();
        context.add("is_adult", &true);
        context.add("age", &18);

        let result = Template::new(
            "", "{% if is_adult && age == 18 %}Adult{% endif %}"
        ).render(context, HashMap::new());
        assert_eq!(result.unwrap(), "Adult".to_owned());
    }

    #[test]
    fn test_render_basic_for() {
        let mut context = Context::new();
        context.add("data", &vec![1,2,3]);

        let result = Template::new(
            "", "{% for i in data %}{{i}}{% endfor %}"
        ).render(context, HashMap::new());
        assert_eq!(result.unwrap(), "123".to_owned());
    }

    #[test]
    fn test_render_loop_variables() {
        let mut context = Context::new();
        context.add("data", &vec![1,2,3]);

        let result = Template::new(
            "",
            "{% for i in data %}{{loop.index}}{{loop.index0}}{{loop.first}}{{loop.last}}{% endfor %}"
        ).render(context, HashMap::new());

        assert_eq!(result.unwrap(), "10truefalse21falsefalse32falsetrue".to_owned());
    }
}