Skip to content

Conversation

@TheEppicJR
Copy link

This adds the ability to use #[default] when declaring the enum and adds Default to the derives for the enum
This allows you to derive the trait Default for structs that contain a enum generated using this macro

Copy link
Owner

@math4tots math4tots left a 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");
Copy link
Owner

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.

Copy link
Author

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

Copy link
Owner

@math4tots math4tots Nov 21, 2023

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());
Copy link
Owner

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.

Copy link
Owner

@math4tots math4tots left a 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.

@math4tots math4tots merged commit 4abec09 into math4tots:master Nov 21, 2023
@math4tots
Copy link
Owner

@TheEppicJR Thanks! Merged! I'll do some nitpick changes and publish a new version soon

@math4tots
Copy link
Owner

@TheEppicJR Just a heads up: I'm going to make some modifications so that we only derive Default if the user specifies a #[default].

I just realized that this could break anyone who already has code like impl Default for SomeEnum { .. }.

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 #[default] anyway.

I'm open to changing my mind, so let me know if you disagree.

@math4tots
Copy link
Owner

Version 1.2.0 now published with this pr link

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.

2 participants