Add named arguments for filters #403
Conversation
3fa45ee to
06500bd
Compare
|
The book is not updated, yet, but the changes to the code can already be reviewed. The diff is rather big, and should be digested commit-by-commit, but I think the change is worth it. |
dcaaddb to
79f8389
Compare
8d9cc26 to
9aa2c3e
Compare
9aa2c3e to
74130a1
Compare
| format_args!( | ||
| "filter `{name}` accepts at most {} argument{}{}", | ||
| filter_args.len() - 1, | ||
| if filter_args.len() != 2 { "s" } else { "" }, |
There was a problem hiding this comment.
The comparison to 2 made me bug for a while. XD
There was a problem hiding this comment.
Just imagine that rust's arrays are 1-based. :D
| Err(ctx.generate_error(r#"use filter format like `"a={} b={}"|format(a, b)`"#, node)) | ||
| } | ||
|
|
||
| fn visit_fmt_filter( |
There was a problem hiding this comment.
Since this is the same code as format, shouldn't it be merged into one function and we only pass the filter name for the error messages?
There was a problem hiding this comment.
It's different from format. It's {{ var | fmt("{:?}") }} vs {{ "{var1} {var2}" | format(var1 = 1, var2 = 2) }}.
| The order of the named arguments does not matter, but named arguments must come after | ||
| positional (i.e. unnamed) arguments. | ||
|
|
||
| E.g. the filter `pluralize` takes two optional arguments: `sg` and `pl` (singular and plural), |
There was a problem hiding this comment.
Btw, why not using the full name instead of sg/pl?
There was a problem hiding this comment.
The words are just too long, and sg and pl are universally understood abbreviations, I think.
There was a problem hiding this comment.
It's the first time I ever saw them, so I'm definitely not convinced. ^^'
There was a problem hiding this comment.
There was a problem hiding this comment.
If there is a debate in the first place, I think it makes more sense to just go with with the full word over abbreviations.
There was a problem hiding this comment.
You can add a commit to the PR to change the spelling.
Don't simply pass any arguments to the filter. The error message won't be useful otherwise. Also ensure that the argument is a `usize`.
74130a1 to
5ccc0d8
Compare
Todo: