1use crate::sea_query::{Nullable, ValueType};
2use crate::{ActiveValue, Value};
3
4pub trait DefaultActiveValue {
6 fn default_value(&self) -> Self;
8}
9
10pub trait DefaultActiveValueNone {
12 fn default_value(&self) -> Self;
14}
15
16pub trait DefaultActiveValueNotSet {
18 type Value;
20
21 fn default_value(&self) -> Self::Value;
23}
24
25impl<V> DefaultActiveValue for ActiveValue<V>
26where
27 V: Into<Value> + ValueType + Nullable,
28{
29 fn default_value(&self) -> Self {
30 match V::try_from(V::null().dummy_value()) {
31 Ok(v) => ActiveValue::Set(v),
32 Err(_) => ActiveValue::NotSet,
33 }
34 }
35}
36
37impl<V> DefaultActiveValueNone for ActiveValue<Option<V>>
38where
39 V: Into<Value> + Nullable,
40{
41 fn default_value(&self) -> Self {
42 ActiveValue::Set(None)
43 }
44}
45
46impl<V> DefaultActiveValueNotSet for &ActiveValue<V>
47where
48 V: Into<Value>,
49{
50 type Value = ActiveValue<V>;
51
52 fn default_value(&self) -> Self::Value {
53 ActiveValue::NotSet
54 }
55}
56
57#[cfg(test)]
58mod test {
59 use super::*;
60 use crate::prelude::TimeDateTime;
61
62 #[test]
63 fn test_default_value() {
64 let v = (&ActiveValue::<i32>::NotSet).default_value();
65 assert_eq!(v, ActiveValue::Set(0));
66
67 let v = (&ActiveValue::<Option<i32>>::NotSet).default_value();
68 assert_eq!(v, ActiveValue::Set(None));
69
70 let v = (&ActiveValue::<String>::NotSet).default_value();
71 assert_eq!(v, ActiveValue::Set("".to_owned()));
72
73 let v = (&ActiveValue::<Option<String>>::NotSet).default_value();
74 assert_eq!(v, ActiveValue::Set(None));
75
76 let v = (&ActiveValue::<TimeDateTime>::NotSet).default_value();
77 assert!(matches!(v, ActiveValue::Set(_)));
78 }
79}