mirror of
https://github.com/askama-rs/askama.git
synced 2025-10-01 23:10:54 +00:00
Allow omitting "with" keyword in match blocks
Askama uses the syntax `{% when Variant with (parameters) %}` in `{% match %}` blocks. This is done because Askama does not implement the whole pattern matching of Rust's `match` statements. This PR wants to bring Askama a step closer Rust's matching, so the "with" keyword should not be needed anymore.
This commit is contained in:
parent
9e7fe8f2f9
commit
1622df7aee
@ -550,7 +550,7 @@ fn parameters(i: &[u8]) -> IResult<&[u8], Vec<&str>> {
|
||||
|
||||
fn with_parameters(i: &[u8]) -> IResult<&[u8], MatchParameters<'_>> {
|
||||
let (i, (_, value)) = tuple((
|
||||
tag("with"),
|
||||
opt(tag("with")),
|
||||
alt((match_simple_parameters, match_named_parameters)),
|
||||
))(i)?;
|
||||
Ok((i, value))
|
||||
|
@ -113,3 +113,20 @@ fn test_match_no_whitespace() {
|
||||
let s = MatchNoWhitespace { foo: Some(1) };
|
||||
assert_eq!(s.render().unwrap(), "1");
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(
|
||||
source = "{% match foo %}{% when Some(bar) %}{{ bar }}{% when None %}{% endmatch %}",
|
||||
ext = "txt"
|
||||
)]
|
||||
struct MatchWithoutWithKeyword {
|
||||
foo: Option<usize>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_match_without_with_keyword() {
|
||||
let s = MatchWithoutWithKeyword { foo: Some(1) };
|
||||
assert_eq!(s.render().unwrap(), "1");
|
||||
let s = MatchWithoutWithKeyword { foo: None };
|
||||
assert_eq!(s.render().unwrap(), "");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user