sea_orm::entity

Trait ActiveEnum

Source
pub trait ActiveEnum: Sized + Iterable {
    type Value: ActiveEnumValue;
    type ValueVec;

    // Required methods
    fn name() -> DynIden;
    fn to_value(&self) -> Self::Value;
    fn try_from_value(v: &Self::Value) -> Result<Self, DbErr>;
    fn db_type() -> ColumnDef;

    // Provided methods
    fn into_value(self) -> Self::Value { ... }
    fn as_enum(&self) -> SimpleExpr { ... }
    fn values() -> Vec<Self::Value> { ... }
}
Expand description

A Rust representation of enum defined in database.

§Implementations

You can implement ActiveEnum manually by hand or use the derive macro DeriveActiveEnum.

§Examples

Implementing it manually versus using the derive macro DeriveActiveEnum.

See DeriveActiveEnum for the full specification of macro attributes.

use sea_orm::entity::prelude::*;

// Using the derive macro
#[derive(Debug, PartialEq, EnumIter, DeriveActiveEnum, DeriveDisplay)]
#[sea_orm(
    rs_type = "String",
    db_type = "String(StringLen::N(1))",
    enum_name = "category"
)]
pub enum DeriveCategory {
    #[sea_orm(string_value = "B")]
    Big,
    #[sea_orm(string_value = "S")]
    Small,
}

// Implementing it manually
#[derive(Debug, PartialEq, EnumIter)]
pub enum Category {
    Big,
    Small,
}

#[derive(Debug, DeriveIden)]
pub struct CategoryEnum;

impl ActiveEnum for Category {
    // The macro attribute `rs_type` is being pasted here
    type Value = String;

    type ValueVec = Vec<String>;

    // Will be atomically generated by `DeriveActiveEnum`
    fn name() -> DynIden {
        SeaRc::new(CategoryEnum)
    }

    // 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::Type(format!(
                "unexpected value for Category enum: {}",
                v
            ))),
        }
    }

    fn db_type() -> ColumnDef {
        // The macro attribute `db_type` is being pasted here
        ColumnType::String(StringLen::N(1)).def()
    }
}

Using ActiveEnum on Model.

use sea_orm::entity::prelude::*;

// Define the `Category` active enum
#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum, DeriveDisplay)]
#[sea_orm(rs_type = "String", db_type = "String(StringLen::N(1))")]
pub enum Category {
    #[sea_orm(string_value = "B")]
    Big,
    #[sea_orm(string_value = "S")]
    Small,
}

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "active_enum")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    // Represents a db column using `Category` active enum
    pub category: Category,
    pub category_opt: Option<Category>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

Required Associated Types§

Source

type Value: ActiveEnumValue

Define the Rust type that each enum variant corresponds.

Source

type ValueVec

This has no purpose. It will be removed in the next major version.

Required Methods§

Source

fn name() -> DynIden

Get the name of enum

Source

fn to_value(&self) -> Self::Value

Convert enum variant into the corresponding value.

Source

fn try_from_value(v: &Self::Value) -> Result<Self, DbErr>

Try to convert the corresponding value into enum variant.

Source

fn db_type() -> ColumnDef

Get the database column definition of this active enum.

Provided Methods§

Source

fn into_value(self) -> Self::Value

Convert an owned enum variant into the corresponding value.

Source

fn as_enum(&self) -> SimpleExpr

Construct a enum expression with casting

Source

fn values() -> Vec<Self::Value>

Get the name of all enum variants

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§