seanum Macro Documentation
Overview
The seanum procedural macro transforms a simple Rust enum into a fully-featured SeaORM active enum with database mapping capabilities. It automatically adds the necessary attributes, derives, and implementations to make the enum compatible with SeaORM's entity system.
Usage
Apply the #[seanum] attribute to an enum definition with the required parameters:
use seanum;
Parameters
The seanum macro accepts the following parameters:
rs_type(Ident): The Rust type that represents the enum in the database (e.g.,String,i32).db_type(LitStr): The database type for the enum (e.g.,"Enum","Text").
Transformation
When applied, the macro transforms the input enum by:
- Adding necessary imports for SeaORM, serde, and fake
- Adding a derive macro with various traits:
Clone- Enables cloning of the enumDummy- Enables generation of fake data for testingDebug- Enables debug formattingPartialEq- Enables partial equality comparisonEnumIter- Enables iteration over enum variantsDeriveActiveEnum- SeaORM's derive for active enumsEq- Enables full equality comparisonSerialize- Enables serializationDeserialize- Enables deserialization
- Adding a
#[sea_orm(...)]attribute to the enum with:rs_type- The Rust type for the enum in the databasedb_type- The database type for the enumenum_name- The snake_case version of the enum name for database use
- Adding
#[sea_orm(string_value = "...")]attributes to each variant with the string representation of the variant name
Example
Input:
use seanum;
Output:
use Dummy;
use *;
use *;
use ;
Notes
- The macro automatically converts the enum name to snake_case for the database name (e.g.,
SwitchActionbecomesswitch_action) - Each variant is mapped to its string representation in the database
- The generated enum is fully compatible with SeaORM's entity system and can be used in database operations
- The macro requires the
megamacframework for proper execution
Dependencies
This macro requires the following dependencies:
sea-orm- For theDeriveActiveEnumtrait and related attributessea-orm-migration- For database migration supportserde- For serialization and deserializationfake- For generating fake data in tests