From 70b41f704eea9c5f4c589a5488667e96dbeb357b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Mon, 21 Jul 2025 22:12:47 +0200 Subject: [PATCH] Address comments --- askama/src/filters/default.rs | 61 +++++++++++++---------------------- book/src/filters.md | 4 +-- 2 files changed, 25 insertions(+), 40 deletions(-) diff --git a/askama/src/filters/default.rs b/askama/src/filters/default.rs index fd6e0f4e..98d0a70b 100644 --- a/askama/src/filters/default.rs +++ b/askama/src/filters/default.rs @@ -190,7 +190,7 @@ impl DefaultFilterable for Saturating { } macro_rules! impl_for_int { - ($($ty:ty)*) => { $( + ($($name:ident : $ty:ty)*) => { $( #[doc = concat!("A [`", stringify!($ty), "`] has a value if it is not `0`.")] impl DefaultFilterable for $ty { type Filtered<'a> = $ty; @@ -204,16 +204,7 @@ macro_rules! impl_for_int { } } } - )* }; -} -impl_for_int!( - u8 u16 u32 u64 u128 usize - i8 i16 i32 i64 i128 isize -); - -macro_rules! impl_for_non_zero { - ($($name:ident : $ty:ty)*) => { $( #[doc = concat!("A [`", stringify!($name), "`][num::", stringify!($name),"] always has a value.")] impl DefaultFilterable for num::$name { type Filtered<'a> = $ty; @@ -227,7 +218,7 @@ macro_rules! impl_for_non_zero { )* }; } -impl_for_non_zero!( +impl_for_int!( NonZeroU8:u8 NonZeroU16:u16 NonZeroU32:u32 NonZeroU64:u64 NonZeroU128:u128 NonZeroUsize:usize NonZeroI8:i8 NonZeroI16:i16 NonZeroI32:i32 NonZeroI64:i64 NonZeroI128:i128 NonZeroIsize:isize ); @@ -246,37 +237,31 @@ impl DefaultFilterable for bool { } } -/// An `f32` has a value if it is [`Normal`][FpCategory::Normal], i.e. it is not zero, -/// not sub-normal, not infinite and not NaN. -impl DefaultFilterable for f32 { - type Filtered<'a> - = Self - where - Self: 'a; +macro_rules! impl_for_float { + ($($ty:ty)*) => { $( + #[doc = concat!( + "An [`", + stringify!($ty), + "`] has a value if it is [`Normal`][FpCategory::Normal], i.e. it is not zero, \ + not sub-normal, not infinite and not NaN." + )] + impl DefaultFilterable for $ty { + type Filtered<'a> + = Self + where + Self: 'a; - type Error = Infallible; + type Error = Infallible; - #[inline] - fn as_filtered(&self) -> Result>, Self::Error> { - Ok((self.classify() == FpCategory::Normal).then_some(*self)) - } + #[inline] + fn as_filtered(&self) -> Result>, Self::Error> { + Ok((self.classify() == FpCategory::Normal).then_some(*self)) + } + } + )* } } -/// An `f64` has a value if it is [`Normal`](FpCategory::Normal), i.e. it is not zero, -/// not sub-normal, not infinite and not NaN. -impl DefaultFilterable for f64 { - type Filtered<'a> - = Self - where - Self: 'a; - - type Error = Infallible; - - #[inline] - fn as_filtered(&self) -> Result>, Self::Error> { - Ok((self.classify() == FpCategory::Normal).then_some(*self)) - } -} +impl_for_float!(f32 f64); #[test] #[cfg(feature = "std")] diff --git a/book/src/filters.md b/book/src/filters.md index b7a27339..8bede09d 100644 --- a/book/src/filters.md +++ b/book/src/filters.md @@ -110,12 +110,12 @@ Output: ``` This filter works like the Jinja filter of the [same name](https://jinja.palletsprojects.com/en/stable/templates/#jinja-filters.default). -If the second argument is not an boolean `true`, then the filter behaves like [`|defined_or`][#defined_or]. +If the second argument is not a boolean `true`, then the filter behaves like [`|defined_or`][#defined_or]. If it is supplied and `true`, then the filter behaves like [`|assigned_or`][#assigned_or]. **This filter exists for compatibility with Jinja.** Askama provides [`|defined_or`][#defined_or] and [`|assigned_or`][#assigned_or] which both -better express the intention and should generally usually be used instead of this filter. +better express the intention and should generally be used instead of this filter. ### defined_or [#defined_or]: #defined_or