Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow comments in {% match %} and remove panic!() #616

Merged
merged 1 commit into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)