diff options
author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2023-10-23 21:15:08 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2023-10-25 14:30:35 +0200 |
commit | c3281e353d8192e6425f903216eeb6ecf9dd39fc (patch) | |
tree | 3d4b2fb068ea5debd7fa3447890afd589420b75e /askama_parser/src/node.rs | |
parent | 6dae920a451407913ab188fd507a2cfc2cd03018 (diff) | |
download | askama-c3281e353d8192e6425f903216eeb6ecf9dd39fc.tar.gz askama-c3281e353d8192e6425f903216eeb6ecf9dd39fc.tar.bz2 askama-c3281e353d8192e6425f903216eeb6ecf9dd39fc.zip |
Improve error for invalid name used in `endblock`
Diffstat (limited to 'askama_parser/src/node.rs')
-rw-r--r-- | askama_parser/src/node.rs | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/askama_parser/src/node.rs b/askama_parser/src/node.rs index 596b99a..b321c80 100644 --- a/askama_parser/src/node.rs +++ b/askama_parser/src/node.rs @@ -1,3 +1,4 @@ +use std::borrow::Cow; use std::str; use nom::branch::alt; @@ -650,7 +651,13 @@ impl<'a> BlockDef<'a> { |i| s.tag_block_start(i), opt(Whitespace::parse), ws(keyword("endblock")), - cut(tuple((opt(ws(keyword(name))), opt(Whitespace::parse)))), + cut(tuple(( + opt(|before| { + let (after, end_name) = ws(identifier)(before)?; + check_end_name(before, after, name, end_name, "block") + }), + opt(Whitespace::parse), + ))), ))), ))); let (i, (nodes, (_, pws2, _, (_, nws2)))) = end(i)?; @@ -667,6 +674,27 @@ impl<'a> BlockDef<'a> { } } +fn check_end_name<'a>( + before: &'a str, + after: &'a str, + name: &'a str, + end_name: &'a str, + kind: &str, +) -> IResult<&'a str, &'a str, ErrorContext<&'a str>> { + if name == end_name { + return Ok((after, end_name)); + } + let message = if name.is_empty() && !end_name.is_empty() { + format!("unexpected name `{end_name}` in `end{kind}` tag for unnamed `{kind}`") + } else { + format!("expected name `{name}` in `end{kind}` tag, found `{end_name}`") + }; + Err(nom::Err::Failure(ErrorContext { + input: before, + message: Some(Cow::Owned(message)), + })) +} + #[derive(Debug, PartialEq)] pub struct Lit<'a> { pub lws: &'a str, |