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 6b0ea27 commit 9beca5e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
7 changes: 5 additions & 2 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 @@ -1429,7 +1429,10 @@ impl<'a> Generator<'a> {
})
.ok_or_else(|| {
ctx.generate_error(
&format!("invalid escaper '{name}' for `escape` filter"),
&format!(
"invalid escaper '{name}' for `escape` filter. {}",
MsgValidEscapers(&self.input.config.escapers),
),
node,
)
})?,
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
16 changes: 16 additions & 0 deletions rinja_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod input;
#[cfg(test)]
mod tests;

use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt;
use std::path::Path;
Expand Down Expand Up @@ -231,6 +232,21 @@ 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 mut exts = self
.0
.iter()
.flat_map(|(exts, _)| exts)
.map(|x| format!("{x:?}"))
.collect::<Vec<_>>();
exts.sort();
write!(f, "The available extensions are: {}", exts.join(", "))
}
}

// 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: "", "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: "", "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 9beca5e

Please sign in to comment.