diff --git a/src/lexer.rs b/src/lexer.rs index 53d54906a..fe6512598 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -103,6 +103,7 @@ fn granularize(block: &str) -> Result> { "?" => Question, "-" => Dash, "=" => Assignment, + "or" => Or, "==" => Comparison(Equals), "!=" => Comparison(NotEquals), diff --git a/src/lib.rs b/src/lib.rs index 6105a6a0b..81f04b184 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,7 +43,7 @@ extern crate regex; use std::collections::HashMap; use lexer::Element; use tags::{assign_tag, cycle_tag, include_tag, break_tag, continue_tag, - comment_block, raw_block, for_block, if_block, unless_block, capture_block}; + comment_block, raw_block, for_block, if_block, unless_block, capture_block, case_block}; use std::default::Default; use std::path::PathBuf; use error::Result; @@ -161,6 +161,7 @@ impl LiquidOptions { self.register_block("for", Box::new(for_block)); self.register_block("comment", Box::new(comment_block)); self.register_block("capture", Box::new(capture_block)); + self.register_block("case", Box::new(case_block)); } pub fn register_block(&mut self, name: &str, block: Box) { diff --git a/src/parser.rs b/src/parser.rs index 89e7064e4..c430bf919 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -308,4 +308,4 @@ mod test { None => panic!("split failed") } } -} \ No newline at end of file +} diff --git a/src/tags/case_block.rs b/src/tags/case_block.rs new file mode 100644 index 000000000..79a848774 --- /dev/null +++ b/src/tags/case_block.rs @@ -0,0 +1,216 @@ +use Renderable; +use LiquidOptions; +use error::{Error, Result}; +use context::{Context}; +use lexer::Element::{self, Tag}; +use token::Token::{self, Or, Identifier}; +use parser::{parse, consume_value_token, split_block}; + +use template::Template; +use value::Value; + +struct CaseOption { + tokens: Vec, + template: Template +} + +impl CaseOption { + fn new(tokens: Vec, template: Template) -> CaseOption { + CaseOption { tokens: tokens, template: template } + } + + fn evaluate(&self, value: &Value, context: &Context) -> Result { + for t in &self.tokens { + match try!(context.evaluate(&t)) { + Some(ref v) if *v == *value => { return Ok(true) }, + _ => {} + } + } + Ok(false) + } +} + +struct Case { + target: Token, + cases: Vec, + else_block: Option