From 997c5e1930ab40a4cc052633842477d7ace3dbe0 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Mon, 3 Jul 2023 09:39:39 +0200 Subject: parser: move when parsers into impl block --- askama_parser/src/node.rs | 97 ++++++++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 47 deletions(-) (limited to 'askama_parser/src/node.rs') diff --git a/askama_parser/src/node.rs b/askama_parser/src/node.rs index 54dc333..7031289 100644 --- a/askama_parser/src/node.rs +++ b/askama_parser/src/node.rs @@ -228,9 +228,9 @@ impl<'a> Node<'a> { |i| s.tag_block_end(i), cut(tuple(( ws(many0(ws(value((), |i| Self::comment(i, s))))), - many1(|i| when_block(i, s)), + many1(|i| When::when(i, s)), cut(tuple(( - opt(|i| match_else_block(i, s)), + opt(|i| When::r#match(i, s)), cut(tuple(( ws(|i| s.tag_block_start(i)), opt(Whitespace::parse), @@ -588,6 +588,54 @@ pub struct When<'a> { pub block: Vec>, } +impl<'a> When<'a> { + fn r#match(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { + let mut p = tuple(( + |i| s.tag_block_start(i), + opt(Whitespace::parse), + ws(keyword("else")), + cut(tuple(( + opt(Whitespace::parse), + |i| s.tag_block_end(i), + cut(|i| Node::many(i, s)), + ))), + )); + let (i, (_, pws, _, (nws, _, block))) = p(i)?; + Ok(( + i, + Self { + ws: Ws(pws, nws), + target: Target::Name("_"), + block, + }, + )) + } + + #[allow(clippy::self_named_constructors)] + fn when(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { + let mut p = tuple(( + |i| s.tag_block_start(i), + opt(Whitespace::parse), + ws(keyword("when")), + cut(tuple(( + ws(Target::parse), + opt(Whitespace::parse), + |i| s.tag_block_end(i), + cut(|i| Node::many(i, s)), + ))), + )); + let (i, (_, pws, _, (target, nws, _, block))) = p(i)?; + Ok(( + i, + Self { + ws: Ws(pws, nws), + target, + block, + }, + )) + } +} + #[derive(Debug, PartialEq)] pub struct Macro<'a> { pub ws1: Ws, @@ -662,51 +710,6 @@ fn cond_block<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Cond<'a>> { )) } -fn match_else_block<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, When<'a>> { - let mut p = tuple(( - |i| s.tag_block_start(i), - opt(Whitespace::parse), - ws(keyword("else")), - cut(tuple(( - opt(Whitespace::parse), - |i| s.tag_block_end(i), - cut(|i| Node::many(i, s)), - ))), - )); - let (i, (_, pws, _, (nws, _, block))) = p(i)?; - Ok(( - i, - When { - ws: Ws(pws, nws), - target: Target::Name("_"), - block, - }, - )) -} - -fn when_block<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, When<'a>> { - let mut p = tuple(( - |i| s.tag_block_start(i), - opt(Whitespace::parse), - ws(keyword("when")), - cut(tuple(( - ws(Target::parse), - opt(Whitespace::parse), - |i| s.tag_block_end(i), - cut(|i| Node::many(i, s)), - ))), - )); - let (i, (_, pws, _, (target, nws, _, block))) = p(i)?; - Ok(( - i, - When { - ws: Ws(pws, nws), - target, - block, - }, - )) -} - fn parse_loop_content<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Vec>> { s.enter_loop(); let result = Node::many(i, s); -- cgit