diff options
author | Anthony Nowell <anowell@gmail.com> | 2017-09-27 01:53:35 -0600 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-11-02 14:55:10 +0100 |
commit | 468f376bfc2cf3e09addac37cd144de56b5f93bf (patch) | |
tree | 3a6e127bac7e6a2193739902ff60a16b12c51c12 /askama_shared/src/generator.rs | |
parent | 14beb21d0cef62ca47ad85617bd9460369a15b61 (diff) | |
download | askama-468f376bfc2cf3e09addac37cd144de56b5f93bf.tar.gz askama-468f376bfc2cf3e09addac37cd144de56b5f93bf.tar.bz2 askama-468f376bfc2cf3e09addac37cd144de56b5f93bf.zip |
implement basic match functionality
Diffstat (limited to 'askama_shared/src/generator.rs')
-rw-r--r-- | askama_shared/src/generator.rs | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index dcdea32..6fca3fb 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -1,6 +1,6 @@ use filters; use input::TemplateInput; -use parser::{self, Cond, Expr, Macro, Node, Target, WS}; +use parser::{self, Cond, Expr, Macro, Node, Target, When, WS}; use path; use quote::{Tokens, ToTokens}; @@ -339,6 +339,9 @@ impl<'a> Generator<'a> { Node::Cond(ref conds, ref ws) => { self.write_cond(state, conds, ws); }, + Node::Match(ref ws1, ref expr, ref inter, ref arms, ref ws2) => { + self.write_match(state, ws1, expr, inter, arms, ws2); + }, Node::Loop(ref ws1, ref var, ref iter, ref body, ref ws2) => { self.write_loop(state, ws1, var, iter, body, ws2); }, @@ -428,6 +431,43 @@ impl<'a> Generator<'a> { self.writeln("}"); } + fn write_match(&mut self, state: &'a State, ws1: &WS, expr: &Expr, inter: &'a str, arms: + &'a [When], ws2: &WS) { + self.flush_ws(ws1); + if !inter.is_empty() { + self.next_ws = Some(inter); + } + + self.write("match "); + self.visit_expr(expr); + self.writeln(" {"); + + for arm in arms { + let &(ref ws, ref variant, ref params, ref body) = arm; + self.locals.push(); + self.write(variant); + if params.len() > 0 { + self.write("("); + for (i, param) in params.iter().enumerate() { + self.locals.insert(param); + if i > 0 { + self.write(", "); + } + self.write(param); + } + self.write(")"); + } + self.writeln(" => {"); + self.handle_ws(ws); + self.handle(state, body, AstLevel::Nested); + self.writeln("}"); + self.locals.pop(); + } + + self.writeln("}"); + self.handle_ws(ws2); + } + fn write_loop(&mut self, state: &'a State, ws1: &WS, var: &'a Target, iter: &Expr, body: &'a [Node], ws2: &WS) { self.handle_ws(ws1); |