use crate::sea_query::{Nullable, ValueType};
use crate::{ActiveValue, Value};
mod timestamp;
use timestamp::*;
#[cfg(feature = "with-chrono")]
mod with_chrono;
#[cfg(feature = "with-chrono")]
pub use with_chrono::*;
#[cfg(feature = "with-time")]
mod with_time;
#[cfg(feature = "with-time")]
pub use with_time::*;
#[cfg(feature = "with-uuid")]
mod text_uuid;
#[cfg(feature = "with-uuid")]
pub use text_uuid::*;
pub trait DefaultActiveValue {
fn default_value(&self) -> Self;
}
pub trait DefaultActiveValueNone {
fn default_value(&self) -> Self;
}
pub trait DefaultActiveValueNotSet {
type Value;
fn default_value(&self) -> Self::Value;
}
impl<V> DefaultActiveValue for ActiveValue<V>
where
V: Into<Value> + ValueType + Nullable,
{
fn default_value(&self) -> Self {
match V::try_from(V::null().dummy_value()) {
Ok(v) => ActiveValue::Set(v),
Err(_) => ActiveValue::NotSet,
}
}
}
impl<V> DefaultActiveValueNone for ActiveValue<Option<V>>
where
V: Into<Value> + Nullable,
{
fn default_value(&self) -> Self {
ActiveValue::Set(None)
}
}
impl<V> DefaultActiveValueNotSet for &ActiveValue<V>
where
V: Into<Value>,
{
type Value = ActiveValue<V>;
fn default_value(&self) -> Self::Value {
ActiveValue::NotSet
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::prelude::TimeDateTime;
#[test]
fn test_default_value() {
let v = (&ActiveValue::<i32>::NotSet).default_value();
assert_eq!(v, ActiveValue::Set(0));
let v = (&ActiveValue::<Option<i32>>::NotSet).default_value();
assert_eq!(v, ActiveValue::Set(None));
let v = (&ActiveValue::<String>::NotSet).default_value();
assert_eq!(v, ActiveValue::Set("".to_owned()));
let v = (&ActiveValue::<Option<String>>::NotSet).default_value();
assert_eq!(v, ActiveValue::Set(None));
let v = (&ActiveValue::<TimeDateTime>::NotSet).default_value();
assert!(matches!(v, ActiveValue::Set(_)));
}
}