1use crate::sea_query::{Nullable, ValueType};
2use crate::{ActiveValue, Value};
3
4mod timestamp;
5use timestamp::*;
6
7#[cfg(feature = "with-chrono")]
8mod with_chrono;
9#[cfg(feature = "with-chrono")]
10pub use with_chrono::*;
11
12#[cfg(feature = "with-time")]
13mod with_time;
14#[cfg(feature = "with-time")]
15pub use with_time::*;
16
17pub trait DefaultActiveValue {
19 fn default_value(&self) -> Self;
21}
22
23pub trait DefaultActiveValueNone {
25 fn default_value(&self) -> Self;
27}
28
29pub trait DefaultActiveValueNotSet {
31 type Value;
33
34 fn default_value(&self) -> Self::Value;
36}
37
38impl<V> DefaultActiveValue for ActiveValue<V>
39where
40 V: Into<Value> + ValueType + Nullable,
41{
42 fn default_value(&self) -> Self {
43 match V::try_from(V::null().dummy_value()) {
44 Ok(v) => ActiveValue::Set(v),
45 Err(_) => ActiveValue::NotSet,
46 }
47 }
48}
49
50impl<V> DefaultActiveValueNone for ActiveValue<Option<V>>
51where
52 V: Into<Value> + Nullable,
53{
54 fn default_value(&self) -> Self {
55 ActiveValue::Set(None)
56 }
57}
58
59impl<V> DefaultActiveValueNotSet for &ActiveValue<V>
60where
61 V: Into<Value>,
62{
63 type Value = ActiveValue<V>;
64
65 fn default_value(&self) -> Self::Value {
66 ActiveValue::NotSet
67 }
68}
69
70#[cfg(test)]
71mod test {
72 use super::*;
73 use crate::prelude::TimeDateTime;
74
75 #[test]
76 fn test_default_value() {
77 let v = (&ActiveValue::<i32>::NotSet).default_value();
78 assert_eq!(v, ActiveValue::Set(0));
79
80 let v = (&ActiveValue::<Option<i32>>::NotSet).default_value();
81 assert_eq!(v, ActiveValue::Set(None));
82
83 let v = (&ActiveValue::<String>::NotSet).default_value();
84 assert_eq!(v, ActiveValue::Set("".to_owned()));
85
86 let v = (&ActiveValue::<Option<String>>::NotSet).default_value();
87 assert_eq!(v, ActiveValue::Set(None));
88
89 let v = (&ActiveValue::<TimeDateTime>::NotSet).default_value();
90 assert!(matches!(v, ActiveValue::Set(_)));
91 }
92}