aboutsummaryrefslogtreecommitdiffstats
path: root/askama_parser
diff options
context:
space:
mode:
Diffstat (limited to 'askama_parser')
-rw-r--r--askama_parser/src/lib.rs2
-rw-r--r--askama_parser/src/node.rs48
2 files changed, 33 insertions, 17 deletions
diff --git a/askama_parser/src/lib.rs b/askama_parser/src/lib.rs
index 43d953d..0f796ae 100644
--- a/askama_parser/src/lib.rs
+++ b/askama_parser/src/lib.rs
@@ -15,7 +15,7 @@ use nom::sequence::{delimited, pair, tuple};
use nom::{error_position, AsChar, IResult, InputTakeAtPosition};
pub use self::expr::Expr;
-pub use self::node::{Cond, CondTest, Loop, Macro, Node, Target, When, Whitespace, Ws};
+pub use self::node::{Cond, CondTest, Import, Loop, Macro, Node, Target, When, Whitespace, Ws};
mod expr;
mod node;
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".