Skip to content

Commit

Permalink
More useful error message and UI tests for missing escaper
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Jul 7, 2024
1 parent 0aead15 commit ba0d8ac
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion rinja/src/filters/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::str;

/// Marks a string (or other `Display` type) as safe
///
/// Use this is you want to allow markup in an expression, or if you know
/// Use this if you want to allow markup in an expression, or if you know
/// that the expression's contents don't need to be escaped.
///
/// Rinja will automatically insert the first (`Escaper`) argument,
Expand Down
5 changes: 4 additions & 1 deletion rinja_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,10 @@ impl<'a> Generator<'a> {
.contains(&Cow::Borrowed(name))
.then_some(path.as_ref())
})
.ok_or_else(|| ctx.generate_error("invalid escaper for escape filter", node))?,
.ok_or_else(|| ctx.generate_error(
&format!("invalid escaper '{name}' for `escape` filter"),
node,
))?,
None => self.input.escaper,
};
buf.write(format_args!("{CRATE}::filters::escape("));
Expand Down
22 changes: 22 additions & 0 deletions testing/tests/ui/no-such-escaper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use rinja::Template;

#[derive(Template)]
#[template(
ext = "html",
source = r#"In LaTeX you write `{{text}}` like `{{text|escape("latex")}}`."#,
)]
struct LocalEscaper<'a> {
text: &'a str,
}

#[derive(Template)]
#[template(
ext = "tex",
source = r#"In HTML you write `{{text}}` like `{{text|escape("html")}}`."#,
)]
struct GlobalEscaper<'a> {
text: &'a str,
}

fn main() {
}
17 changes: 17 additions & 0 deletions testing/tests/ui/no-such-escaper.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: invalid escaper 'latex' for `escape` filter
--> LocalEscaper.html:1:38
"text|escape(\"latex\")}}`."
--> tests/ui/no-such-escaper.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)

error: no escaper defined for extension 'tex'
--> tests/ui/no-such-escaper.rs:12:10
|
12 | #[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 ba0d8ac

Please sign in to comment.