From ffc74a1775f6e4bbd8cd161fd68d814ec981f3c2 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 8 Aug 2017 20:46:38 +0200 Subject: Add helper methods to abstract away dealing with scopes --- askama_derive/src/generator.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index ac23290..02bf71e 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -178,6 +178,20 @@ impl<'a> Generator<'a> { self.prepare_ws(ws); } + /* Helper methods for dealing with scope */ + + fn is_local(&self, var: &str) -> bool { + self.locals.contains(var) + } + + fn make_local(&mut self, var: &str) { + self.locals.insert(var.to_string()); + } + + fn drop_local(&mut self, var: &str) { + self.locals.remove(var); + } + /* Visitor methods for expression types */ fn visit_num_lit(&mut self, s: &str) { @@ -189,7 +203,7 @@ impl<'a> Generator<'a> { } fn visit_var(&mut self, s: &str) { - if self.locals.contains(s) { + if self.is_local(s) { self.write(s); } else { self.write(&format!("self.{}", s)); @@ -340,7 +354,7 @@ impl<'a> Generator<'a> { self.write("for (_loop_index, "); let targets = self.visit_target(var); for name in &targets { - self.locals.insert(name.clone()); + self.make_local(name); self.write(name); } self.write(") in (&"); @@ -351,7 +365,7 @@ impl<'a> Generator<'a> { self.handle_ws(ws2); self.writeln("}"); for name in &targets { - self.locals.remove(name); + self.drop_local(name); } } -- cgit