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

Add #[derive(ormlite::Enum)] Procedural Macro #59

Merged
merged 7 commits into from
Jan 3, 2025

Conversation

eboody
Copy link
Contributor

@eboody eboody commented Dec 5, 2024

This pull request introduces a new procedural macro, #[derive(ormlite::Enum)], to simplify the process of creating enums compatible with ormlite and SQLx. This macro automates the implementation of various traits for enums, such as:

  • std::fmt::Display
  • std::str::FromStr
  • std::convert::TryFrom
  • sqlx::Encode
  • sqlx::Decode
  • sqlx::Type

By deriving ormlite::Enum, developers can focus on defining their enums while ensuring they are ready to use with ormlite and SQLx without boilerplate code.


Example Usage

Before this feature, developers needed to manually implement traits for each enum:

use std::str::FromStr;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum MyEnum {
    VariantOne,
    VariantTwo,
}

impl std::fmt::Display for MyEnum { /* Implementation omitted */ }
impl std::str::FromStr for MyEnum { /* Implementation omitted */ }
// Other trait implementations...

With #[derive(ormlite::Enum)], the code becomes:

#[derive(ormlite::Enum)]
pub enum MyEnum {
    VariantOne,
    VariantTwo,
}

Features

  • Automatically generates implementations for:
    • Display for string representations
    • FromStr for parsing strings into enum variants
    • SQLx traits (Encode, Decode, Type) for seamless database integration
  • Supports snake_case conversion of enum variant names to strings.

Technical Details

Macro Implementation:

  • Built using the proc-macro crate with dependencies on syn, quote, and proc-macro2.
  • Ensures enums are safe and ergonomic for database operations.
  • Internally, variants are converted to and from their snake_case string representations.

Impact

  • Developer Experience: Reduces boilerplate code for ormlite/database-ready enums.
  • Performance: Implements traits efficiently, matching manual implementations.

How to Use

  1. Annotate enums with #[derive(ormlite::Enum)]:
    #[derive(ormlite::Enum)]
    pub enum Status {
       Active,
       Inactive,
    }
  2. Use them in your ormlite::Models:
    #[derive(ormlite::Model)]
    struct Article {
       status: Status
    }

Next Steps

  1. Create tests
  2. Review the implementation and provide feedback.
  3. Publish updated documentation for ormlite to include the new macro.
  4. Tag a new release after merging.

Acknowledgments

Special thanks to everyone involved in designing and refining this crate! It's beautifully simple. 🎉

Closes #37

@eboody eboody closed this Dec 6, 2024
@eboody eboody reopened this Dec 6, 2024
@eboody
Copy link
Contributor Author

eboody commented Dec 6, 2024

ok! how is this looking @kurtbuilds ?

@eboody
Copy link
Contributor Author

eboody commented Dec 6, 2024

bump!

@kurtbuilds
Copy link
Owner

let me get the master tests fixed this wekeend, then i'll tackle this. busy time personally as i'm moving apartments, so feel free to bump again if i don't get to it.

@eboody
Copy link
Contributor Author

eboody commented Dec 6, 2024

sound good! no rush dude and congrats on the move

@eboody
Copy link
Contributor Author

eboody commented Dec 15, 2024

bump!

@eboody
Copy link
Contributor Author

eboody commented Jan 3, 2025

should i keep bumping? @kurtbuilds

@eboody
Copy link
Contributor Author

eboody commented Jan 3, 2025

@kurtbuilds the builds are going to continue to fail until that test is addressed. i think it started happening when that builder-struct-add-derive pr was merged but i might be wrong!

should i take a stab at addressing it?

@kurtbuilds
Copy link
Owner

Thank you for the pings!

I'm fixing the test now, it's from an experimental feature I added around a month ago. I'm actually reverting that feature b/c it didn't behave as desired, and then I'll merge this in.

@eboody
Copy link
Contributor Author

eboody commented Jan 3, 2025

amazing! thanks @kurtbuilds

once thats addressed my other PRs should pass too

@kurtbuilds kurtbuilds merged commit 255c818 into kurtbuilds:master Jan 3, 2025
1 check passed
@eboody eboody deleted the derive_enum branch January 3, 2025 13:32
@kurtbuilds
Copy link
Owner

publishing as 0.22.8.

Thank you again!

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.

support string enums
2 participants