Skip to content

Add support for macro default value generics#522

Merged
Kijewski merged 3 commits intoaskama-rs:masterfrom
GuillaumeGomez:macro-default-value-generics
Jul 20, 2025
Merged

Add support for macro default value generics#522
Kijewski merged 3 commits intoaskama-rs:masterfrom
GuillaumeGomez:macro-default-value-generics

Conversation

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

Fixes #513.

buf.write('&');
}
self.visit_path(buf, path);
self.visit_macro_path(buf, path);
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.

I'm wondering if I should allow TyGenerics to have path with generics in them... No limit to generics recursion. =D

}
// We special-case "askama::get_value".
Expr::Path(path) if path == &["askama", "get_value"] => {
Expr::Path(path) if matches!(path.as_slice(), [part1, part2] if part1.generics.is_empty() && part1.name == "askama" && part2.name == "get_value") =>
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.

if let is not supported as pattern guard. :'(

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.

Yeah, I hope this restriction is lifted in the future.

buf,
args,
generics,
&path[1].generics,
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.

Should I use path.last().unwrap().generics instead?

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 you could add a comment that this is the "get_value" part?

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.

Added the code comment.

}

pub(super) fn visit_path(&mut self, buf: &mut Buffer, path: &[&str]) -> DisplayWrap {
pub(super) fn visit_macro_path(&mut self, buf: &mut Buffer, path: &[&str]) -> DisplayWrap {
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.

It's the same as visit_path except it's only a list of strings, no generics in there.

return self.visit_custom_filter_with_path(ctx, buf, path, args, generics, node);
}
PathOrIdentifier::Identifier(name) => *name,
let (name, generics): (&str, &[WithSpan<'_, TyGenerics<'_>>]) = match name {
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.

We could cheat here and skip all ensure_no_generics calls (which would be nice) but it would require a small refactor of this code, increasing the diff. I suppose it's better as a follow-up?

@GuillaumeGomez GuillaumeGomez force-pushed the macro-default-value-generics branch from dddac55 to 592cac9 Compare July 20, 2025 21:42
@GuillaumeGomez
Copy link
Copy Markdown
Collaborator Author

Fixed merge conflicts as well. :)

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.

Nice, thanks!

@Kijewski Kijewski merged commit cf558be into askama-rs:master Jul 20, 2025
42 checks passed
@GuillaumeGomez GuillaumeGomez deleted the macro-default-value-generics branch July 21, 2025 08:12
@Kijewski Kijewski mentioned this pull request Jul 21, 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.

Expressions currently don't support generics in paths

2 participants