From 9c42ab2552422db665ea3548c0867cb3e2d0deb0 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Tue, 25 May 2021 00:38:21 +0800 Subject: [PATCH] ActiveModel --- src/entity/active_model.rs | 34 ++++++++++++++++++++++++++ src/entity/mod.rs | 2 ++ src/entity/prelude.rs | 2 +- src/tests_cfg/cake.rs | 50 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/entity/active_model.rs diff --git a/src/entity/active_model.rs b/src/entity/active_model.rs new file mode 100644 index 00000000..5f436568 --- /dev/null +++ b/src/entity/active_model.rs @@ -0,0 +1,34 @@ +use std::fmt::Debug; +use crate::{ColumnTrait, ModelTrait, Value}; + +#[derive(Clone, Debug)] +pub enum Action { + Set(V), + Unset, +} + +impl Action where V: Into { + pub fn into_action_value(self) -> Action { + match self { + Self::Set(v) => Action::Set(v.into()), + Self::Unset => Action::Unset, + } + } +} + +pub trait ActiveModelOf +where + M: ModelTrait, +{ + fn from_model(m: M) -> Self; +} + +pub trait ActiveModelTrait: Clone + Debug { + type Column: ColumnTrait; + + fn get(&self, c: Self::Column) -> Action; + + fn set(&mut self, c: Self::Column, v: Value); + + fn unset(&mut self, c: Self::Column); +} \ No newline at end of file diff --git a/src/entity/mod.rs b/src/entity/mod.rs index 06b63301..0fa11b87 100644 --- a/src/entity/mod.rs +++ b/src/entity/mod.rs @@ -1,3 +1,4 @@ +mod active_model; mod base; mod column; mod identity; @@ -6,6 +7,7 @@ pub mod prelude; mod primary_key; mod relation; +pub use active_model::*; pub use base::*; pub use column::*; pub use identity::*; diff --git a/src/entity/prelude.rs b/src/entity/prelude.rs index d9521003..01e11c68 100644 --- a/src/entity/prelude.rs +++ b/src/entity/prelude.rs @@ -1,5 +1,5 @@ pub use crate::{ - ColumnTrait, ColumnType, DeriveColumn, DeriveEntity, DeriveModel, DerivePrimaryKey, EntityName, + Action, ActiveModelOf, ActiveModelTrait, ColumnTrait, ColumnType, DeriveColumn, DeriveEntity, DeriveModel, DerivePrimaryKey, EntityName, EntityTrait, EnumIter, Iden, IdenStatic, ModelTrait, PrimaryKeyOfModel, PrimaryKeyTrait, QueryResult, Related, RelationDef, RelationTrait, Select, TypeErr, Value, }; diff --git a/src/tests_cfg/cake.rs b/src/tests_cfg/cake.rs index d61c13ee..46a14765 100644 --- a/src/tests_cfg/cake.rs +++ b/src/tests_cfg/cake.rs @@ -11,6 +11,13 @@ pub struct Model { pub name: String, } +// can we generate this? +#[derive(Clone, Debug)] +pub struct ActiveModel { + pub id: Action, + pub name: Action, +} + #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] pub enum Column { Id, @@ -74,3 +81,46 @@ impl Model { Entity::find_related().belongs_to::(self) } } + +// can we generate this? +impl ActiveModelOf for ActiveModel { + fn from_model(m: Model) -> Self { + Self::from(m) + } +} + +// can we generate this? +impl From for ActiveModel { + fn from(m: Model) -> Self { + Self { + id: Action::Set(m.id), + name: Action::Set(m.name), + } + } +} + +// can we generate this? +impl ActiveModelTrait for ActiveModel { + type Column = Column; + + fn get(&self, c: Self::Column) -> Action { + match c { + Column::Id => self.id.clone().into_action_value(), + Column::Name => self.name.clone().into_action_value(), + } + } + + fn set(&mut self, c: Self::Column, v: Value) { + match c { + Column::Id => self.id = Action::Set(v.unwrap()), + Column::Name => self.name = Action::Set(v.unwrap()), + } + } + + fn unset(&mut self, c: Self::Column) { + match c { + Column::Id => self.id = Action::Unset, + Column::Name => self.name = Action::Unset, + } + } +} \ No newline at end of file