Skip to content

Commit ab485df

Browse files
authored
Merge pull request #91 from GuillaumeGomez/cond-evaluation
Add handling of boolean literals to condition evaluation
2 parents bb77b1e + 96be934 commit ab485df

File tree

3 files changed

+104
-7
lines changed

3 files changed

+104
-7
lines changed

rinja_derive/src/generator.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,7 @@ impl<'a> Generator<'a> {
372372
only_contains_is_defined: &mut bool,
373373
) -> EvaluatedResult {
374374
match **expr {
375-
Expr::BoolLit(_)
376-
| Expr::NumLit(_)
375+
Expr::NumLit(_)
377376
| Expr::StrLit(_)
378377
| Expr::CharLit(_)
379378
| Expr::Var(_)
@@ -392,6 +391,8 @@ impl<'a> Generator<'a> {
392391
*only_contains_is_defined = false;
393392
EvaluatedResult::Unknown
394393
}
394+
Expr::BoolLit(true) => EvaluatedResult::AlwaysTrue,
395+
Expr::BoolLit(false) => EvaluatedResult::AlwaysFalse,
395396
Expr::Unary("!", ref inner) => {
396397
match self.evaluate_condition(inner, only_contains_is_defined) {
397398
EvaluatedResult::AlwaysTrue => EvaluatedResult::AlwaysFalse,
@@ -1888,8 +1889,12 @@ impl<'a> Generator<'a> {
18881889
DisplayWrap::Wrapped
18891890
}
18901891

1891-
fn visit_bool_lit(&mut self, buf: &mut Buffer, s: &str) -> DisplayWrap {
1892-
buf.write(s);
1892+
fn visit_bool_lit(&mut self, buf: &mut Buffer, s: bool) -> DisplayWrap {
1893+
if s {
1894+
buf.write("true");
1895+
} else {
1896+
buf.write("false");
1897+
}
18931898
DisplayWrap::Unwrapped
18941899
}
18951900

rinja_derive/src/tests.rs

+92
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,95 @@ writer.write_str("12")?;
334334
3,
335335
);
336336
}
337+
338+
#[test]
339+
fn check_bool_conditions() {
340+
// Checks that it removes conditions if we know at compile-time that they always return false.
341+
//
342+
// We're forced to add `bla` otherwise `compare` assert fails in weird ways...
343+
compare(
344+
"{% if false %}{{query}}{% endif %}bla",
345+
r#"writer.write_str("bla")?;"#,
346+
&[],
347+
3,
348+
);
349+
compare(
350+
"{% if false && false %}{{query}}{% endif %}bla",
351+
r#"writer.write_str("bla")?;"#,
352+
&[],
353+
3,
354+
);
355+
compare(
356+
"{% if false && true %}{{query}}{% endif %}bla",
357+
r#"writer.write_str("bla")?;"#,
358+
&[],
359+
3,
360+
);
361+
compare(
362+
"{% if true && false %}{{query}}{% endif %}bla",
363+
r#"writer.write_str("bla")?;"#,
364+
&[],
365+
3,
366+
);
367+
compare(
368+
"{% if false || true %}bli{% endif %}bla",
369+
r#"writer.write_str("blibla")?;"#,
370+
&[],
371+
6,
372+
);
373+
compare(
374+
"{% if true || false %}bli{% endif %}bla",
375+
r#"writer.write_str("blibla")?;"#,
376+
&[],
377+
6,
378+
);
379+
380+
compare(
381+
"{% if true || x == 12 %}{{x}}{% endif %}",
382+
r#"if *(&(true || self.x == 12) as &bool) {
383+
match (
384+
&((&&::rinja::filters::AutoEscaper::new(&(self.x), ::rinja::filters::Text)).rinja_auto_escape()?),
385+
) {
386+
(expr0,) => {
387+
(&&::rinja::filters::Writable(expr0)).rinja_write(writer)?;
388+
}
389+
}
390+
}
391+
"#,
392+
&[("x", "u32")],
393+
3,
394+
);
395+
compare(
396+
"{% if false || x == 12 %}{{x}}{% endif %}",
397+
r#"if *(&(false || self.x == 12) as &bool) {
398+
match (
399+
&((&&::rinja::filters::AutoEscaper::new(
400+
&(self.x),
401+
::rinja::filters::Text,
402+
))
403+
.rinja_auto_escape()?),
404+
) {
405+
(expr0,) => {
406+
(&&::rinja::filters::Writable(expr0)).rinja_write(writer)?;
407+
}
408+
}
409+
}
410+
"#,
411+
&[("x", "u32")],
412+
3,
413+
);
414+
415+
// Some funny cases.
416+
compare(
417+
"{% if !(false) %}bla{% endif %}",
418+
r#"writer.write_str("bla")?;"#,
419+
&[],
420+
3,
421+
);
422+
compare(
423+
"{% if !(true) %}{{query}}{% endif %}bla",
424+
r#"writer.write_str("bla")?;"#,
425+
&[],
426+
3,
427+
);
428+
}

rinja_parser/src/expr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ macro_rules! expr_prec_layer {
3535

3636
#[derive(Clone, Debug, PartialEq)]
3737
pub enum Expr<'a> {
38-
BoolLit(&'a str),
38+
BoolLit(bool),
3939
NumLit(&'a str),
4040
StrLit(&'a str),
4141
CharLit(&'a str),
@@ -334,8 +334,8 @@ impl<'a> Expr<'a> {
334334
let start = i;
335335
map(path_or_identifier, |v| match v {
336336
PathOrIdentifier::Path(v) => Self::Path(v),
337-
PathOrIdentifier::Identifier(v @ "true") => Self::BoolLit(v),
338-
PathOrIdentifier::Identifier(v @ "false") => Self::BoolLit(v),
337+
PathOrIdentifier::Identifier("true") => Self::BoolLit(true),
338+
PathOrIdentifier::Identifier("false") => Self::BoolLit(false),
339339
PathOrIdentifier::Identifier(v) => Self::Var(v),
340340
})(i)
341341
.map(|(i, expr)| (i, WithSpan::new(expr, start)))

0 commit comments

Comments
 (0)