aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar René Kijewski <rene.kijewski@fu-berlin.de>2023-08-01 04:07:07 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-08-01 13:04:41 +0200
commit14df8e16c1b2803b23b4bf6eda3bc0e3a5fe5354 (patch)
tree3581c0fb04975cb9feea6ef42eaeb98168017aa4
parentc5c46ada406aea830293a22bcef64a0d6205b3f4 (diff)
downloadaskama-14df8e16c1b2803b23b4bf6eda3bc0e3a5fe5354.tar.gz
askama-14df8e16c1b2803b23b4bf6eda3bc0e3a5fe5354.tar.bz2
askama-14df8e16c1b2803b23b4bf6eda3bc0e3a5fe5354.zip
parser: add type for `Node::Include`
-rw-r--r--askama_derive/src/generator.rs15
-rw-r--r--askama_parser/src/lib.rs4
-rw-r--r--askama_parser/src/node.rs38
3 files changed, 35 insertions, 22 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs
index 4cf972b..626c7d2 100644
--- a/askama_derive/src/generator.rs
+++ b/askama_derive/src/generator.rs
@@ -3,7 +3,7 @@ use crate::heritage::{Context, Heritage};
use crate::input::{Print, Source, TemplateInput};
use crate::CompileError;
use parser::{
- Call, CondTest, Expr, If, Let, Lit, Loop, Match, Node, Parsed, Target, Whitespace, Ws,
+ Call, CondTest, Expr, If, Include, Let, Lit, Loop, Match, Node, Parsed, Target, Whitespace, Ws,
};
use proc_macro::TokenStream;
@@ -652,8 +652,8 @@ impl<'a> Generator<'a> {
Node::BlockDef(ref b) => {
size_hint += self.write_block(buf, Some(b.name), Ws(b.ws1.0, b.ws2.1))?;
}
- Node::Include(ws, path) => {
- size_hint += self.handle_include(ctx, buf, ws, path)?;
+ Node::Include(ref i) => {
+ size_hint += self.handle_include(ctx, buf, i)?;
}
Node::Call(ref call) => {
size_hint += self.write_call(ctx, buf, call)?;
@@ -1002,15 +1002,14 @@ impl<'a> Generator<'a> {
&mut self,
ctx: &'a Context<'_>,
buf: &mut Buffer,
- ws: Ws,
- path: &str,
+ i: &'a Include<'_>,
) -> Result<usize, CompileError> {
- self.flush_ws(ws);
+ self.flush_ws(i.ws);
self.write_buf_writable(buf)?;
let path = self
.input
.config
- .find_template(path, Some(&self.input.path))?;
+ .find_template(i.path, Some(&self.input.path))?;
// Make sure the compiler understands that the generated code depends on the template file.
{
@@ -1048,7 +1047,7 @@ impl<'a> Generator<'a> {
let mut size_hint = child.handle(ctx, nodes, buf, AstLevel::Nested)?;
size_hint += child.write_buf_writable(buf)?;
- self.prepare_ws(ws);
+ self.prepare_ws(i.ws);
Ok(size_hint)
}
diff --git a/askama_parser/src/lib.rs b/askama_parser/src/lib.rs
index 8e90802..6822233 100644
--- a/askama_parser/src/lib.rs
+++ b/askama_parser/src/lib.rs
@@ -16,8 +16,8 @@ use nom::{error_position, AsChar, IResult, InputTakeAtPosition};
pub use self::expr::Expr;
pub use self::node::{
- BlockDef, Call, Cond, CondTest, If, Import, Let, Lit, Loop, Macro, Match, Node, Raw, Target,
- When, Whitespace, Ws,
+ BlockDef, Call, Cond, CondTest, If, Import, Include, Let, Lit, Loop, Macro, Match, Node, Raw,
+ Target, When, Whitespace, Ws,
};
mod expr;
diff --git a/askama_parser/src/node.rs b/askama_parser/src/node.rs
index f7e51de..cb6da92 100644
--- a/askama_parser/src/node.rs
+++ b/askama_parser/src/node.rs
@@ -26,7 +26,7 @@ pub enum Node<'a> {
Loop(Loop<'a>),
Extends(&'a str),
BlockDef(BlockDef<'a>),
- Include(Ws, &'a str),
+ Include(Include<'a>),
Import(Import<'a>),
Macro(Macro<'a>),
Raw(Raw<'a>),
@@ -54,7 +54,7 @@ impl<'a> Node<'a> {
|i| Self::r#for(i, s),
map(|i| Match::parse(i, s), Self::Match),
Self::extends,
- Self::include,
+ map(Include::parse, Self::Include),
map(Import::parse, Self::Import),
map(|i| BlockDef::parse(i, s), Self::BlockDef),
map(|i| Macro::parse(i, s), Self::Macro),
@@ -140,16 +140,6 @@ impl<'a> Node<'a> {
Ok((i, Self::Extends(name)))
}
- fn include(i: &'a str) -> IResult<&'a str, Self> {
- let mut p = tuple((
- opt(Whitespace::parse),
- ws(keyword("include")),
- cut(pair(ws(str_lit), opt(Whitespace::parse))),
- ));
- let (i, (pws, _, (name, nws))) = p(i)?;
- Ok((i, Self::Include(Ws(pws, nws), name)))
- }
-
fn r#break(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> {
let mut p = tuple((
opt(Whitespace::parse),
@@ -843,6 +833,30 @@ impl<'a> If<'a> {
}
}
+#[derive(Debug, PartialEq)]
+pub struct Include<'a> {
+ pub ws: Ws,
+ pub path: &'a str,
+}
+
+impl<'a> Include<'a> {
+ fn parse(i: &'a str) -> IResult<&'a str, Self> {
+ let mut p = tuple((
+ opt(Whitespace::parse),
+ ws(keyword("include")),
+ cut(pair(ws(str_lit), opt(Whitespace::parse))),
+ ));
+ let (i, (pws, _, (path, nws))) = p(i)?;
+ Ok((
+ i,
+ Self {
+ ws: Ws(pws, nws),
+ path,
+ },
+ ))
+ }
+}
+
/// 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".