Skip to content

Commit

Permalink
parser: add type for Node::Include
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Aug 1, 2023
1 parent 5b3d01c commit 486653c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
15 changes: 7 additions & 8 deletions askama_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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.
{
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions askama_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 15 additions & 3 deletions askama_parser/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>),
Expand Down Expand Up @@ -291,8 +291,14 @@ impl<'a> Node<'a> {
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)))
let (i, (pws, _, (path, nws))) = p(i)?;
Ok((
i,
Self::Include(Include {
ws: Ws(pws, nws),
path,
}),
))
}

fn block(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> {
Expand Down Expand Up @@ -825,6 +831,12 @@ pub struct If<'a> {
pub branches: Vec<Cond<'a>>,
}

#[derive(Debug, PartialEq)]
pub struct Include<'a> {
pub ws: Ws,
pub path: &'a str,
}

/// 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".
Expand Down

0 comments on commit 486653c

Please sign in to comment.