aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar René Kijewski <kijewski@library.vetmed.fu-berlin.de>2022-01-31 09:02:17 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2022-01-31 11:30:35 +0100
commit91874702f0f3581549c129369a8bc0783006b5fa (patch)
tree5b70acb63e7634cc8b9d1dfc29eabd7b4d30238a
parentda0b6ead0e75082bfffa24f8529d1f83961ba45c (diff)
downloadaskama-91874702f0f3581549c129369a8bc0783006b5fa.tar.gz
askama-91874702f0f3581549c129369a8bc0783006b5fa.tar.bz2
askama-91874702f0f3581549c129369a8bc0783006b5fa.zip
Allow comments in `{% match %}` and remove panic!
-rw-r--r--askama_shared/src/parser.rs21
-rw-r--r--testing/tests/matches.rs25
-rw-r--r--testing/tests/ui/match_with_extra.rs20
-rw-r--r--testing/tests/ui/match_with_extra.stderr8
4 files changed, 55 insertions, 19 deletions
diff --git a/askama_shared/src/parser.rs b/askama_shared/src/parser.rs
index 085d73b..97ccf6c 100644
--- a/askama_shared/src/parser.rs
+++ b/askama_shared/src/parser.rs
@@ -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)),
@@ -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(
diff --git a/testing/tests/matches.rs b/testing/tests/matches.rs
index 206cd1d..35c1204 100644
--- a/testing/tests/matches.rs
+++ b/testing/tests/matches.rs
@@ -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");
+}
diff --git a/testing/tests/ui/match_with_extra.rs b/testing/tests/ui/match_with_extra.rs
new file mode 100644
index 0000000..528441e
--- /dev/null
+++ b/testing/tests/ui/match_with_extra.rs
@@ -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() {
+}
diff --git a/testing/tests/ui/match_with_extra.stderr b/testing/tests/ui/match_with_extra.stderr
new file mode 100644
index 0000000..8f515bd
--- /dev/null
+++ b/testing/tests/ui/match_with_extra.stderr
@@ -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)