sea_query/value/
with_array.rs

1use super::*;
2use crate::RcOrArc;
3
4// We only implement conversion from Vec<T> to Array when T is not u8.
5// This is because for u8's case, there is already conversion to Byte defined above.
6// TODO When negative trait becomes a stable feature, following code can be much shorter.
7pub trait NotU8 {}
8
9impl NotU8 for bool {}
10impl NotU8 for i8 {}
11impl NotU8 for i16 {}
12impl NotU8 for i32 {}
13impl NotU8 for i64 {}
14impl NotU8 for u16 {}
15impl NotU8 for u32 {}
16impl NotU8 for u64 {}
17impl NotU8 for f32 {}
18impl NotU8 for f64 {}
19impl NotU8 for char {}
20impl NotU8 for String {}
21impl NotU8 for Vec<u8> {}
22
23// TODO impl<T: NotU8> NotU8 for Option<T> {}
24
25#[cfg(feature = "with-json")]
26impl NotU8 for Json {}
27
28#[cfg(feature = "with-chrono")]
29impl NotU8 for NaiveDate {}
30
31#[cfg(feature = "with-chrono")]
32impl NotU8 for NaiveTime {}
33
34#[cfg(feature = "with-chrono")]
35impl NotU8 for NaiveDateTime {}
36
37#[cfg(feature = "with-chrono")]
38impl<Tz> NotU8 for DateTime<Tz> where Tz: chrono::TimeZone {}
39
40#[cfg(feature = "with-time")]
41impl NotU8 for time::Date {}
42
43#[cfg(feature = "with-time")]
44impl NotU8 for time::Time {}
45
46#[cfg(feature = "with-time")]
47impl NotU8 for PrimitiveDateTime {}
48
49#[cfg(feature = "with-time")]
50impl NotU8 for OffsetDateTime {}
51
52#[cfg(feature = "with-rust_decimal")]
53impl NotU8 for Decimal {}
54
55#[cfg(feature = "with-bigdecimal")]
56impl NotU8 for BigDecimal {}
57
58#[cfg(feature = "with-uuid")]
59impl NotU8 for Uuid {}
60
61#[cfg(feature = "with-uuid")]
62impl NotU8 for uuid::fmt::Braced {}
63
64#[cfg(feature = "with-uuid")]
65impl NotU8 for uuid::fmt::Hyphenated {}
66
67#[cfg(feature = "with-uuid")]
68impl NotU8 for uuid::fmt::Simple {}
69
70#[cfg(feature = "with-uuid")]
71impl NotU8 for uuid::fmt::Urn {}
72
73#[cfg(feature = "with-ipnetwork")]
74impl NotU8 for IpNetwork {}
75
76#[cfg(feature = "with-mac_address")]
77impl NotU8 for MacAddress {}
78
79impl<T> From<Vec<T>> for Value
80where
81    T: Into<Value> + NotU8 + ValueType,
82{
83    fn from(x: Vec<T>) -> Value {
84        Value::Array(
85            T::array_type(),
86            Some(Box::new(x.into_iter().map(|e| e.into()).collect())),
87        )
88    }
89}
90
91impl<T> Nullable for Vec<T>
92where
93    T: Into<Value> + NotU8 + ValueType,
94{
95    fn null() -> Value {
96        Value::Array(T::array_type(), None)
97    }
98}
99
100impl<T> ValueType for Vec<T>
101where
102    T: NotU8 + ValueType,
103{
104    fn try_from(v: Value) -> Result<Self, ValueTypeErr> {
105        match v {
106            Value::Array(ty, Some(v)) if T::array_type() == ty => {
107                Ok(v.into_iter().map(|e| e.unwrap()).collect())
108            }
109            _ => Err(ValueTypeErr),
110        }
111    }
112
113    fn type_name() -> String {
114        stringify!(Vec<T>).to_owned()
115    }
116
117    fn array_type() -> ArrayType {
118        T::array_type()
119    }
120
121    fn column_type() -> ColumnType {
122        use ColumnType::*;
123        Array(RcOrArc::new(T::column_type()))
124    }
125}
126
127impl Value {
128    pub fn is_array(&self) -> bool {
129        matches!(self, Self::Array(_, _))
130    }
131
132    pub fn as_ref_array(&self) -> Option<&Vec<Value>> {
133        match self {
134            Self::Array(_, v) => v.as_ref().map(|v| v.as_ref()),
135            _ => panic!("not Value::Array"),
136        }
137    }
138}