-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
derive: unify filter argument handling #318
base: master
Are you sure you want to change the base?
Conversation
rinja_derive/src/generator/expr.rs
Outdated
"unexpected argument{} in `{name}` filter", | ||
if args.len() == 2 { "" } else { "s" }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"unexpected argument{} in `{name}` filter", | |
if args.len() == 2 { "" } else { "s" }, | |
"expected 1 argument in `{name}` filter, found {}", | |
args.len(), |
Better than handling plural: not handling plural. :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, but the "1" is not true in all cases. :) I reworded the message, and implemented it with less magic. 🪄
98e1791
to
6a95aee
Compare
Conflict. I'll approve once updated otherwise it doesn't work. :') |
b4dfbbe
to
a9166d3
Compare
I expanded slightly on the initial idea. |
if BUILTIN_FILTERS_NEED_ALLOC.contains(&name) { | ||
ensure_filter_has_feature_alloc(ctx, name, node)?; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check was in the wrong method. Moved to _visit_builtin_filter
.
@@ -303,43 +297,18 @@ impl<'a> Generator<'a, '_> { | |||
buf: &mut Buffer, | |||
name: &str, | |||
args: &[WithSpan<'_, Expr<'a>>], | |||
generics: &[WithSpan<'_, TyGenerics<'_>>], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No built-in filter needs generics.
rinja_derive/src/generator/expr.rs
Outdated
return self._visit_builtin_filter(ctx, buf, name, args, generics, node); | ||
"urlencode" | "urlencode_strict" => Self::_visit_urlencode_filter, | ||
name if BUILTIN_FILTERS.contains(&name) => Self::_visit_builtin_filter, | ||
"value" => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why moving "value"
below the name
check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You still didn't answer to this one. 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved the filters that return
to the bottom, to make it easier to follow the code. In the last/current implementation it's strictly alphabetical again. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case I don't think it's the right way to do it. In the (unlikely) case we added (by mistake) value
into BUILTIN_FILTERS
, then we'd get no way of noticing that the real value
filter is not called until the tests failed.
But anyway, in the current case I think the code can be improved a bit (adding suggestion comment below). 😛
rinja_derive/src/generator/expr.rs
Outdated
"linebreaks" => Self::_visit_linebreaks_filter, | ||
"linebreaksbr" => Self::_visit_linebreaksbr_filter, | ||
"paragraphbreaks" => Self::_visit_paragraphbreaks_filter, | ||
"linebreaks" | "linebreaksbr" | "paragraphbreaks" => Self::_visit_linebreaks_filters, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we're back giving the filter name as argument? That's one extra argument we could skip but if you think it's better this way, then let's go with it.
rinja_derive/src/generator/expr.rs
Outdated
_ => { | ||
return self._visit_custom_filter(ctx, buf, name, args, generics); | ||
} | ||
type Filter = for<'a> fn( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering the amount of code it adds, what's the perf gain?
The first two tests are only to measure that my CPU is probably running hotter on the second run. (It shows +216.0°C, but I'm fairly confident that the measurement is incorrect. :D) |
I'm shared here: the code complexity increases a lot, and although % improvement is noticeable, we're talking in µs. I don't think it's noticeable for a user and therefore, I don't think it's worth the extra complexity. |
e611aa3
to
6ac3ac0
Compare
Back to a simple |
rinja_derive/src/generator/expr.rs
Outdated
name if BUILTIN_FILTERS.contains(&name) => Self::_visit_builtin_filter, | ||
"value" => { | ||
return self._visit_value(ctx, buf, args, generics, node, "`value` filter"); | ||
} | ||
_ => { | ||
return self._visit_custom_filter(ctx, buf, name, args, generics); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name if BUILTIN_FILTERS.contains(&name) => Self::_visit_builtin_filter, | |
"value" => { | |
return self._visit_value(ctx, buf, args, generics, node, "`value` filter"); | |
} | |
_ => { | |
return self._visit_custom_filter(ctx, buf, name, args, generics); | |
} | |
"value" => { | |
return self._visit_value(ctx, buf, args, generics, node, "`value` filter"); | |
} | |
name => if BUILTIN_FILTERS.contains(&name) { | |
Self::_visit_builtin_filter | |
} else { | |
return self._visit_custom_filter(ctx, buf, name, args, generics); | |
}, |
This allows to remove one arm and have the contains
call to be done after all other strings have been checked.
No description provided.