ActiveModel

This commit is contained in:
Chris Tsang 2021-05-25 00:38:21 +08:00
parent 8c354c6c92
commit 9c42ab2552
4 changed files with 87 additions and 1 deletions

View File

@ -0,0 +1,34 @@
use std::fmt::Debug;
use crate::{ColumnTrait, ModelTrait, Value};
#[derive(Clone, Debug)]
pub enum Action<V> {
Set(V),
Unset,
}
impl<V> Action<V> where V: Into<Value> {
pub fn into_action_value(self) -> Action<Value> {
match self {
Self::Set(v) => Action::Set(v.into()),
Self::Unset => Action::Unset,
}
}
}
pub trait ActiveModelOf<M>
where
M: ModelTrait,
{
fn from_model(m: M) -> Self;
}
pub trait ActiveModelTrait: Clone + Debug {
type Column: ColumnTrait;
fn get(&self, c: Self::Column) -> Action<Value>;
fn set(&mut self, c: Self::Column, v: Value);
fn unset(&mut self, c: Self::Column);
}

View File

@ -1,3 +1,4 @@
mod active_model;
mod base; mod base;
mod column; mod column;
mod identity; mod identity;
@ -6,6 +7,7 @@ pub mod prelude;
mod primary_key; mod primary_key;
mod relation; mod relation;
pub use active_model::*;
pub use base::*; pub use base::*;
pub use column::*; pub use column::*;
pub use identity::*; pub use identity::*;

View File

@ -1,5 +1,5 @@
pub use crate::{ 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, EntityTrait, EnumIter, Iden, IdenStatic, ModelTrait, PrimaryKeyOfModel, PrimaryKeyTrait,
QueryResult, Related, RelationDef, RelationTrait, Select, TypeErr, Value, QueryResult, Related, RelationDef, RelationTrait, Select, TypeErr, Value,
}; };

View File

@ -11,6 +11,13 @@ pub struct Model {
pub name: String, pub name: String,
} }
// can we generate this?
#[derive(Clone, Debug)]
pub struct ActiveModel {
pub id: Action<i32>,
pub name: Action<String>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column { pub enum Column {
Id, Id,
@ -74,3 +81,46 @@ impl Model {
Entity::find_related().belongs_to::<Entity>(self) Entity::find_related().belongs_to::<Entity>(self)
} }
} }
// can we generate this?
impl ActiveModelOf<Model> for ActiveModel {
fn from_model(m: Model) -> Self {
Self::from(m)
}
}
// can we generate this?
impl From<Model> 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<Value> {
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,
}
}
}