Skip to content

Commit

Permalink
Error in case an operator is used as a delimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Sep 19, 2024
1 parent 43d8e75 commit 942fe8e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion askama_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ with-rocket = []
with-warp = []

[dependencies]
parser = { package = "askama_parser", version = "0.3", path = "../askama_parser" }
parser = { package = "askama_parser", version = "0.3.1", path = "../askama_parser" }
mime = "0.3"
mime_guess = "2"
proc-macro2 = "1"
Expand Down
6 changes: 6 additions & 0 deletions askama_derive/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::{env, fs};

use parser::expr::TWO_PLUS_CHAR_OPS;
#[cfg(feature = "serde")]
use serde::Deserialize;

Expand Down Expand Up @@ -161,6 +162,11 @@ impl<'a> TryInto<Syntax<'a>> for RawSyntax<'a> {
);
} else if s.chars().any(|c| c.is_whitespace()) {
return Err(format!("delimiters may not contain white spaces: {s:?}").into());
} else if TWO_PLUS_CHAR_OPS.contains(&s) {
return Err(format!(
"delimiters may not contain operators: {s:?}"
)
.into());
}
}

Expand Down
2 changes: 1 addition & 1 deletion askama_parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "askama_parser"
version = "0.3.0"
version = "0.3.1"
description = "Parser for Askama templates"
documentation = "https://docs.rs/askama"
keywords = ["markup", "template", "jinja2", "html"]
Expand Down
4 changes: 4 additions & 0 deletions askama_parser/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ impl<'a> Expr<'a> {
))(i)
}

// Keep in sync with `TWO_PLUS_CHAR_OPS`, below
expr_prec_layer!(or, and, "||");
expr_prec_layer!(and, compare, "&&");
expr_prec_layer!(compare, bor, "==", "!=", ">=", ">", "<=", "<");
Expand Down Expand Up @@ -430,3 +431,6 @@ impl<'a> Suffix<'a> {
map(preceded(take_till(not_ws), char('?')), |_| Self::Try)(i)
}
}

pub const TWO_PLUS_CHAR_OPS: &[&str] =
&["||", "&&", "==", "!=", ">=", "<=", "<<", ">>", "..", "..="];

0 comments on commit 942fe8e

Please sign in to comment.