1use super::{Decimal, Null, OrderedMap, TimeUnit, Type};
2
3#[derive(Clone, Debug, PartialEq)]
13#[non_exhaustive]
14pub enum Value {
15 Null,
17 Boolean(bool),
19 TinyInt(i8),
21 SmallInt(i16),
23 Int(i32),
25 BigInt(i64),
27 HugeInt(i128),
29 UHugeInt(u128),
36 UTinyInt(u8),
38 USmallInt(u16),
40 UInt(u32),
42 UBigInt(u64),
44 Float(f32),
46 Double(f64),
48 Decimal(Decimal),
53 Timestamp(TimeUnit, i64),
55 Text(String),
57 Blob(Vec<u8>),
59 Geometry(Vec<u8>),
69 Date32(i32),
75 Time64(TimeUnit, i64),
77 Interval {
79 months: i32,
81 days: i32,
83 nanos: i64,
85 },
86 List(Vec<Value>),
88 Enum(String),
90 Struct(OrderedMap<String, Value>),
92 Array(Vec<Value>),
94 Map(OrderedMap<Value, Value>),
96 Union(Box<Value>),
98}
99
100impl From<Null> for Value {
101 #[inline]
102 fn from(_: Null) -> Self {
103 Self::Null
104 }
105}
106
107impl From<bool> for Value {
108 #[inline]
109 fn from(i: bool) -> Self {
110 Self::Boolean(i)
111 }
112}
113
114impl From<usize> for Value {
115 #[inline]
116 fn from(i: usize) -> Self {
117 Self::UBigInt(i as u64)
118 }
119}
120
121impl From<isize> for Value {
122 #[inline]
123 fn from(i: isize) -> Self {
124 Self::BigInt(i as i64)
125 }
126}
127
128#[cfg(feature = "uuid")]
129impl From<uuid::Uuid> for Value {
130 #[inline]
131 fn from(id: uuid::Uuid) -> Self {
132 Self::Text(id.to_string())
133 }
134}
135
136impl From<i8> for Value {
137 #[inline]
138 fn from(i: i8) -> Self {
139 Self::TinyInt(i)
140 }
141}
142
143impl From<i16> for Value {
144 #[inline]
145 fn from(i: i16) -> Self {
146 Self::SmallInt(i)
147 }
148}
149
150impl From<i32> for Value {
151 #[inline]
152 fn from(i: i32) -> Self {
153 Self::Int(i)
154 }
155}
156
157impl From<i64> for Value {
158 #[inline]
159 fn from(i: i64) -> Self {
160 Self::BigInt(i)
161 }
162}
163
164impl From<u8> for Value {
165 #[inline]
166 fn from(i: u8) -> Self {
167 Self::UTinyInt(i)
168 }
169}
170
171impl From<u16> for Value {
172 #[inline]
173 fn from(i: u16) -> Self {
174 Self::USmallInt(i)
175 }
176}
177
178impl From<u32> for Value {
179 #[inline]
180 fn from(i: u32) -> Self {
181 Self::UInt(i)
182 }
183}
184
185impl From<u64> for Value {
186 #[inline]
187 fn from(i: u64) -> Self {
188 Self::UBigInt(i)
189 }
190}
191
192impl From<i128> for Value {
193 #[inline]
194 fn from(i: i128) -> Self {
195 Self::HugeInt(i)
196 }
197}
198
199impl From<u128> for Value {
200 #[inline]
201 fn from(i: u128) -> Self {
202 Self::UHugeInt(i)
203 }
204}
205
206impl From<Decimal> for Value {
207 #[inline]
208 fn from(decimal: Decimal) -> Self {
209 Self::Decimal(decimal)
210 }
211}
212
213impl From<f32> for Value {
214 #[inline]
215 fn from(f: f32) -> Self {
216 Self::Float(f)
217 }
218}
219
220impl From<f64> for Value {
221 #[inline]
222 fn from(f: f64) -> Self {
223 Self::Double(f)
224 }
225}
226
227impl From<String> for Value {
228 #[inline]
229 fn from(s: String) -> Self {
230 Self::Text(s)
231 }
232}
233
234impl From<Vec<u8>> for Value {
235 #[inline]
236 fn from(v: Vec<u8>) -> Self {
237 Self::Blob(v)
238 }
239}
240
241impl<T> From<Option<T>> for Value
242where
243 T: Into<Self>,
244{
245 #[inline]
246 fn from(v: Option<T>) -> Self {
247 match v {
248 Some(x) => x.into(),
249 None => Self::Null,
250 }
251 }
252}
253
254impl Value {
255 #[inline]
257 pub fn data_type(&self) -> Type {
258 match *self {
259 Self::Null => Type::Null,
260 Self::Boolean(_) => Type::Boolean,
261 Self::TinyInt(_) => Type::TinyInt,
262 Self::SmallInt(_) => Type::SmallInt,
263 Self::Int(_) => Type::Int,
264 Self::BigInt(_) => Type::BigInt,
265 Self::HugeInt(_) => Type::HugeInt,
266 Self::UHugeInt(_) => Type::UHugeInt,
267 Self::UTinyInt(_) => Type::UTinyInt,
268 Self::USmallInt(_) => Type::USmallInt,
269 Self::UInt(_) => Type::UInt,
270 Self::UBigInt(_) => Type::UBigInt,
271 Self::Float(_) => Type::Float,
272 Self::Double(_) => Type::Double,
273 Self::Decimal(_) => Type::Decimal,
274 Self::Timestamp(_, _) => Type::Timestamp,
275 Self::Text(_) => Type::Text,
276 Self::Blob(_) => Type::Blob,
277 Self::Geometry(_) => Type::Geometry,
278 Self::Date32(_) => Type::Date32,
279 Self::Time64(..) => Type::Time64,
280 Self::Interval { .. } => Type::Interval,
281 Self::Union(..) | Self::Struct(..) | Self::List(..) | Self::Array(..) | Self::Map(..) => todo!(),
282 Self::Enum(..) => Type::Enum,
283 }
284 }
285}