Skip to content
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

Enhance match to include multiple targets #911

Merged
merged 5 commits into from
Nov 22, 2023
Merged

Enhance match to include multiple targets #911

merged 5 commits into from
Nov 22, 2023

Conversation

PizzasBear
Copy link
Contributor

@PizzasBear PizzasBear commented Nov 19, 2023

This adds ability to or patterns in any context (e.g. in when and let).
This is done by adding an OrChain variant to the Target enum in askama_parser,
and simply adding a | between or-chained patterns in askama_derive.

Closes #904

@PizzasBear PizzasBear changed the title Enhance match to include multiple targets, closes #904 Enhance match to include multiple targets Nov 19, 2023
askama_parser/src/node.rs Outdated Show resolved Hide resolved
askama_parser/src/node.rs Outdated Show resolved Hide resolved
Signed-off-by: max <gmx.sht@gmail.com>
@PizzasBear
Copy link
Contributor Author

I've removed the comments, forgot to remove them prior to committing...

pub(super) fn parse(i: &'a str) -> ParseResult<'a, Self> {
map(
separated_list1(ws(tag("or")), Self::parse_one),
|opts| match <[_; 1]>::try_from(opts) {
Copy link
Owner

Choose a reason for hiding this comment

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

I don't find this <[_; 1]>::try_from() idiom particularly clear. How about this:

match opts.len() {
    1 => opts.pop().unwrap(),
    _ => Self::OrChain(opts),
}

(Is this actually necessary? What happens if we just always yield a Vec<Target<'a>> from parse()?)

Copy link
Contributor Author

@PizzasBear PizzasBear Nov 22, 2023

Choose a reason for hiding this comment

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

I liked the try_from because it avoided mut and .unwrap(), but I can change this.
The match statement helps avoid one item Target::OrChain which will help debugging by cleaning up the output of println!("{target:#?}") or other statements that'll result in pretty printing a target.

Signed-off-by: max <gmx.sht@gmail.com>
…if let` statements

Signed-off-by: max <gmx.sht@gmail.com>
@djc djc merged commit 48c6cd3 into djc:main Nov 22, 2023
20 checks passed
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.

Feature request: enhance match to include multiple targets
2 participants