Skip to content

Commit 48f07a4

Browse files
committed
Port askama to new filter_fn proc-macro-attr
1 parent d0b3074 commit 48f07a4

File tree

7 files changed

+33
-34
lines changed

7 files changed

+33
-34
lines changed

askama/src/filters/escape.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ impl<'a, T: HtmlSafe + ?Sized> AutoEscape for &AutoEscaper<'a, T, Html> {
256256
/// use askama::{filters::MaybeSafe, Result, Values};
257257
///
258258
/// // Do not actually use this filter! It's an intentionally bad example.
259+
/// #[askama::filter_fn]
259260
/// pub fn backdoor<T: std::fmt::Display>(
260261
/// s: T,
261262
/// _: &dyn Values,
@@ -386,6 +387,7 @@ const _: () = {
386387
/// use askama::{filters::Safe, Result, Values};
387388
///
388389
/// // Do not actually use this filter! It's an intentionally bad example.
390+
/// #[askama::filter_fn]
389391
/// pub fn strip_except_apos(s: impl ToString, _: &dyn Values) -> Result<Safe<String>> {
390392
/// Ok(Safe(s
391393
/// .to_string()

askama_derive/src/generator/filter.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,33 @@ impl<'a> Generator<'a, '_> {
7676
args: &[WithSpan<'a, Expr<'a>>],
7777
node: Span<'_>,
7878
) -> Result<DisplayWrap, CompileError> {
79-
ensure_no_named_arguments(ctx, path.last().unwrap().name, args, node)?;
80-
self.visit_path(buf, path);
81-
buf.write('(');
82-
self.visit_arg(ctx, buf, &args[0])?;
83-
buf.write(",__askama_values");
84-
if args.len() > 1 {
85-
buf.write(',');
86-
self.visit_args(ctx, buf, &args[1..])?;
79+
buf.write("{");
80+
let prefix_path = &path[..path.len() - 1];
81+
self.visit_path(buf, prefix_path);
82+
let filter = path
83+
.last()
84+
.ok_or_else(|| ctx.generate_error("Filter invocations don't support generics", node))?;
85+
if !prefix_path.is_empty() {
86+
buf.write("::");
8787
}
88-
buf.write(")?");
88+
buf.write(format!("{}::default()", filter.name));
89+
90+
for (arg_idx, arg) in args[1..].iter().enumerate() {
91+
let expr: &Expr<'a> = arg;
92+
if let Expr::NamedArgument(name, arg) = expr {
93+
buf.write(format!(".with_{name}("));
94+
self.visit_arg(ctx, buf, arg)?;
95+
buf.write(")");
96+
} else {
97+
buf.write(format!(".with_{arg_idx}("));
98+
self.visit_arg(ctx, buf, arg)?;
99+
buf.write(")");
100+
}
101+
}
102+
103+
buf.write(".execute(");
104+
self.visit_arg(ctx, buf, &args[0])?;
105+
buf.write(",__askama_values)?}");
89106
Ok(DisplayWrap::Unwrapped)
90107
}
91108

askama_derive/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ fn test_filter_with_path() {
11971197
r#"
11981198
match (
11991199
&((&&askama::filters::AutoEscaper::new(
1200-
&(b::c::d(&(self.a), __askama_values)?),
1200+
&({ b::c::d::default().execute(&(self.a), __askama_values)? }),
12011201
askama::filters::Text,
12021202
))
12031203
.askama_auto_escape()?),

testing/tests/book_example_runtime_values_in_custom_filters.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use askama::{Template, Values};
99
mod filters {
1010
use super::*;
1111

12+
#[askama::filter_fn]
1213
pub fn cased(value: impl ToString, values: &dyn Values) -> askama::Result<String> {
1314
let value = value.to_string();
1415
let case = askama::get_value(values, "case").ok();

testing/tests/filters.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,13 @@ fn filter_fmt() {
9898
mod filters {
9999
use askama::Values;
100100

101+
#[askama::filter_fn]
101102
pub fn myfilter(s: &str, _: &dyn Values) -> ::askama::Result<String> {
102103
Ok(s.replace("oo", "aa"))
103104
}
104105

105106
// for test_nested_filter_ref
107+
#[askama::filter_fn]
106108
pub fn mytrim(s: &dyn std::fmt::Display, _: &dyn Values) -> ::askama::Result<String> {
107109
Ok(s.to_string().trim().to_owned())
108110
}
@@ -477,6 +479,7 @@ fn test_whitespace_around_filter_operator() {
477479
assert_eq!(S.render().unwrap(), "12\n8\n4");
478480
}
479481

482+
#[askama::filter_fn]
480483
pub(crate) fn minus(value: &str, _: &dyn askama::Values) -> askama::Result<i32> {
481484
Ok(-value.parse().map_err(askama::Error::custom)?)
482485
}

testing/tests/ui/custom-filter-with-named-arg.rs

Lines changed: 0 additions & 17 deletions
This file was deleted.

testing/tests/ui/custom-filter-with-named-arg.stderr

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)