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

issues-721 Add impl IdenStatic and AsRef<str> to #[enum_def] attr #769

Conversation

AndreiOrmanji
Copy link
Contributor

@AndreiOrmanji AndreiOrmanji commented Apr 15, 2024

PR Info

New Features

@tyt2y3
Copy link
Member

tyt2y3 commented Apr 17, 2024

Hi, as I said in another comment, for long term maintenance I would like to deprecate sea-query-attr and move the macro into sea-query-derive. However, we'll have to migrate to syn2 and probably rename a few things. Would you like to do that?

- added extended version of  `#[enum_def]`  in `sea-query-derive`
- upgraded `darling` crate from 0.14 to 0.20
@AndreiOrmanji AndreiOrmanji force-pushed the feature/issues-721_enum_def_impl_iden_static branch from 7f996cc to d75bf0a Compare April 18, 2024 09:29
@AndreiOrmanji
Copy link
Contributor Author

Hi, as I said in another comment, for long term maintenance I would like to deprecate sea-query-attr and move the macro into sea-query-derive. However, we'll have to migrate to syn2 and probably rename a few things. Would you like to do that?

I've done it in d75bf0a.
I'm not sure if I've marked as deprecated correctly, and that's why I did not update CHANGELOG.md and README.md to reflect my changes there.

fields: Fields::Named(fields),
..
}) => &fields.named,
_ => panic!("#[enum_def] can only be used on structs"),
Copy link
Contributor

Choose a reason for hiding this comment

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

This should emit a compile_error! instead of panicking in the proc-macro

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean something like this:

            _ => return quote_spanned! {
                input.span() => compile_error!("you can only derive Iden on enums or unit structs");
            }
            .into(),

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep! The user feedback is better when emitting compiler errors instead of panicking

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed in my last commit

Copy link
Contributor

@sandhose sandhose left a comment

Choose a reason for hiding this comment

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

I was looking at upgrading syn/darling in sea-query-attr, and did not see your PR first.

So, I still opened a draft PR for visibility: #799

There are some improvements there with proper compilation errors, and snapshot tests for those errors that you may want to integrate in this PR. I'd be happy to do it if you don't have the time to do so

let ident = &field.ident;
let string = ident
.as_ref()
.expect("#[enum_def] can only be used on structs with named fields")
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, this should not panic

.to_string();
let as_pascal = string.to_pascal_case();
NamingHolder {
default: ident.as_ref().unwrap().clone(),
Copy link
Contributor

Choose a reason for hiding this comment

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

That's an unnecessary unwrap, the unwrapped option should be stored before

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My final variant is the following:

    let fields =
        match &input.data {
            Data::Struct(DataStruct {
                fields: Fields::Named(fields),
                ..
            }) => &fields.named,
            _ => return quote_spanned! {
                input.span() => compile_error!("you can only derive Iden on enums or unit structs");
            }
            .into(),
        };

    let field_names: Vec<NamingHolder> = fields
        .iter()
        .map(|field| {
            let ident = field.ident.as_ref().unwrap();
            NamingHolder {
                default: ident.clone(),
                pascal: Ident::new(ident.to_string().to_pascal_case().as_str(), ident.span()),
            }
        })
        .collect();

I'm not sure, but Fields::Named means there are only named fields, so I've kept unwrap with slight changes for ident.

@AndreiOrmanji
Copy link
Contributor Author

AndreiOrmanji commented Aug 9, 2024

I was looking at upgrading syn/darling in sea-query-attr, and did not see your PR first.

So, I still opened a draft PR for visibility: #799

@sandhose,
Can I merge your branch with mine as part of this MR?

@sandhose
Copy link
Contributor

sandhose commented Aug 9, 2024

I was looking at upgrading syn/darling in sea-query-attr, and did not see your PR first.

So, I still opened a draft PR for visibility: #799

@sandhose,

Can I merge your branch with mine as part of this MR?

Of course, take whatever you need from that PR

@tyt2y3
Copy link
Member

tyt2y3 commented Sep 28, 2024

Umm... how can I get the CI to run?

@@ -0,0 +1,10 @@
use sea_query_attr::enum_def;
Copy link
Member

Choose a reason for hiding this comment

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

I think we're meant to import sea_query_derive and test it's implementation?

@tyt2y3 tyt2y3 changed the base branch from master to sea-query-derive September 28, 2024 08:51
@tyt2y3 tyt2y3 merged commit eb9f9bf into SeaQL:sea-query-derive Sep 28, 2024
@tyt2y3 tyt2y3 mentioned this pull request Sep 28, 2024
tyt2y3 added a commit that referenced this pull request Sep 28, 2024
@AndreiOrmanji AndreiOrmanji deleted the feature/issues-721_enum_def_impl_iden_static branch October 4, 2024 11:15
Copy link

github-actions bot commented Oct 5, 2024

🎉 Released In 0.32.0-rc.2 🎉

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.

enum_def could derive IdenStatic
3 participants