From 65fb5630825b62ca959fcb55252d1657aefea0f3 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Mon, 3 Jul 2023 09:30:28 +0200 Subject: parser: define a struct for Match --- askama_derive/src/generator.rs | 7 +++---- askama_derive/src/heritage.rs | 4 ++-- askama_parser/src/node.rs | 25 ++++++++++++++++++++++--- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index addde7a..66f1b1c 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -796,8 +796,7 @@ impl<'a> Generator<'a> { let mut arm_size = 0; for (i, arm) in arms.iter().enumerate() { - let &(ws, ref target, ref body) = arm; - self.handle_ws(ws); + self.handle_ws(arm.ws); if i > 0 { arm_sizes.push(arm_size + self.write_buf_writable(buf)?); @@ -807,10 +806,10 @@ impl<'a> Generator<'a> { } self.locals.push(); - self.visit_target(buf, true, true, target); + self.visit_target(buf, true, true, &arm.target); buf.writeln(" => {")?; - arm_size = self.handle(ctx, body, buf, AstLevel::Nested)?; + arm_size = self.handle(ctx, &arm.block, buf, AstLevel::Nested)?; } self.handle_ws(ws2); diff --git a/askama_derive/src/heritage.rs b/askama_derive/src/heritage.rs index 66f1a1a..342416e 100644 --- a/askama_derive/src/heritage.rs +++ b/askama_derive/src/heritage.rs @@ -94,8 +94,8 @@ impl Context<'_> { nested.push(else_block); } Node::Match(_, _, arms, _) => { - for (_, _, arm) in arms { - nested.push(arm); + for arm in arms { + nested.push(&arm.block); } } _ => {} diff --git a/askama_parser/src/node.rs b/askama_parser/src/node.rs index 84b4911..54dc333 100644 --- a/askama_parser/src/node.rs +++ b/askama_parser/src/node.rs @@ -581,7 +581,12 @@ pub struct Loop<'a> { pub ws3: Ws, } -pub type When<'a> = (Ws, Target<'a>, Vec>); +#[derive(Debug, PartialEq)] +pub struct When<'a> { + pub ws: Ws, + pub target: Target<'a>, + pub block: Vec>, +} #[derive(Debug, PartialEq)] pub struct Macro<'a> { @@ -669,7 +674,14 @@ fn match_else_block<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, When<'a>> ))), )); let (i, (_, pws, _, (nws, _, block))) = p(i)?; - Ok((i, (Ws(pws, nws), Target::Name("_"), block))) + Ok(( + i, + When { + ws: Ws(pws, nws), + target: Target::Name("_"), + block, + }, + )) } fn when_block<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, When<'a>> { @@ -685,7 +697,14 @@ fn when_block<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, When<'a>> { ))), )); let (i, (_, pws, _, (target, nws, _, block))) = p(i)?; - Ok((i, (Ws(pws, nws), target, block))) + Ok(( + i, + When { + ws: Ws(pws, nws), + target, + block, + }, + )) } fn parse_loop_content<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Vec>> { -- cgit