1use super::{Null, OrderedMap, TimeUnit, Type};
2use rust_decimal::prelude::*;
3
4#[derive(Clone, Debug, PartialEq)]
10pub enum Value {
11 Null,
13 Boolean(bool),
15 TinyInt(i8),
17 SmallInt(i16),
19 Int(i32),
21 BigInt(i64),
23 HugeInt(i128),
25 UTinyInt(u8),
27 USmallInt(u16),
29 UInt(u32),
31 UBigInt(u64),
33 Float(f32),
35 Double(f64),
37 Decimal(Decimal),
39 Timestamp(TimeUnit, i64),
41 Text(String),
43 Blob(Vec<u8>),
45 Date32(i32),
51 Time64(TimeUnit, i64),
53 Interval {
55 months: i32,
57 days: i32,
59 nanos: i64,
61 },
62 List(Vec<Value>),
64 Enum(String),
66 Struct(OrderedMap<String, Value>),
68 Array(Vec<Value>),
70 Map(OrderedMap<Value, Value>),
72 Union(Box<Value>),
74}
75
76impl From<Null> for Value {
77 #[inline]
78 fn from(_: Null) -> Self {
79 Self::Null
80 }
81}
82
83impl From<bool> for Value {
84 #[inline]
85 fn from(i: bool) -> Self {
86 Self::Boolean(i)
87 }
88}
89
90impl From<usize> for Value {
91 #[inline]
92 fn from(i: usize) -> Self {
93 Self::UBigInt(i as u64)
94 }
95}
96
97impl From<isize> for Value {
98 #[inline]
99 fn from(i: isize) -> Self {
100 Self::BigInt(i as i64)
101 }
102}
103
104#[cfg(feature = "uuid")]
105impl From<uuid::Uuid> for Value {
106 #[inline]
107 fn from(id: uuid::Uuid) -> Self {
108 Self::Text(id.to_string())
109 }
110}
111
112impl From<i8> for Value {
113 #[inline]
114 fn from(i: i8) -> Self {
115 Self::TinyInt(i)
116 }
117}
118
119impl From<i16> for Value {
120 #[inline]
121 fn from(i: i16) -> Self {
122 Self::SmallInt(i)
123 }
124}
125
126impl From<i32> for Value {
127 #[inline]
128 fn from(i: i32) -> Self {
129 Self::Int(i)
130 }
131}
132
133impl From<i64> for Value {
134 #[inline]
135 fn from(i: i64) -> Self {
136 Self::BigInt(i)
137 }
138}
139
140impl From<u8> for Value {
141 #[inline]
142 fn from(i: u8) -> Self {
143 Self::UTinyInt(i)
144 }
145}
146
147impl From<u16> for Value {
148 #[inline]
149 fn from(i: u16) -> Self {
150 Self::USmallInt(i)
151 }
152}
153
154impl From<u32> for Value {
155 #[inline]
156 fn from(i: u32) -> Self {
157 Self::UInt(i)
158 }
159}
160
161impl From<u64> for Value {
162 #[inline]
163 fn from(i: u64) -> Self {
164 Self::UBigInt(i)
165 }
166}
167
168impl From<i128> for Value {
169 #[inline]
170 fn from(i: i128) -> Self {
171 Self::HugeInt(i)
172 }
173}
174
175impl From<f32> for Value {
176 #[inline]
177 fn from(f: f32) -> Self {
178 Self::Float(f)
179 }
180}
181
182impl From<f64> for Value {
183 #[inline]
184 fn from(f: f64) -> Self {
185 Self::Double(f)
186 }
187}
188
189impl From<String> for Value {
190 #[inline]
191 fn from(s: String) -> Self {
192 Self::Text(s)
193 }
194}
195
196impl From<Vec<u8>> for Value {
197 #[inline]
198 fn from(v: Vec<u8>) -> Self {
199 Self::Blob(v)
200 }
201}
202
203impl<T> From<Option<T>> for Value
204where
205 T: Into<Self>,
206{
207 #[inline]
208 fn from(v: Option<T>) -> Self {
209 match v {
210 Some(x) => x.into(),
211 None => Self::Null,
212 }
213 }
214}
215
216impl Value {
217 #[inline]
219 pub fn data_type(&self) -> Type {
220 match *self {
221 Self::Null => Type::Null,
222 Self::Boolean(_) => Type::Boolean,
223 Self::TinyInt(_) => Type::TinyInt,
224 Self::SmallInt(_) => Type::SmallInt,
225 Self::Int(_) => Type::Int,
226 Self::BigInt(_) => Type::BigInt,
227 Self::HugeInt(_) => Type::HugeInt,
228 Self::UTinyInt(_) => Type::UTinyInt,
229 Self::USmallInt(_) => Type::USmallInt,
230 Self::UInt(_) => Type::UInt,
231 Self::UBigInt(_) => Type::UBigInt,
232 Self::Float(_) => Type::Float,
233 Self::Double(_) => Type::Double,
234 Self::Decimal(_) => Type::Decimal,
235 Self::Timestamp(_, _) => Type::Timestamp,
236 Self::Text(_) => Type::Text,
237 Self::Blob(_) => Type::Blob,
238 Self::Date32(_) => Type::Date32,
239 Self::Time64(..) => Type::Time64,
240 Self::Interval { .. } => Type::Interval,
241 Self::Union(..) | Self::Struct(..) | Self::List(..) | Self::Array(..) | Self::Map(..) => todo!(),
242 Self::Enum(..) => Type::Enum,
243 }
244 }
245}