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