mirror of
https://github.com/askama-rs/askama.git
synced 2025-10-02 15:25:19 +00:00
Added testing for reserved variable names
Signed-off-by: max <gmx.sht@gmail.com>
This commit is contained in:
parent
b5797cba88
commit
33eb70c34a
@ -135,7 +135,6 @@ pub(crate) type ParseResult<'a, T = &'a str> = Result<(&'a str, T), nom::Err<Err
|
|||||||
///
|
///
|
||||||
/// It cannot be used to replace `ParseError` because it expects a generic, which would make
|
/// It cannot be used to replace `ParseError` because it expects a generic, which would make
|
||||||
/// `askama`'s users experience less good (since this generic is only needed for `nom`).
|
/// `askama`'s users experience less good (since this generic is only needed for `nom`).
|
||||||
#[derive(Debug)]
|
|
||||||
pub(crate) struct ErrorContext<'a> {
|
pub(crate) struct ErrorContext<'a> {
|
||||||
pub(crate) input: &'a str,
|
pub(crate) input: &'a str,
|
||||||
pub(crate) message: Option<Cow<'static, str>>,
|
pub(crate) message: Option<Cow<'static, str>>,
|
||||||
|
@ -224,7 +224,8 @@ impl<'a> Target<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// neither literal nor struct nor path
|
// 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> {
|
fn lit(i: &'a str) -> ParseResult<'a, Self> {
|
||||||
@ -236,9 +237,24 @@ impl<'a> Target<'a> {
|
|||||||
))(i)
|
))(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn named(i: &'a str) -> ParseResult<'a, (&str, Self)> {
|
fn named(init_i: &'a str) -> ParseResult<'a, (&str, Self)> {
|
||||||
let (i, (src, target)) = pair(identifier, opt(preceded(ws(char(':')), Self::parse)))(i)?;
|
let (i, (src, target)) =
|
||||||
Ok((i, (src, target.unwrap_or(Self::Name(src)))))
|
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)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user