Skip to content

Commit

Permalink
Add MsgValidEscapers to show list of configured escapers
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Jul 7, 2024
1 parent ba0d8ac commit b009d48
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
15 changes: 10 additions & 5 deletions rinja_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use quote::quote;
use crate::config::WhitespaceHandling;
use crate::heritage::{Context, Heritage};
use crate::input::{Source, TemplateInput};
use crate::{CompileError, CRATE};
use crate::{CompileError, MsgValidEscapers, CRATE};

pub(crate) struct Generator<'a> {
// The template input state: original struct AST and attributes
Expand Down Expand Up @@ -1427,10 +1427,15 @@ impl<'a> Generator<'a> {
.contains(&Cow::Borrowed(name))
.then_some(path.as_ref())
})
.ok_or_else(|| ctx.generate_error(
&format!("invalid escaper '{name}' for `escape` filter"),
node,
))?,
.ok_or_else(|| {
ctx.generate_error(
&format!(
"invalid escaper '{name}' for `escape` filter. {}",
MsgValidEscapers(&self.input.config.escapers),
),
node,
)
})?,
None => self.input.escaper,
};
buf.write(format_args!("{CRATE}::filters::escape("));
Expand Down
7 changes: 5 additions & 2 deletions rinja_derive/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use quote::ToTokens;
use syn::punctuated::Punctuated;

use crate::config::{get_template_source, Config};
use crate::CompileError;
use crate::{CompileError, MsgValidEscapers};

pub(crate) struct TemplateInput<'a> {
pub(crate) ast: &'a syn::DeriveInput,
Expand Down Expand Up @@ -87,7 +87,10 @@ impl TemplateInput<'_> {
.then_some(path.as_ref())
})
.ok_or_else(|| {
CompileError::no_file_info(format!("no escaper defined for extension '{escaping}'"))
CompileError::no_file_info(format!(
"no escaper defined for extension '{escaping}'. {}",
MsgValidEscapers(&config.escapers),
))
})?;

let mime_type =
Expand Down
27 changes: 26 additions & 1 deletion rinja_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ mod input;
#[cfg(test)]
mod tests;

use std::collections::HashMap;
use std::borrow::Cow;
use std::collections::{BTreeSet, HashMap};
use std::fmt;
use std::path::Path;

Expand Down Expand Up @@ -231,6 +232,30 @@ impl fmt::Display for FileInfo<'_> {
}
}

struct MsgValidEscapers<'a>(&'a [(Vec<Cow<'a, str>>, Cow<'a, str>)]);

impl fmt::Display for MsgValidEscapers<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let exts: BTreeSet<&str> = self
.0
.iter()
.flat_map(|(exts, _)| exts)
.map(|x| x.as_ref())
.collect();
f.write_str("The available extensions are:")?;
for (i, mut e) in exts.iter().copied().enumerate() {
if e.is_empty() {
e = r#""" <empty string>"#;
}
match i {
0 => write!(f, " {e}")?,
_ => write!(f, ", {e}")?,
}
}
Ok(())
}
}

// This is used by the code generator to decide whether a named filter is part of
// Rinja or should refer to a local `filters` module. It should contain all the
// filters shipped with Rinja, even the optional ones (since optional inclusion
Expand Down
4 changes: 2 additions & 2 deletions testing/tests/ui/no-such-escaper.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: invalid escaper 'latex' for `escape` filter
error: invalid escaper 'latex' for `escape` filter. The available extensions are: "" <empty string>, htm, html, j2, jinja, jinja2, md, none, svg, txt, xml, yml
--> LocalEscaper.html:1:38
"text|escape(\"latex\")}}`."
--> tests/ui/no-such-escaper.rs:3:10
Expand All @@ -8,7 +8,7 @@ error: invalid escaper 'latex' for `escape` filter
|
= 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'
error: no escaper defined for extension 'tex'. The available extensions are: "" <empty string>, htm, html, j2, jinja, jinja2, md, none, svg, txt, xml, yml
--> tests/ui/no-such-escaper.rs:12:10
|
12 | #[derive(Template)]
Expand Down

0 comments on commit b009d48

Please sign in to comment.