Skip to content

Add new reject filter#430

Merged
Kijewski merged 4 commits intoaskama-rs:masterfrom
GuillaumeGomez:reject-filter
Jun 6, 2025
Merged

Add new reject filter#430
Kijewski merged 4 commits intoaskama-rs:masterfrom
GuillaumeGomez:reject-filter

Conversation

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

This one is interesting because whether you give it a path or a value, it will not call the same rust function. Found it interesting so implemented it.

@Kijewski
Copy link
Copy Markdown
Member

Should we (try to) implement closures like {{ x | reject(|x| *x % 2 != 0) }} to make this filter even more useful?

Comment thread book/src/filters.md
For more control over the filtering, you can use a callback instead. Declare a function:

```jinja
fn is_odd(value: &&u32) -> bool {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were does the second reference come from?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter method. Made me sad too.

Comment thread askama/src/filters/builtin.rs Outdated
/// ```
pub fn reject_with<T: PartialEq, F: Fn(&T) -> bool>(
it: impl IntoIterator<Item = T>,
callback: F,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please make this F: FnMut…, mut callback: F?

Comment thread askama/src/filters/builtin.rs Outdated
/// ```
pub fn reject<T: PartialEq>(
it: impl IntoIterator<Item = T>,
filter: T,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better filter: &T, with an additional &(…) in the generated code. This way (I think) the type-coercion might work better if the input is already an &T.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you mean two &&? There is one already.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, added a second one.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that's not what I meant, but I confused myself with my comment. Let me think for a second or 86400. :)

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator Author

Should we (try to) implement closures like {{ x | reject(|x| *x % 2 != 0) }} to make this filter even more useful?

I thought about it and concluded that we definitely didn't want to implement closures in our parser.

* Re-use iterator getting from `{% for %}`'s implementation
* Don't use edition 2024 features
@Kijewski
Copy link
Copy Markdown
Member

Kijewski commented Jun 6, 2025

I tried to get rid of the second reference, but I failed. :( Still, I think that the changes I came up with during my tests could be useful. Please have a look.

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator Author

Looks good to me (can't approve if it's on my PR 😞).

Kijewski
Kijewski previously approved these changes Jun 6, 2025
Copy link
Copy Markdown
Member

@Kijewski Kijewski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can :)

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator Author

Show off. 😛

@Kijewski
Copy link
Copy Markdown
Member

Kijewski commented Jun 6, 2025

Oops, the filter is not display wrapped. I don't think it would be matter, actually, because impl Iterator does not implement Display anyway, but better safe than sorry.

@Kijewski Kijewski merged commit 478d726 into askama-rs:master Jun 6, 2025
42 checks passed
@GuillaumeGomez GuillaumeGomez deleted the reject-filter branch June 6, 2025 13:49
@Kijewski Kijewski mentioned this pull request Jun 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants