From cd075630eab578a66afec6000ce0f266e6b281e8 Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Tue, 1 Aug 2023 03:18:10 +0200 Subject: parser: add type for `Node::Import` --- askama_derive/src/generator.rs | 8 +++---- askama_derive/src/heritage.rs | 8 +++---- askama_parser/src/lib.rs | 2 +- askama_parser/src/node.rs | 48 ++++++++++++++++++++++++++++-------------- 4 files changed, 41 insertions(+), 25 deletions(-) diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 1c6e722..8a2c0e3 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -228,8 +228,8 @@ fn find_used_templates( let source = get_template_source(&extends)?; check.push((extends, source)); } - Node::Import(_, import, _) => { - let import = input.config.find_template(import, Some(&path))?; + Node::Import(import) => { + let import = input.config.find_template(import.path, Some(&path))?; let source = get_template_source(&import)?; check.push((import, source)); } @@ -671,11 +671,11 @@ impl<'a> Generator<'a> { self.visit_lit(lws, val, rws); self.handle_ws(ws2); } - Node::Import(ws, _, _) => { + Node::Import(ref i) => { if level != AstLevel::Top { return Err("import blocks only allowed at the top level".into()); } - self.handle_ws(ws); + self.handle_ws(i.ws); } Node::Extends(_) => { if level != AstLevel::Top { diff --git a/askama_derive/src/heritage.rs b/askama_derive/src/heritage.rs index 801b109..9f642e3 100644 --- a/askama_derive/src/heritage.rs +++ b/askama_derive/src/heritage.rs @@ -67,11 +67,11 @@ impl Context<'_> { Node::Macro(m) if top => { macros.insert(m.name, m); } - Node::Import(_, import_path, scope) if top => { - let path = config.find_template(import_path, Some(path))?; - imports.insert(*scope, path); + Node::Import(import) if top => { + let path = config.find_template(import.path, Some(path))?; + imports.insert(import.scope, path); } - Node::Extends(_) | Node::Macro(_) | Node::Import(_, _, _) if !top => { + Node::Extends(_) | Node::Macro(_) | Node::Import(_) if !top => { return Err( "extends, macro or import blocks not allowed below top level".into(), ); 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>, 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". -- cgit