diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-08-08 20:46:38 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-08-08 20:46:38 +0200 |
commit | ffc74a1775f6e4bbd8cd161fd68d814ec981f3c2 (patch) | |
tree | df0c5d846f77fc82d896c9664746de2161d22729 /askama_derive/src | |
parent | 3e1dbe4d6ddf64596d47f5fd80f1a489c1b7673a (diff) | |
download | askama-ffc74a1775f6e4bbd8cd161fd68d814ec981f3c2.tar.gz askama-ffc74a1775f6e4bbd8cd161fd68d814ec981f3c2.tar.bz2 askama-ffc74a1775f6e4bbd8cd161fd68d814ec981f3c2.zip |
Add helper methods to abstract away dealing with scopes
Diffstat (limited to 'askama_derive/src')
-rw-r--r-- | askama_derive/src/generator.rs | 20 |
1 files 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); } } |