sql_builder/value/
postgres_array.rs1use super::*;
2use std::sync::Arc;
3
4pub trait NotU8 {}
7
8impl NotU8 for bool {}
9impl NotU8 for i8 {}
10impl NotU8 for i16 {}
11impl NotU8 for i32 {}
12impl NotU8 for i64 {}
13impl NotU8 for u16 {}
14impl NotU8 for u32 {}
15impl NotU8 for u64 {}
16impl NotU8 for f32 {}
17impl NotU8 for f64 {}
18impl NotU8 for char {}
19impl NotU8 for String {}
20impl NotU8 for Vec<u8> {}
21
22impl NotU8 for Json {}
23
24impl NotU8 for NaiveDate {}
25
26impl NotU8 for NaiveTime {}
27
28impl NotU8 for NaiveDateTime {}
29
30impl<Tz> NotU8 for DateTime<Tz> where Tz: chrono::TimeZone {}
31
32impl NotU8 for Decimal {}
33
34impl NotU8 for Uuid {}
35
36impl NotU8 for uuid::fmt::Braced {}
37
38impl NotU8 for uuid::fmt::Hyphenated {}
39
40impl NotU8 for uuid::fmt::Simple {}
41
42impl NotU8 for uuid::fmt::Urn {}
43
44impl<T> NotU8 for &T where T: NotU8 {}
46
47impl<T> From<Vec<T>> for Value
48where
49 T: Into<Value> + NotU8 + ValueType,
50{
51 fn from(x: Vec<T>) -> Value {
52 Value::Array(
53 T::array_type(),
54 Some(Box::new(x.into_iter().map(|e| e.into()).collect())),
55 )
56 }
57}
58
59impl<T> Nullable for Vec<T>
60where
61 T: Into<Value> + NotU8 + ValueType,
62{
63 fn null() -> Value {
64 Value::Array(T::array_type(), None)
65 }
66}
67
68impl<T> ValueType for Vec<T>
69where
70 T: NotU8 + ValueType,
71{
72 fn try_from(v: Value) -> Result<Self, ValueTypeErr> {
73 match v {
74 Value::Array(ty, Some(v)) if T::array_type() == ty => {
75 v.into_iter().map(try_array_element::<T>).collect()
76 }
77 _ => Err(ValueTypeErr),
78 }
79 }
80
81 fn type_name() -> String {
82 stringify!(Vec<T>).to_owned()
83 }
84
85 fn array_type() -> ArrayType {
86 T::array_type()
87 }
88
89 fn column_type() -> ColumnType {
90 use ColumnType::*;
91 Array(Arc::new(T::column_type()))
92 }
93}
94
95impl<T> ValueType for Vec<Option<T>>
96where
97 T: NotU8 + ValueType,
98{
99 fn try_from(v: Value) -> Result<Self, ValueTypeErr> {
100 match v {
101 Value::Array(ty, Some(v)) if T::array_type() == ty => v
102 .into_iter()
103 .map(|e| {
104 if e.is_some() {
105 T::try_from(e).map(Some)
106 } else {
107 Ok(None)
108 }
109 })
110 .collect(),
111 _ => Err(ValueTypeErr),
112 }
113 }
114
115 fn type_name() -> String {
116 stringify!(Vec<Option<T>>).to_owned()
117 }
118
119 fn array_type() -> ArrayType {
120 T::array_type()
121 }
122
123 fn column_type() -> ColumnType {
124 use ColumnType::*;
125 Array(Arc::new(T::column_type()))
126 }
127}
128
129impl Value {
130 pub fn is_array(&self) -> bool {
131 matches!(self, Self::Array(_, _))
132 }
133
134 pub fn as_ref_array(&self) -> Option<&Vec<Value>> {
135 match self {
136 Self::Array(_, v) => v.as_ref().map(|v| v.as_ref()),
137 _ => panic!("not Value::Array"),
138 }
139 }
140}
141
142pub fn try_array_element<T: ValueType>(value: Value) -> Result<T, ValueTypeErr> {
144 if value.is_some() {
145 T::try_from(value)
146 } else {
147 Err(ValueTypeErr)
148 }
149}
150
151pub fn enum_array_element(value: &Value) -> Result<&Enum, ValueTypeErr> {
153 match value {
154 Value::Enum(OptionEnum::Some(value)) => Ok(value),
155 _ => Err(ValueTypeErr),
156 }
157}