diff options
Diffstat (limited to 'askama_parser/src/node.rs')
-rw-r--r-- | askama_parser/src/node.rs | 80 |
1 files changed, 47 insertions, 33 deletions
diff --git a/askama_parser/src/node.rs b/askama_parser/src/node.rs index 9055d99..f7e51de 100644 --- a/askama_parser/src/node.rs +++ b/askama_parser/src/node.rs @@ -21,7 +21,7 @@ pub enum Node<'a> { Expr(Ws, Expr<'a>), Call(Call<'a>), Let(Let<'a>), - Cond(Vec<Cond<'a>>, Ws), + If(If<'a>), Match(Match<'a>), Loop(Loop<'a>), Extends(&'a str), @@ -50,7 +50,7 @@ impl<'a> Node<'a> { alt(( map(Call::parse, Self::Call), map(Let::parse, Self::Let), - |i| Self::r#if(i, s), + map(|i| If::parse(i, s), Self::If), |i| Self::r#for(i, s), map(|i| Match::parse(i, s), Self::Match), Self::extends, @@ -68,37 +68,6 @@ impl<'a> Node<'a> { Ok((i, contents)) } - fn r#if(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { - let mut p = tuple(( - opt(Whitespace::parse), - CondTest::parse, - cut(tuple(( - opt(Whitespace::parse), - |i| s.tag_block_end(i), - cut(tuple(( - |i| Node::many(i, s), - many0(|i| Cond::parse(i, s)), - cut(tuple(( - |i| s.tag_block_start(i), - opt(Whitespace::parse), - ws(keyword("endif")), - opt(Whitespace::parse), - ))), - ))), - ))), - )); - - let (i, (pws1, cond, (nws1, _, (nodes, elifs, (_, pws2, _, nws2))))) = p(i)?; - let mut res = vec![Cond { - ws: Ws(pws1, nws1), - cond: Some(cond), - nodes, - }]; - res.extend(elifs); - - Ok((i, Self::Cond(res, Ws(pws2, nws2)))) - } - fn r#for(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { fn content<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Vec<Node<'a>>> { s.enter_loop(); @@ -829,6 +798,51 @@ impl<'a> Let<'a> { } } +#[derive(Debug, PartialEq)] +pub struct If<'a> { + pub ws: Ws, + pub branches: Vec<Cond<'a>>, +} + +impl<'a> If<'a> { + fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { + let mut p = tuple(( + opt(Whitespace::parse), + CondTest::parse, + cut(tuple(( + opt(Whitespace::parse), + |i| s.tag_block_end(i), + cut(tuple(( + |i| Node::many(i, s), + many0(|i| Cond::parse(i, s)), + cut(tuple(( + |i| s.tag_block_start(i), + opt(Whitespace::parse), + ws(keyword("endif")), + opt(Whitespace::parse), + ))), + ))), + ))), + )); + + let (i, (pws1, cond, (nws1, _, (nodes, elifs, (_, pws2, _, nws2))))) = p(i)?; + let mut branches = vec![Cond { + ws: Ws(pws1, nws1), + cond: Some(cond), + nodes, + }]; + branches.extend(elifs); + + Ok(( + i, + Self { + ws: Ws(pws2, nws2), + branches, + }, + )) + } +} + /// First field is "minus/plus sign was used on the left part of the item". /// /// Second field is "minus/plus sign was used on the right part of the item". |