diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-02-04 21:45:54 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-02-04 21:45:54 +0100 |
commit | be99df789a21257da18a3664f4ac6409314e8682 (patch) | |
tree | 7f07800dd9de07a1804b94f28bdfe6cd30714138 | |
parent | e96c361da339f5fcff781b750a3bb23b8d2d9134 (diff) | |
download | askama-be99df789a21257da18a3664f4ac6409314e8682.tar.gz askama-be99df789a21257da18a3664f4ac6409314e8682.tar.bz2 askama-be99df789a21257da18a3664f4ac6409314e8682.zip |
Generalize support for comparison operators
-rw-r--r-- | askama/src/generator.rs | 7 | ||||
-rw-r--r-- | askama/src/parser.rs | 15 |
2 files changed, 14 insertions, 8 deletions
diff --git a/askama/src/generator.rs b/askama/src/generator.rs index 7ad1313..56bd19e 100644 --- a/askama/src/generator.rs +++ b/askama/src/generator.rs @@ -91,9 +91,9 @@ impl Generator { self.write(")"); } - fn visit_eq(&mut self, left: &Expr, right: &Expr) { + fn visit_compare(&mut self, op: &str, left: &Expr, right: &Expr) { self.visit_expr(left); - self.write(" == "); + self.write(&format!(" {} ", op)); self.visit_expr(right); } @@ -101,7 +101,8 @@ impl Generator { match expr { &Expr::Var(s) => self.visit_var(s), &Expr::Filter(name, ref val) => self.visit_filter(name, &val), - &Expr::Eq(ref left, ref right) => self.visit_eq(&left, &right), + &Expr::Compare(op, ref left, ref right) => + self.visit_compare(op, &left, &right), } } diff --git a/askama/src/parser.rs b/askama/src/parser.rs index d0ad809..f395cb6 100644 --- a/askama/src/parser.rs +++ b/askama/src/parser.rs @@ -4,7 +4,7 @@ use std::str; pub enum Expr<'a> { Var(&'a [u8]), Filter(&'a str, Box<Expr<'a>>), - Eq(Box<Expr<'a>>, Box<Expr<'a>>), + Compare(&'a str, Box<Expr<'a>>, Box<Expr<'a>>), } pub enum Target<'a> { @@ -66,13 +66,18 @@ fn expr_filtered(i: &[u8]) -> IResult<&[u8], Expr> { return IResult::Done(left, expr); } -named!(expr_eq<Expr>, do_parse!( +named!(expr_compare<Expr>, do_parse!( left: expr_filtered >> - ws!(tag_s!("==")) >> + op: ws!(alt!( + tag_s!("==") | tag_s!("!=") | + tag_s!(">=") | tag_s!(">") | + tag_s!("<=") | tag_s!("<") + )) >> right: expr_filtered >> - (Expr::Eq(Box::new(left), Box::new(right))))); + (Expr::Compare(str::from_utf8(op).unwrap(), + Box::new(left), Box::new(right))))); -named!(expr_any<Expr>, alt!(expr_eq | expr_filtered)); +named!(expr_any<Expr>, alt!(expr_compare | expr_filtered)); named!(expr_node<Node>, map!( delimited!(tag_s!("{{"), ws!(expr_any), tag_s!("}}")), |