From be99df789a21257da18a3664f4ac6409314e8682 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 4 Feb 2017 21:45:54 +0100 Subject: Generalize support for comparison operators --- askama/src/generator.rs | 7 ++++--- 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>), - Eq(Box>, Box>), + Compare(&'a str, Box>, Box>), } pub enum Target<'a> { @@ -66,13 +66,18 @@ fn expr_filtered(i: &[u8]) -> IResult<&[u8], Expr> { return IResult::Done(left, expr); } -named!(expr_eq, do_parse!( +named!(expr_compare, 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, alt!(expr_eq | expr_filtered)); +named!(expr_any, alt!(expr_compare | expr_filtered)); named!(expr_node, map!( delimited!(tag_s!("{{"), ws!(expr_any), tag_s!("}}")), -- cgit