This commit is contained in:
Billy Chan 2021-10-20 15:06:23 +08:00
parent ceba3ef7a0
commit eed8b7c51e
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
2 changed files with 79 additions and 0 deletions

View File

@ -102,6 +102,29 @@ pub fn derive_active_model_behavior(input: TokenStream) -> TokenStream {
}
}
/// A derive macro to implement `sea_orm::ActiveEnum` trait for enums.
///
/// # Limitations
///
/// This derive macros can only be used on enums.
///
/// # Macro Attributes
///
/// All macro attributes listed below have to be annotated in the form of `#[sea_orm(attr = value)]`.
///
/// - For enum
/// - `rs_type`: Define `ActiveEnum::Value`
/// - Possible values: `String`, `i8`, `i16`, `i32`, `i64`, `u8`, `u16`, `u32`, `u64`
/// - Note that value has to be passed as string, i.e. `rs_type = "i8"`
/// - `db_type`: Define `ColumnType` returned by `ActiveEnum::db_type()`
/// - Possible values: all available enum variants of `ColumnType`, e.g. `String(None)`, `String(Some(1))`, `Integer`
/// - Note that value has to be passed as string, i.e. `db_type = "Integer"`
///
/// - For enum variant
/// - `string_value` or `num_value`:
/// - For `string_value`, value should be passed as string, i.e. `string_value = "A"`
/// - For `num_value`, value should be passed as integer, i.e. `num_value = 1` or `num_value = 1i32`
/// - Note that only one of it can be specified, and all variants of an enum have to annotate with the same `*_value` macro attribute
#[proc_macro_derive(DeriveActiveEnum, attributes(sea_orm))]
pub fn derive_active_enum(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);

View File

@ -9,6 +9,62 @@ use sea_query::{Nullable, Value, ValueType};
///
/// # Examples
///
/// Implementing it manually versus using the derive macro [DeriveActiveEnum](sea_orm_macros::DeriveActiveEnum).
///
/// > See [DeriveActiveEnum](sea_orm_macros::DeriveActiveEnum) for the full specification of macro attributes.
///
/// ```rust
/// // Using the derive macro
/// #[derive(Debug, PartialEq, DeriveActiveEnum)]
/// #[sea_orm(rs_type = "String", db_type = "String(Some(1))")]
/// pub enum DeriveCategory {
/// #[sea_orm(string_value = "B")]
/// Big,
/// #[sea_orm(string_value = "S")]
/// Small,
/// }
///
/// // Implementing it manually
/// #[derive(Debug, PartialEq)]
/// pub enum Category {
/// Big,
/// Small,
/// }
///
/// impl ActiveEnum for Category {
/// // The macro attribute `rs_type` is being pasted here
/// type Value = String;
///
/// // Will be atomically generated by `DeriveActiveEnum`
/// fn to_value(&self) -> Self::Value {
/// match self {
/// Self::Big => "B",
/// Self::Small => "S",
/// }
/// .to_owned()
/// }
///
/// // Will be atomically generated by `DeriveActiveEnum`
/// fn try_from_value(v: &Self::Value) -> Result<Self, DbErr> {
/// match v.as_ref() {
/// "B" => Ok(Self::Big),
/// "S" => Ok(Self::Small),
/// _ => Err(DbErr::Query(format!(
/// "unexpected value for Category enum: {}",
/// v
/// ))),
/// }
/// }
///
/// fn db_type() -> ColumnDef {
/// // The macro attribute `db_type` is being pasted here
/// ColumnType::String(Some(1)).def()
/// }
/// }
/// ```
///
/// Using [ActiveEnum] on Model.
///
/// ```
/// use sea_orm::entity::prelude::*;
///