Skip to content

Commit

Permalink
derive: make syntax element errors more specific
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Sep 19, 2024
1 parent 231dbb2 commit 0c9351c
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions askama_derive/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,23 @@ impl<'a> TryInto<Syntax<'a>> for RawSyntax<'a> {
comment_end: self.comment_end.unwrap_or(default.comment_end),
};

for s in [
syntax.block_start,
syntax.block_end,
syntax.expr_start,
syntax.expr_end,
syntax.comment_start,
syntax.comment_end,
for (s, kind) in [
(syntax.block_start, "block_start"),
(syntax.block_end, "block_end"),
(syntax.expr_start, "expr_start"),
(syntax.expr_end, "expr_end"),
(syntax.comment_start, "comment_start"),
(syntax.comment_end, "comment_end"),
] {
if s.len() < 2 {
return Err(
format!("delimiters must be at least two characters long: {s:?}").into(),
);
return Err(format!(
"{kind} delimiter must be at least two characters long: {s:?}"
)
.into());
} else if s.chars().any(|c| c.is_whitespace()) {
return Err(format!("delimiters may not contain white spaces: {s:?}").into());
return Err(format!("{kind} delimiter may not contain whitespace: {s:?}").into());
} else if TWO_PLUS_CHAR_OPS.contains(&s) {
return Err(format!("delimiters may not contain operators: {s:?}").into());
return Err(format!("{kind} delimiter may not contain operators: {s:?}").into());
}
}

Expand Down Expand Up @@ -489,7 +490,7 @@ mod tests {
let config = Config::new(raw_config, None);
assert_eq!(
config.unwrap_err().msg,
r#"delimiters must be at least two characters long: "<""#,
r#"block_start delimiter must be at least two characters long: "<""#,
);

let raw_config = r#"
Expand All @@ -500,7 +501,7 @@ mod tests {
let config = Config::new(raw_config, None);
assert_eq!(
config.unwrap_err().msg,
r#"delimiters may not contain white spaces: " {{ ""#,
r#"block_start delimiter may not contain whitespace: " {{ ""#,
);

let raw_config = r#"
Expand Down

0 comments on commit 0c9351c

Please sign in to comment.