From 856d2d7a27d9bb40aba0bcca60a9a5c82939bce2 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 6 Jan 2017 11:21:17 +0100 Subject: Separate writing to buffer and node visitation in generator --- askama_derive/src/generator.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'askama_derive/src') 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) { 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); }, } } } -- cgit