-
Notifications
You must be signed in to change notification settings - Fork 104
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
add visit and visit_mut APIs #261
Conversation
I agree. We only gate something only if there is some overhead, like it adds a dependency. |
09d0654
to
b57a894
Compare
Added a visit API, and removed the feature flagging. Looks pretty good now, let me know what you think! An easy exercise would be to write a recursive formatter. I can add that in another commit on top of the stack as well. |
Ahh this needs a --all-targets passed into cargo test in CI |
I've changed it so that the table and inline_table visit methods both call table_like, and there's now a single table_like_kv method which should make things simpler. I also made the example slightly more complex -- it can now normalize inline tables into tables within a scope as well. Along the way I found a formatting bug for tables, and I ended up fixing that as well. Seems like we may want to provide a bunch of standard inbox visitors in a follow-up (bundled with
Visitors can also be used in a more delegated style, where visitor1 can have internal logic that switches to visitor2 under some sections of the tree. |
src/table.rs
Outdated
Item::Table(table) => { | ||
kv.key.decor = Decor::new(DEFAULT_KEY_PATH_DECOR.0, DEFAULT_KEY_PATH_DECOR.1); | ||
table.decor = Decor::new(DEFAULT_TABLE_DECOR.0, DEFAULT_TABLE_DECOR.1); | ||
} | ||
Item::ArrayOfTables(array) => { | ||
kv.key.decor = Decor::new(DEFAULT_KEY_PATH_DECOR.0, DEFAULT_KEY_PATH_DECOR.1); | ||
for table in array.iter_mut() { | ||
table.decor = Decor::new(DEFAULT_TABLE_DECOR.0, DEFAULT_TABLE_DECOR.1); | ||
} | ||
} |
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.
Along the way I found a formatting bug for tables, and I ended up fixing that as well.
Table::fmt()
wasn't properly applying the right decor to tables and
arrays of tables.
What is being considered a bug in the old version? Not reformatting the table headers? I think this is a question of whether you are considering it from the test document perspective or the in-memory data structure perspective. I was personally viewing this from the document perspective, that we are only formatting the visual table body. I feel like to consider this from the data structure perspective, we would need to recurse.
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.
Yeah, at first it was about reformatting headers (I ran into an issue where converting an inline table into a standard table would cause the header to have a trailing space). Then I realized that we weren't formatting leading and trailing whitespace on sub tables or arrays of tables either.
The invariant I was going for was that calling fmt() on every element of the document would format the whole document. Currently that invariant is mostly upheld except for the cases fixed in this PR.
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.
btw I realized there is a bug with Table::fmt
for the description I gave above, #262
Yeah, at first it was about reformatting headers (I ran into an issue where converting an inline table into a standard table would cause the header to have a trailing space). Then I realized that we weren't formatting leading and trailing whitespace on sub tables or arrays of tables either.
When converting table types, we should probably reset the formatting to defaults since 99% of the time, the formatting will be counter to the users goal.
And/or we provide a "reset to default" formatting / clear formatting function.
The invariant I was going for was that calling fmt() on every element of the document would format the whole document. Currently that invariant is mostly upheld except for the cases fixed in this PR.
Thanks for sharing the intent! That is a worthwhile goal. I wonder if we should have a separate "format table header".
Either way, maybe we should split this out into a separate PR so we can discuss and iterate on it without being tied up with the rest of the work here?
I think that makes sense. What do you think about recursive formatting? In particular I'm thinking of the situation where you're converting a standard table into an inline one, and there's multiline arrays within it (this is addressed in the visit_mut example). The TOML spec says:
How would this differ from the
So when I started implementing this I was thinking along those lines as well. However, the issue is that the header is not part of the
Ahh unfortunately the issue is that the included example actually depends on the formatting to work properly, so there's a dependency between the two. GH isn't great at expressing dependencies between pull requests (this is something I've personally talked to GH reps about -- also there are others trying to solve this problem). That's why I had to club it together under the same PR. However, I have tried to produce independent, separately reviewable commits. |
Also, as far as recursive formatting goes, I think it would be easiest to build it on top of |
Oh, re
|
When in the context of the stated goal, That said, I am unsure about making it recursive, about tying key-value pair formatting with header formatting. This feels very restrictive. For example, in With that said, I see
The dependency is because of the table conversion, correct? Let's focus on fixing that for unblocking this PR. If you want, I can start working on a PR for it. |
Both the array formatting issue (commit 1 which should be uncontroversial I think) and the table conversion block this PR, yeah. |
(If you want to work on it for a bit, sounds great, yeah!) |
This makes sense. I can see everything being part of a "normalize" API. |
In testing #265 against your branch, I realized that it won't help your case. You are already running |
I feel like the crux of the problem is that the kv visitor should be given a mutable key so you can clear the existing decor. We don't want to expose a I think we should add a |
A formatted array is all in one line, so it shouldn't have trailing commas or whitespace.
Thank you for adding |
This is modeled after the `syn::visit_mut` API. Also include a nontrivial example for custom formatting.
Similar to the visit_mut API, this allows walking over the document tree. I think overall this is probably less useful than `visit_mut`, but is still worth implementing for completeness.
The previous commits added some tests to the `visit` example -- ensure that they're run in CI.
Looks like all of the extra formatting work you did also obviated the need for a ton of |
@@ -45,7 +45,7 @@ jobs: | |||
- uses: actions-rs/cargo@v1 | |||
with: | |||
command: test | |||
args: --all-features | |||
args: --all-features --all-targets |
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.
Unless I'm missing something, it looks like --all-targets
causes doc tests to not run. Ran into this with another project. when comparing cargo test
(fails) to cargo test --all-targets
(passes).
:(
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.
yeah: rust-lang/cargo#6669
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 like we are covered with our default feature run, so probably good enough here.
This is modeled after the
syn::visit
andsyn::visit_mut
APIs. See discussion in #192 (comment) for what prompted me to write this.Also include some nontrivial examples for custom formatting.
One thing that immediately occurs to me is that we could easily provide a recursive formatter based on this (which would strengthen the argument for it being available unconditionally).