From 8667b2ddfc5e14905a07e37debdeb02644faaa7b Mon Sep 17 00:00:00 2001 From: Ari Seyhun Date: Mon, 11 Oct 2021 14:33:47 +0700 Subject: [PATCH] Add trait `IntoActiveValue` --- src/entity/active_model.rs | 86 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/entity/active_model.rs b/src/entity/active_model.rs index cfcb0bbd..9ac92387 100644 --- a/src/entity/active_model.rs +++ b/src/entity/active_model.rs @@ -3,6 +3,7 @@ use crate::{ PrimaryKeyTrait, Value, }; use async_trait::async_trait; +use sea_query::Nullable; use std::fmt::Debug; #[derive(Clone, Debug, Default)] @@ -172,6 +173,78 @@ where } } +pub trait IntoActiveValue +where + V: Into, +{ + fn into_active_value(self) -> ActiveValue; +} + +macro_rules! impl_into_active_value { + ($ty: ty, $fn: ident) => { + impl IntoActiveValue<$ty> for $ty { + fn into_active_value(self) -> ActiveValue<$ty> { + $fn(self) + } + } + }; +} + +impl_into_active_value!(bool, Set); +impl_into_active_value!(i8, Set); +impl_into_active_value!(i16, Set); +impl_into_active_value!(i32, Set); +impl_into_active_value!(i64, Set); +impl_into_active_value!(u8, Set); +impl_into_active_value!(u16, Set); +impl_into_active_value!(u32, Set); +impl_into_active_value!(u64, Set); +impl_into_active_value!(f32, Set); +impl_into_active_value!(f64, Set); +impl_into_active_value!(&'static [u8], Set); +impl_into_active_value!(&'static str, Set); +impl_into_active_value!(String, Set); + +#[cfg(feature = "with-json")] +#[cfg_attr(docsrs, doc(cfg(feature = "with-json")))] +impl_into_active_value!(crate::prelude::Json, Set); + +#[cfg(feature = "with-chrono")] +#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))] +impl_into_active_value!(crate::prelude::Date, Set); + +#[cfg(feature = "with-chrono")] +#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))] +impl_into_active_value!(crate::prelude::Time, Set); + +#[cfg(feature = "with-chrono")] +#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))] +impl_into_active_value!(crate::prelude::DateTime, Set); + +#[cfg(feature = "with-chrono")] +#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))] +impl_into_active_value!(crate::prelude::DateTimeWithTimeZone, Set); + +#[cfg(feature = "with-rust_decimal")] +#[cfg_attr(docsrs, doc(cfg(feature = "with-rust_decimal")))] +impl_into_active_value!(crate::prelude::Decimal, Set); + +#[cfg(feature = "with-uuid")] +#[cfg_attr(docsrs, doc(cfg(feature = "with-uuid")))] +impl_into_active_value!(crate::prelude::Uuid, Set); + +impl IntoActiveValue> for Option +where + V: Into + Nullable, +{ + fn into_active_value(self) -> ActiveValue> { + match self { + Some(value) => Set(Some(value)), + None => Unset(None), + } + } +} + impl ActiveValue where V: Into, @@ -248,3 +321,16 @@ where self.value.as_ref() == other.value.as_ref() } } + +impl From> for ActiveValue> +where + V: Into + Nullable, +{ + fn from(value: ActiveValue) -> Self { + match value.state { + ActiveValueState::Set => Set(value.value), + ActiveValueState::Unset => Unset(None), + ActiveValueState::Unchanged => ActiveValue::unchanged(value.value), + } + } +}