aboutsummaryrefslogtreecommitdiffstats
path: root/askama_parser/src/node.rs
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-07-03 09:39:39 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-07-31 10:27:15 +0200
commit997c5e1930ab40a4cc052633842477d7ace3dbe0 (patch)
tree2cf1f7e1595c670fb87d48b6ab3d40063e92745b /askama_parser/src/node.rs
parent65fb5630825b62ca959fcb55252d1657aefea0f3 (diff)
downloadaskama-997c5e1930ab40a4cc052633842477d7ace3dbe0.tar.gz
askama-997c5e1930ab40a4cc052633842477d7ace3dbe0.tar.bz2
askama-997c5e1930ab40a4cc052633842477d7ace3dbe0.zip
parser: move when parsers into impl block
Diffstat (limited to 'askama_parser/src/node.rs')
-rw-r--r--askama_parser/src/node.rs97
1 files changed, 50 insertions, 47 deletions
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<Node<'a>>,
}
+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<Node<'a>>> {
s.enter_loop();
let result = Node::many(i, s);