ActiveModel
This commit is contained in:
parent
8c354c6c92
commit
9c42ab2552
34
src/entity/active_model.rs
Normal file
34
src/entity/active_model.rs
Normal 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);
|
||||
}
|
@ -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::*;
|
||||
|
@ -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,
|
||||
};
|
||||
|
@ -11,6 +11,13 @@ pub struct Model {
|
||||
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)]
|
||||
pub enum Column {
|
||||
Id,
|
||||
@ -74,3 +81,46 @@ impl Model {
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user