blob: 971e2e25c6431c29cd18d39d060dbe0bf2e28494 (
plain) (
blame)
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
|
use parser::Node;
use std::str;
struct Generator {
buf: String,
}
impl Generator {
fn new() -> Generator {
Generator { buf: String::new() }
}
fn write(&mut self, s: &str) {
self.buf.push_str(s);
}
fn result(self) -> String {
self.buf
}
}
pub fn generate(ctx_name: &str, tokens: &Vec<Node>) -> String {
let mut gen = Generator::new();
gen.write("impl askama::Template for ");
gen.write(ctx_name);
gen.write(" {\n");
gen.write(" fn render(&self) -> String {\n");
gen.write(" let mut buf = String::new();\n");
for n in tokens {
gen.write(" buf.push_str(");
gen.write(&match n {
&Node::Lit(val) => format!("{:#?}", str::from_utf8(val).unwrap()),
&Node::Expr(val) => format!("&self.{}", str::from_utf8(val).unwrap()),
});
gen.write(");\n");
}
gen.write(" buf");
gen.write(" }\n");
gen.write("}\n\n");
gen.result()
}
|