Skip to content

Commit

Permalink
Allow comments in {% match %} and remove panic!
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski authored and djc committed Jan 31, 2022
1 parent da0b6ea commit 9187470
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
21 changes: 2 additions & 19 deletions askama_shared/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ fn block_match<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Node<'a>> {
opt(char('-')),
|i| tag_block_end(i, s),
cut(tuple((
opt(|i| take_content(i, s)),
ws(many0(ws(value((), |i| block_comment(i, s))))),
many1(|i| when_block(i, s)),
cut(tuple((
opt(|i| match_else_block(i, s)),
Expand All @@ -818,30 +818,13 @@ fn block_match<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Node<'a>> {
))),
))),
));
let (i, (pws1, _, (expr, nws1, _, (inter, arms, (else_arm, (_, pws2, _, nws2)))))) = p(i)?;
let (i, (pws1, _, (expr, nws1, _, (_, arms, (else_arm, (_, pws2, _, nws2)))))) = p(i)?;

let mut arms = arms;
if let Some(arm) = else_arm {
arms.push(arm);
}

match inter {
Some(Node::Lit(_, val, rws)) => {
assert!(
val.is_empty(),
"only whitespace allowed between match and first when, found {}",
val
);
assert!(
rws.is_empty(),
"only whitespace allowed between match and first when, found {}",
rws
);
}
None => {}
_ => panic!("only literals allowed between match and first when"),
}

Ok((
i,
Node::Match(
Expand Down
25 changes: 25 additions & 0 deletions testing/tests/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,28 @@ fn test_match_option_result_option() {
};
assert_eq!(s.render().unwrap(), "num=4711");
}

#[derive(Template)]
#[template(
ext = "txt",
source = r#"
{%- match good -%}
{#- when good, then good -#}
{%- when true -%}
good
{%- when _ -%}
bad
{%- endmatch -%}"#
)]
struct MatchWithComment {
good: bool,
}

#[test]
fn test_match_with_comment() {
let s = MatchWithComment { good: true };
assert_eq!(s.render().unwrap(), "good");

let s = MatchWithComment { good: false };
assert_eq!(s.render().unwrap(), "bad");
}
20 changes: 20 additions & 0 deletions testing/tests/ui/match_with_extra.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use askama::Template;

#[derive(Template)]
#[template(
ext = "txt",
source = r#"
{%- match good -%}
// Help, I forgot how to write comments!
{%- when true %}
good
{%- when _ -%}
bad
{%- endmatch -%}"#
)]
struct MatchWithExtra {
good: bool,
}

fn main() {
}
8 changes: 8 additions & 0 deletions testing/tests/ui/match_with_extra.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: problems parsing template source at row 3, column 4 near:
"// Help, I forgot how to write comments!"...
--> tests/ui/match_with_extra.rs:3:10
|
3 | #[derive(Template)]
| ^^^^^^^^
|
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit 9187470

Please sign in to comment.