1use crate::{AsValue, Error, Result, interval::Interval};
2use proc_macro2::TokenStream;
3use quote::{ToTokens, quote};
4use rust_decimal::Decimal;
5use std::{collections::HashMap, hash::Hash, mem::discriminant};
6use time::{Date, OffsetDateTime, PrimitiveDateTime, Time};
7use uuid::Uuid;
8
9#[derive(Default, Debug, Clone)]
15pub enum Value {
16 #[default]
18 Null,
19 Boolean(Option<bool>),
20 Int8(Option<i8>),
21 Int16(Option<i16>),
22 Int32(Option<i32>),
23 Int64(Option<i64>),
24 Int128(Option<i128>),
25 UInt8(Option<u8>),
26 UInt16(Option<u16>),
27 UInt32(Option<u32>),
28 UInt64(Option<u64>),
29 UInt128(Option<u128>),
30 Float32(Option<f32>),
31 Float64(Option<f64>),
32 Decimal(Option<Decimal>, u8, u8),
34 Char(Option<char>),
35 Varchar(Option<String>),
36 Blob(Option<Box<[u8]>>),
37 Date(Option<Date>),
38 Time(Option<Time>),
39 Timestamp(Option<PrimitiveDateTime>),
40 TimestampWithTimezone(Option<OffsetDateTime>),
41 Interval(Option<Interval>),
42 Uuid(Option<Uuid>),
43 Array(
45 Option<Box<[Value]>>,
46 Box<Value>,
47 u32,
48 ),
49 List(Option<Vec<Value>>, Box<Value>),
51 Map(
53 Option<HashMap<Value, Value>>,
54 Box<Value>,
55 Box<Value>,
56 ),
57 Struct(
59 Option<Vec<(String, Value)>>,
60 Vec<(String, Value)>,
61 ),
62 Unknown(Option<String>),
64}
65
66impl Value {
67 pub fn same_type(&self, other: &Self) -> bool {
68 match (self, other) {
69 (Self::Decimal(.., l_width, l_scale), Self::Decimal(.., r_width, r_scale)) => {
70 (*l_width == 0 || *r_width == 0 || l_width == r_width)
71 && (*l_scale == 0 || *r_scale == 0 || l_scale == r_scale)
72 }
73 (Self::Array(.., l_type, l_len), Self::Array(.., r_type, r_len)) => {
74 l_len == r_len && l_type.same_type(&r_type)
75 }
76 (Self::List(.., l), Self::List(.., r)) => l.same_type(r),
77 (Self::Map(.., l_key, l_value), Self::Map(.., r_key, r_value)) => {
78 l_key.same_type(r_key) && l_value.same_type(&r_value)
79 }
80 _ => discriminant(self) == discriminant(other),
81 }
82 }
83
84 pub fn is_null(&self) -> bool {
85 match self {
86 Value::Null
87 | Value::Boolean(None, ..)
88 | Value::Int8(None, ..)
89 | Value::Int16(None, ..)
90 | Value::Int32(None, ..)
91 | Value::Int64(None, ..)
92 | Value::Int128(None, ..)
93 | Value::UInt8(None, ..)
94 | Value::UInt16(None, ..)
95 | Value::UInt32(None, ..)
96 | Value::UInt64(None, ..)
97 | Value::UInt128(None, ..)
98 | Value::Float32(None, ..)
99 | Value::Float64(None, ..)
100 | Value::Decimal(None, ..)
101 | Value::Char(None, ..)
102 | Value::Varchar(None, ..)
103 | Value::Blob(None, ..)
104 | Value::Date(None, ..)
105 | Value::Time(None, ..)
106 | Value::Timestamp(None, ..)
107 | Value::TimestampWithTimezone(None, ..)
108 | Value::Interval(None, ..)
109 | Value::Uuid(None, ..)
110 | Value::Array(None, ..)
111 | Value::List(None, ..)
112 | Value::Map(None, ..)
113 | Value::Struct(None, ..)
114 | Value::Unknown(None, ..) => true,
115 _ => false,
116 }
117 }
118
119 pub fn as_null(&self) -> Value {
120 match self {
121 Value::Null => Value::Null,
122 Value::Boolean(..) => Value::Boolean(None),
123 Value::Int8(..) => Value::Int8(None),
124 Value::Int16(..) => Value::Int16(None),
125 Value::Int32(..) => Value::Int32(None),
126 Value::Int64(..) => Value::Int64(None),
127 Value::Int128(..) => Value::Int128(None),
128 Value::UInt8(..) => Value::UInt8(None),
129 Value::UInt16(..) => Value::UInt16(None),
130 Value::UInt32(..) => Value::UInt32(None),
131 Value::UInt64(..) => Value::UInt64(None),
132 Value::UInt128(..) => Value::UInt128(None),
133 Value::Float32(..) => Value::Float32(None),
134 Value::Float64(..) => Value::Float64(None),
135 Value::Decimal(.., w, s) => Value::Decimal(None, *w, *s),
136 Value::Char(..) => Value::Char(None),
137 Value::Varchar(..) => Value::Varchar(None),
138 Value::Blob(..) => Value::Blob(None),
139 Value::Date(..) => Value::Date(None),
140 Value::Time(..) => Value::Time(None),
141 Value::Timestamp(..) => Value::Timestamp(None),
142 Value::TimestampWithTimezone(..) => Value::TimestampWithTimezone(None),
143 Value::Interval(..) => Value::Interval(None),
144 Value::Uuid(..) => Value::Uuid(None),
145 Value::Array(.., t, len) => Value::Array(None, t.clone(), *len),
146 Value::List(.., t) => Value::List(None, t.clone()),
147 Value::Map(.., k, v) => Value::Map(None, k.clone(), v.clone()),
148 Value::Struct(.., t) => Value::Struct(None, t.clone()),
149 Value::Unknown(..) => Value::Unknown(None),
150 }
151 }
152
153 pub fn try_as(self, value: &Value) -> Result<Value> {
154 if self.same_type(value) {
155 return Ok(self);
156 }
157 match value {
158 Value::Boolean(..) => bool::try_from_value(self).map(AsValue::as_value),
159 Value::Int8(..) => i8::try_from_value(self).map(AsValue::as_value),
160 Value::Int16(..) => i16::try_from_value(self).map(AsValue::as_value),
161 Value::Int32(..) => i32::try_from_value(self).map(AsValue::as_value),
162 Value::Int64(..) => i64::try_from_value(self).map(AsValue::as_value),
163 Value::Int128(..) => i128::try_from_value(self).map(AsValue::as_value),
164 Value::UInt8(..) => u8::try_from_value(self).map(AsValue::as_value),
165 Value::UInt16(..) => u16::try_from_value(self).map(AsValue::as_value),
166 Value::UInt32(..) => u32::try_from_value(self).map(AsValue::as_value),
167 Value::UInt64(..) => u64::try_from_value(self).map(AsValue::as_value),
168 Value::UInt128(..) => u128::try_from_value(self).map(AsValue::as_value),
169 Value::Float32(..) => f32::try_from_value(self).map(AsValue::as_value),
170 Value::Float64(..) => f64::try_from_value(self).map(AsValue::as_value),
171 Value::Decimal(..) => Decimal::try_from_value(self).map(AsValue::as_value),
172 Value::Char(..) => char::try_from_value(self).map(AsValue::as_value),
173 Value::Varchar(..) => String::try_from_value(self).map(AsValue::as_value),
174 Value::Blob(..) => Box::<[u8]>::try_from_value(self).map(AsValue::as_value),
175 Value::Date(..) => Date::try_from_value(self).map(AsValue::as_value),
176 Value::Time(..) => Time::try_from_value(self).map(AsValue::as_value),
177 Value::Timestamp(..) => PrimitiveDateTime::try_from_value(self).map(AsValue::as_value),
178 Value::TimestampWithTimezone(..) => {
179 OffsetDateTime::try_from_value(self).map(AsValue::as_value)
180 }
181 Value::Interval(..) => Interval::try_from_value(self).map(AsValue::as_value),
182 Value::Uuid(..) => Uuid::try_from_value(self).map(AsValue::as_value),
183 _ => {
189 return Err(Error::msg(format!(
190 "Cannot convert value {:?} into value {:?}",
191 self, value
192 )));
193 }
194 }
195 }
196}
197
198impl PartialEq for Value {
199 fn eq(&self, other: &Self) -> bool {
200 match (self, other) {
201 (Self::Null, Self::Null) => true,
202 (Self::Boolean(l), Self::Boolean(r)) => l == r,
203 (Self::Int8(l), Self::Int8(r)) => l == r,
204 (Self::Int16(l), Self::Int16(r)) => l == r,
205 (Self::Int32(l), Self::Int32(r)) => l == r,
206 (Self::Int64(l), Self::Int64(r)) => l == r,
207 (Self::Int128(l), Self::Int128(r)) => l == r,
208 (Self::UInt8(l), Self::UInt8(r)) => l == r,
209 (Self::UInt16(l), Self::UInt16(r)) => l == r,
210 (Self::UInt32(l), Self::UInt32(r)) => l == r,
211 (Self::UInt64(l), Self::UInt64(r)) => l == r,
212 (Self::UInt128(l), Self::UInt128(r)) => l == r,
213 (Self::Float32(l), Self::Float32(r)) => {
214 l == r
215 || l.and_then(|l| r.and_then(|r| Some(l.is_nan() && r.is_nan())))
216 .unwrap_or_default()
217 }
218 (Self::Float64(l), Self::Float64(r)) => {
219 l == r
220 || l.and_then(|l| r.and_then(|r| Some(l.is_nan() && r.is_nan())))
221 .unwrap_or_default()
222 }
223 (Self::Decimal(l, l_width, l_scale), Self::Decimal(r, r_width, r_scale)) => {
224 l == r && l_width == r_width && l_scale == r_scale
225 }
226 (Self::Char(l), Self::Char(r)) => l == r,
227 (Self::Varchar(l), Self::Varchar(r)) => l == r,
228 (Self::Blob(l), Self::Blob(r)) => l == r,
229 (Self::Date(l), Self::Date(r)) => l == r,
230 (Self::Time(l), Self::Time(r)) => l == r,
231 (Self::Timestamp(l), Self::Timestamp(r)) => l == r,
232 (Self::TimestampWithTimezone(l), Self::TimestampWithTimezone(r)) => l == r,
233 (Self::Interval(l), Self::Interval(r)) => l == r,
234 (Self::Uuid(l), Self::Uuid(r)) => l == r,
235 (Self::Array(l, ..), Self::Array(r, ..)) => l == r && self.same_type(other),
236 (Self::List(l, ..), Self::List(r, ..)) => l == r && self.same_type(other),
237 (Self::Map(None, ..), Self::Map(None, ..)) => self.same_type(other),
238 (Self::Map(Some(l), ..), Self::Map(Some(r), ..)) => {
239 l.is_empty() == r.is_empty() && self.same_type(other)
240 }
241 (Self::Map(..), Self::Map(..)) => self.same_type(other),
242 (Self::Unknown(..), Self::Unknown(..)) => false,
243 _ => false,
244 }
245 }
246}
247
248impl Eq for Value {}
249
250impl Hash for Value {
251 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
252 use Value::*;
253 discriminant(self).hash(state);
254 match self {
255 Null => {}
256 Boolean(v) => v.hash(state),
257 Int8(v) => v.hash(state),
258 Int16(v) => v.hash(state),
259 Int32(v) => v.hash(state),
260 Int64(v) => v.hash(state),
261 Int128(v) => v.hash(state),
262
263 UInt8(v) => v.hash(state),
264 UInt16(v) => v.hash(state),
265 UInt32(v) => v.hash(state),
266 UInt64(v) => v.hash(state),
267 UInt128(v) => v.hash(state),
268 Float32(Some(v)) => {
269 v.to_bits().hash(state);
270 }
271 Float32(None) => None::<u32>.hash(state),
272 Float64(Some(v)) => {
273 v.to_bits().hash(state);
274 }
275 Float64(None) => None::<u64>.hash(state),
276 Decimal(v, width, scale) => {
277 v.hash(state);
278 width.hash(state);
279 scale.hash(state);
280 }
281 Char(v) => v.hash(state),
282 Varchar(v) => v.hash(state),
283 Blob(v) => v.hash(state),
284 Date(v) => v.hash(state),
285 Time(v) => v.hash(state),
286 Timestamp(v) => v.hash(state),
287 TimestampWithTimezone(v) => v.hash(state),
288 Interval(v) => v.hash(state),
289 Uuid(v) => v.hash(state),
290 Array(v, typ, len) => {
291 v.hash(state);
292 typ.hash(state);
293 len.hash(state);
294 }
295 List(v, typ) => {
296 v.hash(state);
297 typ.hash(state);
298 }
299 Map(v, key, val) => {
300 match v {
301 Some(map) => {
302 for (key, val) in map {
303 key.hash(state);
304 val.hash(state);
305 }
306 }
307 None => {}
308 }
309 key.hash(state);
310 val.hash(state);
311 }
312 Struct(v, t) => {
313 match v {
314 Some(v) => v.hash(state),
315 None => {}
316 }
317 t.hash(state);
318 }
319 Unknown(v) => v.hash(state),
320 }
321 }
322}
323
324#[derive(Default)]
326pub struct TypeDecoded {
327 pub value: Value,
329 pub nullable: bool,
331 pub passive: bool,
333}
334
335pub type CheckPassive = Box<dyn Fn(TokenStream) -> TokenStream>;
336
337impl ToTokens for Value {
338 fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
339 let ts = match self {
340 Value::Null => quote!(::tank::Value::Null),
341 Value::Boolean(..) => quote!(::tank::Value::Boolean(None)),
342 Value::Int8(..) => quote!(::tank::Value::Int8(None)),
343 Value::Int16(..) => quote!(::tank::Value::Int16(None)),
344 Value::Int32(..) => quote!(::tank::Value::Int32(None)),
345 Value::Int64(..) => quote!(::tank::Value::Int64(None)),
346 Value::Int128(..) => quote!(::tank::Value::Int128(None)),
347 Value::UInt8(..) => quote!(::tank::Value::UInt8(None)),
348 Value::UInt16(..) => quote!(::tank::Value::UInt16(None)),
349 Value::UInt32(..) => quote!(::tank::Value::UInt32(None)),
350 Value::UInt64(..) => quote!(::tank::Value::UInt64(None)),
351 Value::UInt128(..) => quote!(::tank::Value::UInt128(None)),
352 Value::Float32(..) => quote!(::tank::Value::Float32(None)),
353 Value::Float64(..) => quote!(::tank::Value::Float64(None)),
354 Value::Decimal(.., width, scale) => {
355 quote!(::tank::Value::Decimal(None, #width, #scale))
356 }
357 Value::Char(..) => quote!(tank::Value::Char(None)),
358 Value::Varchar(..) => quote!(::tank::Value::Varchar(None)),
359 Value::Blob(..) => quote!(::tank::Value::Blob(None)),
360 Value::Date(..) => quote!(::tank::Value::Date(None)),
361 Value::Time(..) => quote!(::tank::Value::Time(None)),
362 Value::Timestamp(..) => quote!(::tank::Value::Timestamp(None)),
363 Value::TimestampWithTimezone(..) => quote!(::tank::Value::TimestampWithTimezone(None)),
364 Value::Interval(..) => quote!(::tank::Value::Interval(None)),
365 Value::Uuid(..) => quote!(::tank::Value::Uuid(None)),
366 Value::Array(.., inner, size) => {
367 quote!(::tank::Value::Array(None, Box::new(#inner), #size))
368 }
369 Value::List(.., inner) => {
370 let inner = inner.as_ref().to_token_stream();
371 quote!(::tank::Value::List(None, Box::new(#inner)))
372 }
373 Value::Map(.., key, value) => {
374 let key = key.as_ref().to_token_stream();
375 let value = value.as_ref().to_token_stream();
376 quote!(::tank::Value::Map(None, Box::new(#key), Box::new(#value)))
377 }
378 Value::Struct(.., t) => {
379 let values = t.into_iter().map(|(k, v)| quote!((#k.into(), #v)));
380 quote!(::tank::Value::Struct(None, vec!(#(#values),*)))
381 }
382 Value::Unknown(..) => quote!(::tank::Value::Unknown(None)),
383 };
384 tokens.extend(ts);
385 }
386}