-
-
Notifications
You must be signed in to change notification settings - Fork 219
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
Implement match functionality #54
Conversation
re: whitespace. Aside from really struggling with the whitespace handling, I'm not super clear on the right behavior. But mostly, I think that any whitespace between the match and first arm should be ignored, otherwise you will end up with the first arm having an extra newline in the common cases. The examples below should probably be test cases. I've attempted to define the assertions for each of these when rendered with both A) everything on its own line
assert: B) one-line for each arm
assert: C) compact one-liner
assert: D) strange one-liner
assert: E) Endmatch trailing without a newline
assert: F) all arms share one line
assert: G) first arm on the match line
assert: |
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.
Looks great! Feedback on your open items:
-
Whitespace should be fully explicit. So, preserve all the whitespace you see in the template, then allow the placement of
-
in all the usual places to suppress it. This makes it predictable. So I don't think we should implicitly suppress whitespace betweenmatch
and the firstwhen
. -
Let's postpone compound arms to a future PR?
-
Let's also postpone paren-less single-expression variants? Or at least put it in a separate commit.
-
I think we should probably allow
when _
as an alternative toelse
. -
Waiting for match modes will mean that we either need to wait for them to hit stable or we need to add some stuff to deal with the appropriate feature flag.
-
Should add a test to check that literal-as-variant works.
-
Let's use
endmatch
. -
Feel free to leave docs to me.
I'd also prefer to have more tests in separate commits, so keep this easy to review.
askama_shared/src/generator.rs
Outdated
@@ -414,6 +417,38 @@ impl<'a> Generator<'a> { | |||
self.writeln("}"); | |||
} | |||
|
|||
fn write_match(&mut self, state: &'a State, ws1: &WS, expr: &Expr, arms: &'a [When], ws2: &WS) { | |||
self.handle_ws(ws1); | |||
self.locals.push(); |
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 don't think we need to push the locals
here?
askama_shared/src/generator.rs
Outdated
for param in params { | ||
self.locals.insert(param); | ||
self.write(param); | ||
self.write(", "); |
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.
For readability of the generated code, I'd like to only emit the comma where necessary. I think the easiest way to do that is to enumerate
over the loop iterator and emit a prefix comma for index > 0.
testing/templates/match.html
Outdated
Found {{val}} | ||
{% when None %} | ||
Not Found | ||
{% endmatch %} |
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.
Please add a trailing newline here.
I neglected to chime in, but I'm 100% in agreement with all your feedback/suggesions. I just need to find a bit more time to push this across the finish line. ;-) And I still hope to put more thought into what options there are with or without waiting for match modes to land - but I figure I'll worry about that after all the other details mentioned. |
Sounds good! I'll probably focus on other things in the mean time, on the assumption that binding modes are a prerequisite. Unless you need more help/feedback, of course! |
A little bit of progress:
I could (should?) have used 2 different enum types for those, but since both identifier and name are just There is a big caveat I discovered: |
Whitespace: I have to admit it's a bit tricky. Each piece of whitespace (which will be previous whitespace to a particular How does Rust handle the |
(Of course if you're done with trying to get the whitespace to line up, feel free to just say so and I'll dive in myself.) |
I'm not sure how Rust handles I haven't given up on whitespace (yet), but the trial-and-error battling with it has been almost half of my total time working on this PR, so if you see the obvious problem, go ahead and fix it; I get no joy out of mindlessly battling a problem that you could fix in 30s. But I'm hoping that walking the code again sometime with the context you just added might help make something click. But mostly I'm thinking about how to solve the ref/match-mode stuff. |
I stumbled onto a trick that seems to work pretty well for auto-ref/deref. This playground does the best job of showing how it works, using Here's a break down of what the rendered code ends up looking like:
|
Wow, that's good news! If you rebase your branch onto master, you get d497a31, which should help. |
@anowell ping? |
Much of this can be yanked out and made simpler when match-modes lands in stable
I've had the custom enum stuff mostly implemented for weeks, just wasn't ready to take on the whitespace issues. Anyhow, I just rebased everything I had. I feel like I created some redundant code for parsing the I'm a bit pressed on a mid-Nov deadline, so it'll probably be a couple weeks before I can dig into the whitespace issue or anything else that comes up on this, so feel free to resolve that yourself if you want to see it land sooner. ;-) |
I fixed the whitespace issue by allowing the parser to see the whitespace between I'll write up some documentation before I release a new version. |
This is a first stab at basic match functionality discussed in #46. Several things are incomplete:
Some with (val)
when Foo | Bar
else
isn't supported yet (thoughwhen _
probably works - unsure if that should be prevented)when Some with "foo"
endmatch
. Previous discussions just usedend
. Not sure what's preferred.::
can't be in an identifier. needs a path parser)I'm not sure when I'll get to the remainder - I don't expect I'll be able to jump on this too much more in the coming week or so. I'll try to pick it back up when I can, but I certainly don't oppose anyone stepping in to help land any of this functionality in the meantime.