Skip to content
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

DeriveDisplay macro for enum #1726

Merged
merged 17 commits into from
Jul 10, 2023
Merged

DeriveDisplay macro for enum #1726

merged 17 commits into from
Jul 10, 2023

Conversation

darkmmon
Copy link
Contributor

PR Info

New Features

  • added a new macro DeriveDisplay to allow custom display value.

Breaking Changes

  • removed impl Display trait for enum. Add DerivedDisplay if you want.

Changes


let mut variants = Vec::new();
for variant in variant_vec {
let mut display_value = "".into_token_stream();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe?

let mut display_value = TokenStream::new();

https://docs.rs/proc-macro2/latest/proc_macro2/struct.TokenStream.html

Comment on lines 104 to 105
} else if meta.path.is_ident("display_value") {
Some(meta.value()?.parse::<LitStr>()?);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is not needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the code for DeriveActiveEnum will panic if there is some unrecognised attributes, I added this so that deriving both Display and Active Enum would not make the code panic.

@darkmmon
Copy link
Contributor Author

Would want some clarifying input from @tyt2y3 too.
Should we make that the displayed value to follow the order
Display value > num value/string value > enum name (> means the former override the latter one)?

@tyt2y3
Copy link
Member

tyt2y3 commented Jun 29, 2023

Hope I did not get your question wrong, but I think Display should not display the underlying string or numeric value, no matter what. Just to clarify again, displaying the underlying value was a mistake. Just like what the Rust's built-in #[derive(Display)] does.

So the order of precedence can be: display_value -> enum variant.

Comment on lines 393 to 394
assert_eq!(format!("{}", $ident::Big), "1");
assert_eq!(format!("{}", $ident::Small), "0");
assert_eq!(format!("{}", $ident::Big), "'Big'");
assert_eq!(format!("{}", $ident::Small), "'Small'");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently the fallback enums doesn't pass this test as it weirdly doesn't have the single quotes
Will try to find the reason behind it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We expect no single quote:

assert_eq!(format!("{}", Enum::Big), "Big");

@darkmmon darkmmon marked this pull request as ready for review June 30, 2023 03:13
@@ -408,7 +408,7 @@ mod tests {

#[test]
fn escaped_non_uax31() {
#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Copy)]
#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Copy, DeriveDisplay)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a few test cases NOT deriving DeriveDisplay where it is not needed?

Just want to make sure we have test coverage for both cases.

@tyt2y3 tyt2y3 requested a review from billy1624 July 5, 2023 19:15
Comment on lines +59 to +64
pub enum DisplayTea {
#[sea_orm(string_value = "EverydayTea", display_value = "Everyday")]
EverydayTea,
#[sea_orm(string_value = "BreakfastTea", display_value = "Breakfast")]
BreakfastTea,
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait, actually, did I miss the test case for this? There are no corresponding asserts?

As this is the only place the display_value attribute is used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tyt2y3 tyt2y3 self-requested a review July 5, 2023 19:19
@tyt2y3
Copy link
Member

tyt2y3 commented Jul 6, 2023

Awesome, great work

Copy link
Member

@billy1624 billy1624 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!! @darkmmon This is clean :)

@billy1624 billy1624 merged commit 902c0ed into SeaQL:master Jul 10, 2023
@github-actions
Copy link

github-actions bot commented Aug 2, 2023

🎉 Released In 0.12.1 🎉

Thank you everyone for the contribution!
This feature is now available in the latest release. Now is a good time to upgrade!
Your participation is what makes us unique; your adoption is what drives us forward.
You can support SeaQL 🌊 by starring our repos, sharing our libraries and becoming a sponsor ⭐.

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.

DeriveActiveEnum's implicit implementation of Display trait needs to be overridable
3 participants