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
use std::collections::HashMap;
use parser::{parse, Node};
#[derive(Debug, Clone)]
pub struct Template {
pub name: String,
pub ast: Node,
pub parent: Option<String>,
pub blocks: HashMap<String, Node>,
}
impl Template {
pub fn new(tpl_name: &str, input: &str) -> Template {
let ast = match parse(input) {
Ok(a) => a,
Err(e) => panic!("Error when parsing `{}`:\n{}", tpl_name, e)
};
let parent = match ast.get_children().front() {
Some(f) => match *f {
Node::Extends(ref name) => Some(name.to_string()),
_ => None
},
None => None
};
let mut blocks = HashMap::new();
if parent.is_some() {
for node in ast.get_children() {
match node {
Node::Block { ref name, .. } => {
if blocks.contains_key(name) {
panic!("Error when parsing `{}`:\n{} block is duplicated", tpl_name, name);
}
blocks.insert(name.to_string(), node.clone())
},
_ => continue,
};
}
}
Template {
name: tpl_name.to_string(),
ast: ast,
parent: parent,
blocks: blocks,
}
}
}
#[cfg(test)]
mod tests {
use super::Template;
#[test]
fn test_can_parse_ok_template() {
Template::new("hello", "Hello {{ world }}.");
}
#[test]
fn test_can_find_parent_template() {
let tpl = Template::new("hello", "{% extends \"base.html\" %}");
assert_eq!(tpl.parent.unwrap(), "base.html".to_string());
}
#[test]
fn test_can_find_blocks() {
let tpl = Template::new(
"hello",
"{% extends \"base.html\" %}{% block hey %}{% endblock hey %}"
);
assert_eq!(tpl.parent.unwrap(), "base.html".to_string());
assert_eq!(tpl.blocks.contains_key("hey"), true);
}
}