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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

eboody
Copy link

@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
Author

eboody commented Dec 6, 2024

ok! how is this looking @kurtbuilds ?

@eboody
Copy link
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
Author

eboody commented Dec 6, 2024

sound good! no rush dude and congrats on the move

@eboody
Copy link
Author

eboody commented Dec 15, 2024

bump!

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