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:18:10 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-08-01 13:04:41 +0200
commitcd075630eab578a66afec6000ce0f266e6b281e8 (patch)
tree8effbf0c2ab4ff2fb6697ec774180bff6c5c5141 /askama_parser/src/node.rs
parent222c42917aa19162562b8a9c35350e3e992f9823 (diff)
downloadaskama-cd075630eab578a66afec6000ce0f266e6b281e8.tar.gz
askama-cd075630eab578a66afec6000ce0f266e6b281e8.tar.bz2
askama-cd075630eab578a66afec6000ce0f266e6b281e8.zip
parser: add type for `Node::Import`
Diffstat (limited to 'askama_parser/src/node.rs')
-rw-r--r--askama_parser/src/node.rs48
1 files changed, 32 insertions, 16 deletions
diff --git a/askama_parser/src/node.rs b/askama_parser/src/node.rs
index f15aa5a..972314e 100644
--- a/askama_parser/src/node.rs
+++ b/askama_parser/src/node.rs
@@ -28,7 +28,7 @@ pub enum Node<'a> {
Extends(&'a str),
BlockDef(Ws, &'a str, Vec<Node<'a>>, Ws),
Include(Ws, &'a str),
- Import(Ws, &'a str, &'a str),
+ Import(Import<'a>),
Macro(Macro<'a>),
Raw(Ws, &'a str, &'a str, &'a str, Ws),
Break(Ws),
@@ -76,7 +76,7 @@ impl<'a> Node<'a> {
|i| Self::r#match(i, s),
Self::extends,
Self::include,
- Self::import,
+ map(Import::parse, Self::Import),
|i| Self::block(i, s),
map(|i| Macro::parse(i, s), Self::Macro),
|i| Self::raw(i, s),
@@ -301,20 +301,6 @@ impl<'a> Node<'a> {
))
}
- fn import(i: &'a str) -> IResult<&'a str, Self> {
- let mut p = tuple((
- opt(Whitespace::parse),
- ws(keyword("import")),
- cut(tuple((
- ws(str_lit),
- ws(keyword("as")),
- cut(pair(ws(identifier), opt(Whitespace::parse))),
- ))),
- ));
- let (i, (pws, _, (name, _, (scope, nws)))) = p(i)?;
- Ok((i, Self::Import(Ws(pws, nws), name, scope)))
- }
-
fn raw(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> {
let endraw = tuple((
|i| s.tag_block_start(i),
@@ -728,6 +714,36 @@ impl<'a> Macro<'a> {
}
}
+#[derive(Debug, PartialEq)]
+pub struct Import<'a> {
+ pub ws: Ws,
+ pub path: &'a str,
+ pub scope: &'a str,
+}
+
+impl<'a> Import<'a> {
+ fn parse(i: &'a str) -> IResult<&'a str, Self> {
+ let mut p = tuple((
+ opt(Whitespace::parse),
+ ws(keyword("import")),
+ cut(tuple((
+ ws(str_lit),
+ ws(keyword("as")),
+ cut(pair(ws(identifier), opt(Whitespace::parse))),
+ ))),
+ ));
+ let (i, (pws, _, (path, _, (scope, nws)))) = p(i)?;
+ Ok((
+ i,
+ Self {
+ ws: Ws(pws, nws),
+ path,
+ scope,
+ },
+ ))
+ }
+}
+
/// 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".