aboutsummaryrefslogtreecommitdiffstats
path: root/askama_parser/src/node.rs
diff options
context:
space:
mode:
authorLibravatar René Kijewski <rene.kijewski@fu-berlin.de>2023-08-01 03:48:24 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-08-01 13:04:41 +0200
commit6323b5e17198f6d91cb1d880823168a43b264ebf (patch)
tree11456147892b5b8a4b5fb671c6d17be60dd81530 /askama_parser/src/node.rs
parent7b7c38b37785b83ce28f1cc5f6e89abb89672276 (diff)
downloadaskama-6323b5e17198f6d91cb1d880823168a43b264ebf.tar.gz
askama-6323b5e17198f6d91cb1d880823168a43b264ebf.tar.bz2
askama-6323b5e17198f6d91cb1d880823168a43b264ebf.zip
parser: add type for `Node::Raw`
Diffstat (limited to '')
-rw-r--r--askama_parser/src/node.rs65
1 files changed, 37 insertions, 28 deletions
diff --git a/askama_parser/src/node.rs b/askama_parser/src/node.rs
index a743b53..ebab746 100644
--- a/askama_parser/src/node.rs
+++ b/askama_parser/src/node.rs
@@ -30,7 +30,7 @@ pub enum Node<'a> {
Include(Ws, &'a str),
Import(Import<'a>),
Macro(Macro<'a>),
- Raw(Ws, Lit<'a>, Ws),
+ Raw(Raw<'a>),
Break(Ws),
Continue(Ws),
}
@@ -59,7 +59,7 @@ impl<'a> Node<'a> {
map(Import::parse, Self::Import),
map(|i| BlockDef::parse(i, s), Self::BlockDef),
map(|i| Macro::parse(i, s), Self::Macro),
- |i| Self::raw(i, s),
+ map(|i| Raw::parse(i, s), Self::Raw),
|i| Self::r#break(i, s),
|i| Self::r#continue(i, s),
)),
@@ -204,32 +204,6 @@ impl<'a> Node<'a> {
Ok((i, Self::Include(Ws(pws, nws), name)))
}
- fn raw(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> {
- let endraw = tuple((
- |i| s.tag_block_start(i),
- opt(Whitespace::parse),
- ws(keyword("endraw")),
- opt(Whitespace::parse),
- peek(|i| s.tag_block_end(i)),
- ));
-
- let mut p = tuple((
- opt(Whitespace::parse),
- ws(keyword("raw")),
- cut(tuple((
- opt(Whitespace::parse),
- |i| s.tag_block_end(i),
- consumed(skip_till(endraw)),
- ))),
- ));
-
- let (_, (pws1, _, (nws1, _, (contents, (i, (_, pws2, _, nws2, _)))))) = p(i)?;
- let val = Lit::split_ws_parts(contents);
- let ws1 = Ws(pws1, nws1);
- let ws2 = Ws(pws2, nws2);
- Ok((i, Self::Raw(ws1, val, ws2)))
- }
-
fn r#break(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> {
let mut p = tuple((
opt(Whitespace::parse),
@@ -812,6 +786,41 @@ impl<'a> Lit<'a> {
}
}
+#[derive(Debug, PartialEq)]
+pub struct Raw<'a> {
+ pub ws1: Ws,
+ pub lit: Lit<'a>,
+ pub ws2: Ws,
+}
+
+impl<'a> Raw<'a> {
+ fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> {
+ let endraw = tuple((
+ |i| s.tag_block_start(i),
+ opt(Whitespace::parse),
+ ws(keyword("endraw")),
+ opt(Whitespace::parse),
+ peek(|i| s.tag_block_end(i)),
+ ));
+
+ let mut p = tuple((
+ opt(Whitespace::parse),
+ ws(keyword("raw")),
+ cut(tuple((
+ opt(Whitespace::parse),
+ |i| s.tag_block_end(i),
+ consumed(skip_till(endraw)),
+ ))),
+ ));
+
+ let (_, (pws1, _, (nws1, _, (contents, (i, (_, pws2, _, nws2, _)))))) = p(i)?;
+ let lit = Lit::split_ws_parts(contents);
+ let ws1 = Ws(pws1, nws1);
+ let ws2 = Ws(pws2, nws2);
+ Ok((i, Self { ws1, lit, ws2 }))
+ }
+}
+
/// 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".