Skip to content

Commit

Permalink
Merge pull request #150 from epage/logic
Browse files Browse the repository at this point in the history
 fix(nil): Equality logic missed a case
  • Loading branch information
epage authored Dec 18, 2017
2 parents 930982c + e0f8270 commit a8d6d07
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use compiler;
use interpreter;
use super::Template;

#[derive(Default)]
#[derive(Default, Clone)]
pub struct ParserBuilder {
blocks: HashMap<String, compiler::BoxedBlockParser>,
tags: HashMap<String, compiler::BoxedTagParser>,
Expand Down Expand Up @@ -113,11 +113,13 @@ impl ParserBuilder {
filters::url_encode as interpreter::FnFilterValue)
}

/// Register non-standard filters
#[cfg(not(feature = "extra-filters"))]
pub fn extra_filters(self) -> Self {
self
}

/// Register non-standard filters
#[cfg(feature = "extra-filters")]
pub fn extra_filters(self) -> Self {
self.filter("pluralize",
Expand Down Expand Up @@ -150,6 +152,7 @@ impl ParserBuilder {
self
}

/// Create a parser
pub fn build(self) -> Parser {
let Self {
blocks,
Expand All @@ -169,7 +172,7 @@ impl ParserBuilder {
}
}

#[derive(Default)]
#[derive(Default, Clone)]
pub struct Parser {
options: compiler::LiquidOptions,
filters: HashMap<String, interpreter::BoxedValueFilter>,
Expand Down
14 changes: 12 additions & 2 deletions src/tags/if_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn unless_block(_tag_name: &str,
tokens: &[Element],
options: &LiquidOptions)
-> Result<Box<Renderable>> {
let cond = try!(condition(arguments));
let cond = condition(arguments)?;
Ok(Box::new(Conditional {
condition: cond,
mode: false,
Expand All @@ -101,7 +101,7 @@ pub fn if_block(_tag_name: &str,
tokens: &[Element],
options: &LiquidOptions)
-> Result<Box<Renderable>> {
let cond = try!(condition(arguments));
let cond = condition(arguments)?;

let (leading_tokens, trailing_tokens) = split_block(&tokens[..], &["else", "elsif"], options);
let if_false = match trailing_tokens {
Expand Down Expand Up @@ -202,6 +202,16 @@ mod test {
"nope",
"{% endif %}");

let tokens = compiler::tokenize(&text).unwrap();
let template = compiler::parse(&tokens, &options())
.map(interpreter::Template::new)
.unwrap();

let mut context = Context::new();
context.set_global_val("truthy", Value::Nil);
let output = template.render(&mut context).unwrap();
assert_eq!(output, Some("nope".to_owned()));

let tokens = compiler::tokenize(&text).unwrap();
let template = compiler::parse(&tokens, &options())
.map(interpreter::Template::new)
Expand Down
1 change: 1 addition & 0 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct Template {
}

impl Template {
/// Renders an instance of the Template, using the given globals.
pub fn render(&self, globals: &Object) -> Result<String> {
let mut data = interpreter::Context::new()
.with_filters(self.filters.clone())
Expand Down
5 changes: 3 additions & 2 deletions src/value/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ impl PartialEq<Value> for Value {
(&Value::Object(ref x), &Value::Object(ref y)) => x == y,
(&Value::Nil, &Value::Nil) => true,

// encode Ruby truthiness; all values except false and nil
// are true, and we don't have a notion of nil
// encode Ruby truthiness: all values except false and nil are true
(&Value::Nil, &Value::Bool(b)) |
(&Value::Bool(b), &Value::Nil) => !b,
(_, &Value::Bool(b)) |
(&Value::Bool(b), _) => b,

Expand Down

0 comments on commit a8d6d07

Please sign in to comment.