Skip to content

Commit

Permalink
parser: add type for Node::Extends
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Aug 1, 2023
1 parent 486653c commit 208441d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion askama_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ fn find_used_templates(
for n in parsed.nodes() {
match n {
Node::Extends(extends) => {
let extends = input.config.find_template(extends, Some(&path))?;
let extends = input.config.find_template(extends.path, Some(&path))?;
let dependency_path = (path.clone(), extends.clone());
if dependency_graph.contains(&dependency_path) {
return Err(format!(
Expand Down
4 changes: 2 additions & 2 deletions askama_derive/src/heritage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ impl Context<'_> {
while let Some(nodes) = nested.pop() {
for n in nodes {
match n {
Node::Extends(extends_path) if top => match extends {
Node::Extends(e) if top => match extends {
Some(_) => return Err("multiple extend blocks found".into()),
None => {
extends = Some(config.find_template(extends_path, Some(path))?);
extends = Some(config.find_template(e.path, Some(path))?);
}
},
Node::Macro(m) if top => {
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, Include, Let, Lit, Loop, Macro, Match, Node, Raw,
Target, When, Whitespace, Ws,
BlockDef, Call, Cond, CondTest, Extends, If, Import, Include, Let, Lit, Loop, Macro, Match,
Node, Raw, Target, When, Whitespace, Ws,
};

mod expr;
Expand Down
11 changes: 8 additions & 3 deletions askama_parser/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub enum Node<'a> {
If(If<'a>),
Match(Match<'a>),
Loop(Loop<'a>),
Extends(&'a str),
Extends(Extends<'a>),
BlockDef(BlockDef<'a>),
Include(Include<'a>),
Import(Import<'a>),
Expand Down Expand Up @@ -281,8 +281,8 @@ impl<'a> Node<'a> {
}

fn extends(i: &'a str) -> IResult<&'a str, Self> {
let (i, (_, name)) = tuple((ws(keyword("extends")), ws(str_lit)))(i)?;
Ok((i, Self::Extends(name)))
let (i, path) = preceded(ws(keyword("extends")), cut(ws(str_lit)))(i)?;
Ok((i, Self::Extends(Extends { path })))
}

fn include(i: &'a str) -> IResult<&'a str, Self> {
Expand Down Expand Up @@ -837,6 +837,11 @@ pub struct Include<'a> {
pub path: &'a str,
}

#[derive(Debug, PartialEq)]
pub struct Extends<'a> {
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 208441d

Please sign in to comment.