aboutsummaryrefslogtreecommitdiffstats
path: root/askama_derive/src
diff options
context:
space:
mode:
authorLibravatar René Kijewski <rene.kijewski@fu-berlin.de>2023-08-01 03:38:56 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-08-01 13:04:41 +0200
commit26f598c0d189769952b11ea9fe6168718f403590 (patch)
treeb8cab7749e993fd24c8d7abc7a126a1862a87c76 /askama_derive/src
parent108c4a6a33a9eeda3ecf76e5e09da5ccb9b188e8 (diff)
downloadaskama-26f598c0d189769952b11ea9fe6168718f403590.tar.gz
askama-26f598c0d189769952b11ea9fe6168718f403590.tar.bz2
askama-26f598c0d189769952b11ea9fe6168718f403590.zip
parser: add type for `Node::BlockDef`
Diffstat (limited to 'askama_derive/src')
-rw-r--r--askama_derive/src/generator.rs20
-rw-r--r--askama_derive/src/heritage.rs27
2 files changed, 13 insertions, 34 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs
index 60ffa27..beb3c49 100644
--- a/askama_derive/src/generator.rs
+++ b/askama_derive/src/generator.rs
@@ -650,8 +650,8 @@ impl<'a> Generator<'a> {
Node::Loop(ref loop_block) => {
size_hint += self.write_loop(ctx, buf, loop_block)?;
}
- Node::BlockDef(ws1, name, _, ws2) => {
- size_hint += self.write_block(buf, Some(name), Ws(ws1.0, ws2.1))?;
+ 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)?;
@@ -1163,26 +1163,18 @@ impl<'a> Generator<'a> {
// Get the block definition from the heritage chain
let heritage = self
.heritage
- .as_ref()
.ok_or_else(|| CompileError::from("no block ancestors available"))?;
- let (ctx, def) = heritage.blocks[cur.0].get(cur.1).ok_or_else(|| {
+ let (ctx, def) = *heritage.blocks[cur.0].get(cur.1).ok_or_else(|| {
CompileError::from(match name {
None => format!("no super() block found for block '{}'", cur.0),
Some(name) => format!("no block found for name '{name}'"),
})
})?;
- // Get the nodes and whitespace suppression data from the block definition
- let (ws1, nodes, ws2) = if let Node::BlockDef(ws1, _, nodes, ws2) = def {
- (ws1, nodes, ws2)
- } else {
- unreachable!()
- };
-
// Handle inner whitespace suppression spec and process block nodes
- self.prepare_ws(*ws1);
+ self.prepare_ws(def.ws1);
self.locals.push();
- let size_hint = self.handle(ctx, nodes, buf, AstLevel::Block)?;
+ let size_hint = self.handle(ctx, &def.nodes, buf, AstLevel::Block)?;
if !self.locals.is_current_empty() {
// Need to flush the buffer before popping the variable stack
@@ -1190,7 +1182,7 @@ impl<'a> Generator<'a> {
}
self.locals.pop();
- self.flush_ws(*ws2);
+ self.flush_ws(def.ws2);
// Restore original block context and set whitespace suppression for
// succeeding whitespace according to the outer WS spec
diff --git a/askama_derive/src/heritage.rs b/askama_derive/src/heritage.rs
index fbbb71f..2a7ef43 100644
--- a/askama_derive/src/heritage.rs
+++ b/askama_derive/src/heritage.rs
@@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};
use crate::config::Config;
use crate::CompileError;
-use parser::{Loop, Macro, Match, Node};
+use parser::{BlockDef, Loop, Macro, Match, Node};
pub(crate) struct Heritage<'a> {
pub(crate) root: &'a Context<'a>,
@@ -32,12 +32,12 @@ impl Heritage<'_> {
}
}
-type BlockAncestry<'a> = HashMap<&'a str, Vec<(&'a Context<'a>, &'a Node<'a>)>>;
+type BlockAncestry<'a> = HashMap<&'a str, Vec<(&'a Context<'a>, &'a BlockDef<'a>)>>;
pub(crate) struct Context<'a> {
pub(crate) nodes: &'a [Node<'a>],
pub(crate) extends: Option<PathBuf>,
- pub(crate) blocks: HashMap<&'a str, &'a Node<'a>>,
+ pub(crate) blocks: HashMap<&'a str, &'a BlockDef<'a>>,
pub(crate) macros: HashMap<&'a str, &'a Macro<'a>>,
pub(crate) imports: HashMap<&'a str, PathBuf>,
}
@@ -49,7 +49,7 @@ impl Context<'_> {
nodes: &'n [Node<'n>],
) -> Result<Context<'n>, CompileError> {
let mut extends = None;
- let mut blocks = Vec::new();
+ let mut blocks = HashMap::new();
let mut macros = HashMap::new();
let mut imports = HashMap::new();
let mut nested = vec![nodes];
@@ -76,11 +76,9 @@ impl Context<'_> {
"extends, macro or import blocks not allowed below top level".into(),
);
}
- def @ Node::BlockDef(_, _, _, _) => {
- blocks.push(def);
- if let Node::BlockDef(_, _, nodes, _) = def {
- nested.push(nodes);
- }
+ Node::BlockDef(b) => {
+ blocks.insert(b.name, b);
+ nested.push(&b.nodes);
}
Node::Cond(branches, _) => {
for cond in branches {
@@ -106,17 +104,6 @@ impl Context<'_> {
top = false;
}
- let blocks: HashMap<_, _> = blocks
- .iter()
- .map(|def| {
- if let Node::BlockDef(_, name, _, _) = def {
- (*name, *def)
- } else {
- unreachable!()
- }
- })
- .collect();
-
Ok(Context {
nodes,
extends,