-
Notifications
You must be signed in to change notification settings - Fork 602
move primitive dialect configuration into a struct #1431
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
Conversation
Pull Request Test Coverage Report for Build 10862628784Details
💛 - Coveralls |
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.
overall this PR is significantly negative LOC — showing that this approach is at least less verbose.
src/dialect/mod.rs
Outdated
fn dialect(&self) -> std::any::TypeId { | ||
fn settings(&self) -> DialectSettings { | ||
let s = self.0.settings(); | ||
DialectSettings { |
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.
this is slightly ugly because WrappedDialect
only implemented a few of the methods on the dialect.
But this actually demonstrates one of the advtanges of the approach in this PR:
If this test had been written with the new behaviour, this method would become
fn settings(&self) -> DialectSettings {
let mut s = self.0.settings();
s.custom_thing = true;
s
}
And it would stay up-to-date even as new setting were added. Unlike the current behaviour.
I can update this test if you like?
src/dialect/mod.rs
Outdated
fn is_proper_identifier_inside_quotes( | ||
&self, | ||
chars: std::iter::Peekable<std::str::Chars<'_>>, | ||
chars: Peekable<Chars<'_>>, |
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.
it would be great if clippy or rustfmt could warn about these unnecessary paths, rustrover warns, hence the drive by change.
src/dialect/mod.rs
Outdated
fn flags(&self) -> DialectFlags { | ||
DialectFlags::default() | ||
} |
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 like creating a new struct on every call (and there are a lot).
I'd rather store the struct on each dialect, then the signature here can before
fn flags(&self) -> &DialectFlags {
&self.settings
}
The problem then is that every struct needs a fn new()
or impl Default
, and the signature to create all dialects changes everywhere, but if we're happy with that breaking change, the whole thing would become much cleaner. WDYT?
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.
done.
Okay having said I would wait for feedback, I couldn't resist moving flags onto the structs. While I'm aware there's a good chance this might get closed, I think this is the right way to go if we want something of this sort. |
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.
@samuelcolvin Thanks for tackling this! The changes look very reasonable to me
cc @alamb also @jmhain I think you had a similar idea for this, in case you have any thoughts
@@ -63,6 +63,152 @@ macro_rules! dialect_of { | |||
}; | |||
} | |||
|
|||
/// Constant settings for a dialect. | |||
/// | |||
/// For configuration to be defined here, rather than on the rate: |
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.
on the rate
was this a typo?
/// * It must be a const setting, not value that's dependent on context - that has to be a function | ||
/// * have a default that matches `Default::default()` | ||
#[derive(Debug, Default, Clone)] | ||
pub struct DialectFlags { |
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.
Would it make sense to call it e.g. DialectSettings
, DialectConstants
or similar if we envision this potentially containing more than boolean flags?
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.
Happy to change a name.
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.
Thank you @samuelcolvin and @iffyio
In my opinion:
- Adding
impl Default
for the dialects is nice as it is sort of a usability papercut (it would also nice to have anew()
function, to follow convention) - In terms of
DialectFlags
I personally think the improvement is not worth the API churn, though I don't really know how many people have their own Dialect trait implementations 🤔
Okay, I'll leave this open for a few days in case anyone else is particularly keen for it, otherwise I'll close this and work on #1430 by adding lots of boolean functions. |
Maybe we should contemplate creating a parser that parameterized on the dialect ( |
This would cause a lot of code bloat for use cases that require parsing many different dialects. What's the benefit of doing it? |
What do you mean by "a lot of code bloat"? Surely you can create one parser for each dialect? The memory overhead would be tiny. Avoiding dynamic dispatch would lead to better performance, smaller binaries and (most importantly) more canonical code in the library. |
I think the idea is if we made a parser you could end up with multiple copies of the Parser code for each one you instantiated -- like I think one workaround would be to make your own However I think I may have lead us offtopic here. The dynamic dispatch has always kind of annoyed me but I have never had any actual justification to change it. Sorry for starting this discusision |
Converting to a draft as I don't think this PR is waiting on review anymore |
Thank you for your contribution. Unfortunately, this pull request is stale because it has been open 60 days with no activity. Please remove the stale label or comment or this will be closed in 7 days. |
Closing as I don't think we're going this way anymore. |
Step towards #1430.
I personally think this should make it much easier to add more flags to dialects. But I can see it might not be to everyone's taste.
So I want confirmation it's a good idea before prcoeeding.
This should: