-
Notifications
You must be signed in to change notification settings - Fork 1
Added the ability to use #[default] and added Default in the derive trait #3
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
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.
Thanks for adding improvements :)
I just have a couple small requests though.
Once you address them I'll merge it and update the package.
for (attributes, variant_name, variant_value) in triples.into_iter() { | ||
if attributes.to_string().contains("default") { | ||
if default_position.is_some() { | ||
// error!("Multiple variants marked as 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.
Did you mean to get rid of this if statement? I'm not 100% sure, but I would assume that derive(Default)
itself will trigger an error if it sees multiple defaults.
Also it looks like we don't actually need to do anything with the default variant, so you probably don't need the default_position
variable.
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.
So, um, it kinda triggers a error
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.
Got it. Looks like someone opened a pr to fix the error message here in response to the issue you opened.
Still, I suppose I'd prefer it if we propagated the error, because even after that pr lands, I'm assuming it might take a little while for that fix to be in a release.
Also, I just realized, as you probably noticed, we can't just use the error!
macro here because your function doesn't return a TokenStream. I think the "correct" thing to do here would be to return Result<(), String>
from check_for_default
and check for an error when you call it, then call error!
from there.
if default_position.is_some() { | ||
// error!("Multiple variants marked as default"); | ||
} | ||
default_position = Some(variant_value.to_string().parse::<usize>().unwrap()); |
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.
You could do a return here instead. And if you do, you won't need to do if !default_position.is_some()
below.
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.
Let me know if you're in a hurry. If so, at this point I don't mind just merging what we have and addressing some of the nits myself.
@TheEppicJR Thanks! Merged! I'll do some nitpick changes and publish a new version soon |
@TheEppicJR Just a heads up: I'm going to make some modifications so that we only derive I just realized that this could break anyone who already has code like Hope you don't mind. I mean, if you really want the first element to be the default, I think it's also more rusty and not too verbose to have an explicit I'm open to changing my mind, so let me know if you disagree. |
Version 1.2.0 now published with this pr link |
This adds the ability to use
#[default]
when declaring the enum and addsDefault
to the derives for the enumThis allows you to derive the trait
Default
for structs that contain a enum generated using this macro