diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-01-06 11:21:17 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-01-06 13:02:31 +0100 |
commit | 856d2d7a27d9bb40aba0bcca60a9a5c82939bce2 (patch) | |
tree | 01d534f9708ca307f56d7cfa919a412d361ce240 | |
parent | 9ff15fdb59c702c800068a11c7006cbddf7ed6a6 (diff) | |
download | askama-856d2d7a27d9bb40aba0bcca60a9a5c82939bce2.tar.gz askama-856d2d7a27d9bb40aba0bcca60a9a5c82939bce2.tar.bz2 askama-856d2d7a27d9bb40aba0bcca60a9a5c82939bce2.zip |
Separate writing to buffer and node visitation in generator
Diffstat (limited to '')
-rw-r--r-- | askama_derive/src/generator.rs | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index b1babce..3fa5b9f 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -50,17 +50,8 @@ impl Generator { self.start = true; } - fn visit_lit(&mut self, s: &[u8]) { - self.write("buf.push_str("); - self.write(&format!("{:#?}", str::from_utf8(s).unwrap())); - self.writeln(");"); - } - fn visit_var(&mut self, s: &[u8]) { - let var_name = str::from_utf8(s).unwrap(); - let code = format!("std::fmt::Write::write_fmt(\ - &mut buf, format_args!(\"{{}}\", self.{})).unwrap();", var_name); - self.writeln(&code); + self.write(&format!("self.{}", str::from_utf8(s).unwrap())); } fn visit_expr(&mut self, expr: &Expr) { @@ -69,11 +60,23 @@ impl Generator { } } + fn write_lit(&mut self, s: &[u8]) { + self.write("buf.push_str("); + self.write(&format!("{:#?}", str::from_utf8(s).unwrap())); + self.writeln(");"); + } + + fn write_expr(&mut self, s: &Expr) { + self.write("std::fmt::Write::write_fmt(&mut buf, format_args!(\"{}\", "); + self.visit_expr(s); + self.writeln(")).unwrap();"); + } + fn handle(&mut self, tokens: &Vec<Node>) { for n in tokens { match n { - &Node::Lit(val) => { self.visit_lit(val); }, - &Node::Expr(ref val) => { self.visit_expr(&val); }, + &Node::Lit(val) => { self.write_lit(val); }, + &Node::Expr(ref val) => { self.write_expr(&val); }, } } } |