Skip to content

Commit

Permalink
derive: add as_bool() function
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Oct 16, 2024
1 parent 0ec827f commit c3bb07a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
5 changes: 5 additions & 0 deletions rinja/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,8 @@ impl FastWritable for Empty {
Ok(())
}
}

#[inline]
pub fn as_bool<T: PrimitiveType<Value = bool>>(value: &T) -> bool {
value.get()
}
4 changes: 2 additions & 2 deletions rinja_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,9 +650,9 @@ impl<'a> Generator<'a> {
// e.g. `&&&&&bool` to `bool`. First `&(...) as &bool`
// coerces e.g. `&&&bool` to `&bool`. Then `*(&bool)`
// finally dereferences it to `bool`.
buf.write("*(&(");
buf.write("rinja::helpers::as_bool(&(");
buf.write(this.visit_expr_root(ctx, expr)?);
buf.write(") as &rinja::helpers::core::primitive::bool) {");
buf.write(")) {");
}
} else if pos != 0 {
buf.write("} else {");
Expand Down
20 changes: 10 additions & 10 deletions rinja_derive/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ writer.write_str("bla")?;"#,
"{% if x == 12 %}bli
{%- else if x is defined %}12
{%- else %}nope{% endif %}",
r#"if *(&(self.x == 12) as &rinja::helpers::core::primitive::bool) {
r#"if rinja::helpers::as_bool(&(self.x == 12)) {
writer.write_str("bli")?;
} else {
writer.write_str("12")?;
Expand All @@ -306,7 +306,7 @@ writer.write_str("12")?;
// are present.
compare(
"{% if y is defined || x == 12 %}{{x}}{% endif %}",
r"if *(&(self.x == 12) as &rinja::helpers::core::primitive::bool) {
r"if rinja::helpers::as_bool(&(self.x == 12)) {
match (
&((&&rinja::filters::AutoEscaper::new(&(self.x), rinja::filters::Text)).rinja_auto_escape()?),
) {
Expand Down Expand Up @@ -347,7 +347,7 @@ writer.write_str("12")?;
compare(
"{% if y is defined && y == 12 %}{{y}}{% else %}bli{% endif %}",
r#"
if *(&(self.y == 12) as &rinja::helpers::core::primitive::bool) {
if rinja::helpers::as_bool(&(self.y == 12)) {
match (
&((&&rinja::filters::AutoEscaper::new(
&(self.y),
Expand Down Expand Up @@ -418,31 +418,31 @@ match (
// Ensure that the `!` is kept .
compare(
"{% if y is defined && !y %}bla{% endif %}",
r#"if *(&(!self.y) as &rinja::helpers::core::primitive::bool) {
r#"if rinja::helpers::as_bool(&(!self.y)) {
writer.write_str("bla")?;
}"#,
&[("y", "bool")],
3,
);
compare(
"{% if y is defined && !(y) %}bla{% endif %}",
r#"if *(&(!(self.y)) as &rinja::helpers::core::primitive::bool) {
r#"if rinja::helpers::as_bool(&(!(self.y))) {
writer.write_str("bla")?;
}"#,
&[("y", "bool")],
3,
);
compare(
"{% if y is not defined || !y %}bla{% endif %}",
r#"if *(&(!self.y) as &rinja::helpers::core::primitive::bool) {
r#"if rinja::helpers::as_bool(&(!self.y)) {
writer.write_str("bla")?;
}"#,
&[("y", "bool")],
3,
);
compare(
"{% if y is not defined || !(y) %}bla{% endif %}",
r#"if *(&(!(self.y)) as &rinja::helpers::core::primitive::bool) {
r#"if rinja::helpers::as_bool(&(!(self.y))) {
writer.write_str("bla")?;
}"#,
&[("y", "bool")],
Expand Down Expand Up @@ -507,7 +507,7 @@ fn check_bool_conditions() {
);
compare(
"{% if false || x == 12 %}{{x}}{% endif %}",
r"if *(&(self.x == 12) as &rinja::helpers::core::primitive::bool) {
r"if rinja::helpers::as_bool(&(self.x == 12)) {
match (
&((&&rinja::filters::AutoEscaper::new(
&(self.x),
Expand All @@ -531,7 +531,7 @@ fn check_bool_conditions() {
// condition.
compare(
"{% if y == 3 || (true || x == 12) %}{{x}}{% endif %}",
r"if *(&(self.y == 3 || (true)) as &rinja::helpers::core::primitive::bool) {
r"if rinja::helpers::as_bool(&(self.y == 3 || (true))) {
match (
&((&&rinja::filters::AutoEscaper::new(
&(self.x),
Expand Down Expand Up @@ -569,7 +569,7 @@ fn check_bool_conditions() {
);
compare(
"{% if y == 3 || (x == 12 || true) %}{{x}}{% endif %}",
r"if *(&(self.y == 3 || (self.x == 12 || true)) as &rinja::helpers::core::primitive::bool) {
r"if rinja::helpers::as_bool(&(self.y == 3 || (self.x == 12 || true))) {
match (
&((&&rinja::filters::AutoEscaper::new(
&(self.x),
Expand Down

0 comments on commit c3bb07a

Please sign in to comment.