From b1e10aec861e3a6588522b9d13d4fe28022bc8fc Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Wed, 20 Oct 2021 12:23:55 +0800 Subject: [PATCH] Refactoring --- tests/common/features/active_enum.rs | 73 ++-------------------------- 1 file changed, 5 insertions(+), 68 deletions(-) diff --git a/tests/common/features/active_enum.rs b/tests/common/features/active_enum.rs index 1b68d546..e1aef0fe 100644 --- a/tests/common/features/active_enum.rs +++ b/tests/common/features/active_enum.rs @@ -1,5 +1,4 @@ -use sea_orm::{entity::prelude::*, TryGetError, TryGetable}; -use sea_query::{Nullable, ValueType, ValueTypeErr}; +use sea_orm::entity::prelude::*; #[derive(Clone, Debug, PartialEq, DeriveEntityModel)] #[sea_orm(table_name = "active_enum")] @@ -15,73 +14,11 @@ pub enum Relation {} impl ActiveModelBehavior for ActiveModel {} -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, DeriveActiveEnum)] +#[sea_orm(rs_type = "String", db_type = "String(Some(1))")] pub enum Category { + #[sea_orm(string_value = "B")] Big, + #[sea_orm(string_value = "S")] Small, } - -impl ActiveEnum for Category { - type Value = String; - - fn to_value(&self) -> Self::Value { - match self { - Self::Big => "B", - Self::Small => "S", - } - .to_owned() - } - - fn try_from_value(v: &Self::Value) -> Result { - match v.as_ref() { - "B" => Ok(Self::Big), - "S" => Ok(Self::Small), - _ => Err(DbErr::Query(format!( - "unexpected value for {} enum: {}", - stringify!(Category), - v - ))), - } - } - - fn db_type() -> ColumnDef { - ColumnType::String(Some(1)).def() - } -} - -impl Into for Category { - fn into(self) -> Value { - self.to_value().into() - } -} - -impl TryGetable for Category { - fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result { - let value = <::Value as TryGetable>::try_get(res, pre, col)?; - Self::try_from_value(&value).map_err(|e| TryGetError::DbErr(e)) - } -} - -impl ValueType for Category { - fn try_from(v: Value) -> Result { - let value = <::Value as ValueType>::try_from(v)?; - Self::try_from_value(&value).map_err(|_| ValueTypeErr) - } - - fn type_name() -> String { - <::Value as ValueType>::type_name() - } - - fn column_type() -> sea_query::ColumnType { - ::db_type() - .get_column_type() - .to_owned() - .into() - } -} - -impl Nullable for Category { - fn null() -> Value { - <::Value as Nullable>::null() - } -}