aboutsummaryrefslogtreecommitdiffstats
path: root/askama_parser/src/node.rs
diff options
context:
space:
mode:
authorLibravatar max <gmx.sht@gmail.com>2023-11-27 17:34:04 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-12-07 10:17:43 +0100
commit33eb70c34a3ca6c276ebaf7781a7d44b1a9dad83 (patch)
tree70386fb19310fa0fce58f025c48fe236df39615e /askama_parser/src/node.rs
parentb5797cba881cb11455ca9baa25be4573eb1f317e (diff)
downloadaskama-33eb70c34a3ca6c276ebaf7781a7d44b1a9dad83.tar.gz
askama-33eb70c34a3ca6c276ebaf7781a7d44b1a9dad83.tar.bz2
askama-33eb70c34a3ca6c276ebaf7781a7d44b1a9dad83.zip
Added testing for reserved variable names
Signed-off-by: max <gmx.sht@gmail.com>
Diffstat (limited to 'askama_parser/src/node.rs')
-rw-r--r--askama_parser/src/node.rs24
1 files changed, 20 insertions, 4 deletions
diff --git a/askama_parser/src/node.rs b/askama_parser/src/node.rs
index d11895b..27d4adc 100644
--- a/askama_parser/src/node.rs
+++ b/askama_parser/src/node.rs
@@ -224,7 +224,8 @@ impl<'a> Target<'a> {
}
// neither literal nor struct nor path
- map(identifier, Self::Name)(i)
+ let (new_i, name) = identifier(i)?;
+ Ok((new_i, Self::verify_name(i, name)?))
}
fn lit(i: &'a str) -> ParseResult<'a, Self> {
@@ -236,9 +237,24 @@ impl<'a> Target<'a> {
))(i)
}
- fn named(i: &'a str) -> ParseResult<'a, (&str, Self)> {
- let (i, (src, target)) = pair(identifier, opt(preceded(ws(char(':')), Self::parse)))(i)?;
- Ok((i, (src, target.unwrap_or(Self::Name(src)))))
+ fn named(init_i: &'a str) -> ParseResult<'a, (&str, Self)> {
+ let (i, (src, target)) =
+ pair(identifier, opt(preceded(ws(char(':')), Self::parse)))(init_i)?;
+ let target = match target {
+ Some(target) => target,
+ None => Self::verify_name(init_i, src)?,
+ };
+ Ok((i, (src, target)))
+ }
+
+ fn verify_name(input: &'a str, name: &'a str) -> Result<Self, nom::Err<ErrorContext<'a>>> {
+ match name {
+ "self" | "writer" => Err(nom::Err::Failure(ErrorContext {
+ input,
+ message: Some(Cow::Owned(format!("Cannot use `{name}` as a name"))),
+ })),
+ _ => Ok(Self::Name(name)),
+ }
}
}