Add askama::filter_fn proc-macro for custom filters with named/optional arg support#545
Conversation
083ba75 to
b2be06e
Compare
|
If you don't mind, let's wait until https://github.com/GuillaumeGomez/askama/tree/tokenstream is finished because otherwise I'll take forever to finish my rebases. ^^' |
sure, no problem! |
|
I finally opened #558. Once merged and this PR has been rebased (sorry in advance for merge conflicts ><), time for reviews. :) |
cea2759 to
dd0e7f9
Compare
|
#558 was merged so when the merge conflicts are merged, let's review it! :) |
|
The Port askama to new filter_fn proc-macro-attr commit looks probably super dumb. |
|
Looks good to me overall. Please add ui errors test to ensure we emit errors correctly if the attributes is misused or malformed and then it looks good to me. |
I just tried writing some ui tests to check some of the signature validation. But I somehow always get this as compiler output: Did I accidentally use a nightly feature in my compiler error generation? |
|
That seems surprising. What does the generated code looks like? |
|
I created a new project, added askama as dependency and added this above the main function: #[askama::filter_fn]
pub fn filter1(input: usize) {
}This should trigger the assert: p_assert!(sig.inputs.len() >= 2, sig.span() => "Filter function missing required input and environment args")?;. if sig.inputs.len() >= 2 {
return Err(syn::Error::new(sig.span(), "Filter function missing required input and environment args"));
}The generated askama/askama_macros/src/lib.rs Line 25 in dd10583 I get a proper error message, with both nightly and stable compiler: But inside the ui-tests, for the same code, I just get |
|
Gonna try to understand what's going on when I have some time if you or @Kijewski didn't figure out by then. |
ceb85d2 to
634ec4c
Compare
|
Looks good to me, thanks! Just realized: doc is missing. Please add onto the book too. :) |
This is one of the next steps noted down in: #561 |
|
I'd prefer if docs came along the new feature directly. Simpler to keep track. :-/ |
|
@GuillaumeGomez Just updated the Book on custom filter implementations. |
GuillaumeGomez
left a comment
There was a problem hiding this comment.
Just one question then it's ready. :)
|
It's ready. Sorry for taking so long and thanks a lot for your work! |
No worries! |
|
Wow, that's super cool. Maybe we should add a list of projects/websites using |
askama::filter_fn proc-macro for custom filters with named/optional arg support



This adds an initial implementation of the
filter_fnproc-macro idea pitched here.Reading commit-by-commit will look less noisy, but the actual proc-macro implementation is one large commit either way.
The implementation has quite some logic to calculate generic parameters. I documented what is happening in the code comments as best as I could.
This:
#[askama::filter_fn]Some limitations apply. This currently:
whereclauses. (Bounds currently have to reside within the angle brackets)impl <Trait>notation for filter argumentsThis is my first proc-macro. So please bear with me 😅
The current version probably doesn't provide the best compiler diagnostics possible. I plan on incrementally improving this, if you have some pointers for me.