tank-core 0.45.0

Core of Tank: the Rust data layer. This is intended to be used by drivers to implement a backend.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
use crate::{
    AsValue, DynQuery, Expression, GenericSqlWriter, Result, TableRef, interval::Interval,
};
use anyhow::anyhow;
use quote::{ToTokens, quote};
use rust_decimal::Decimal;
use serde_json::Value as JsonValue;
use std::{
    borrow::Cow,
    collections::{HashMap, hash_map::DefaultHasher},
    fmt::{self, Display},
    hash::{Hash, Hasher},
    iter,
    mem::discriminant,
};
use time::{Date, OffsetDateTime, PrimitiveDateTime, Time};
use uuid::Uuid;

/// SQL value representation.
///
/// Variants correspond to database column types.
#[derive(Default, Debug, Clone)]
pub enum Value<'a> {
    /// SQL NULL.
    #[default]
    Null,
    /// Boolean value.
    Boolean(Option<bool>),
    /// 8-bit signed integer.
    Int8(Option<i8>),
    /// 16-bit signed integer.
    Int16(Option<i16>),
    /// 32-bit signed integer.
    Int32(Option<i32>),
    /// 64-bit signed integer.
    Int64(Option<i64>),
    /// 128-bit signed integer.
    Int128(Option<Box<i128>>),
    /// 8-bit unsigned integer.
    UInt8(Option<u8>),
    /// 16-bit unsigned integer.
    UInt16(Option<u16>),
    /// 32-bit unsigned integer.
    UInt32(Option<u32>),
    /// 64-bit unsigned integer.
    UInt64(Option<u64>),
    /// 128-bit unsigned integer.
    UInt128(Option<Box<u128>>),
    /// 32-bit floating point number.
    Float32(Option<f32>),
    /// 64-bit floating point number.
    Float64(Option<f64>),
    /// Decimal (value, precision, scale).
    Decimal(Option<Decimal>, u8, u8),
    /// Single character.
    Char(Option<char>),
    /// Variable-length character string.
    Varchar(Option<Cow<'a, str>>),
    /// Binary large object.
    Blob(Option<Cow<'a, [u8]>>),
    /// Date value (without time).
    Date(Option<Date>),
    /// Time value (without date).
    Time(Option<Time>),
    /// Timestamp (date and time).
    Timestamp(Option<PrimitiveDateTime>),
    /// Timestamp with time zone.
    TimestampWithTimezone(Option<OffsetDateTime>),
    /// Time interval.
    Interval(Option<Interval>),
    /// UUID (Universally Unique Identifier).
    Uuid(Option<Uuid>),
    /// Homogeneous array (values, element type info).
    Array(Option<Box<[Value<'a>]>>, Box<CollectionType<'a>>),
    /// Variable-length list (values, element type info).
    List(Option<Box<[Value<'a>]>>, Box<CollectionType<'a>>),
    /// Map (entries, key and value types).
    Map(Option<Box<HashMap<Value<'a>, Value<'a>>>>, Box<MapType<'a>>),
    /// JSON value.
    Json(Option<JsonValue>),
    /// Named struct (fields, field types and table name).
    Struct(Option<Box<[(String, Value<'a>)]>>, Box<StructType<'a>>),
    /// Unknown type (usually used when no further information is available).
    Unknown(Option<Cow<'a, str>>),
}

/// Collection element type and fixed length, shared by [`Value::Array`] and [`Value::List`].
#[derive(Debug, Clone)]
pub struct CollectionType<'a> {
    /// Element type.
    pub element: Value<'a>,
    /// Fixed length (0 for a variable-length collection).
    pub len: u32,
}

/// Map key and value types.
#[derive(Debug, Clone)]
pub struct MapType<'a> {
    /// Key type.
    pub key: Value<'a>,
    /// Value type.
    pub value: Value<'a>,
}

/// Struct field types and table reference.
#[derive(Debug, Clone)]
pub struct StructType<'a> {
    /// Field types.
    pub fields: Vec<(String, Value<'a>)>,
    /// Table reference.
    pub name: TableRef,
}

/// Owning SQL value, with all payloads owned (`Cow::Owned`) or static scope.
pub type ValueOwned = Value<'static>;

impl<'a> Value<'a> {
    /// Checks if two values have the same type, ignoring their actual data.
    pub fn same_type(&self, other: &Value<'_>) -> bool {
        match (self, other) {
            (Value::Decimal(.., l_width, l_scale), Value::Decimal(.., r_width, r_scale)) => {
                (*l_width == 0 || *r_width == 0 || l_width == r_width)
                    && (*l_scale == 0 || *r_scale == 0 || l_scale == r_scale)
            }
            (Value::Array(.., l), Value::Array(.., r))
            | (Value::List(.., l), Value::List(.., r)) => {
                l.len == r.len && l.element.same_type(&r.element)
            }
            (Value::Map(.., l), Value::Map(.., r)) => {
                l.key.same_type(&r.key) && l.value.same_type(&r.value)
            }
            (Value::Struct(.., l), Value::Struct(.., r)) => {
                l.name == r.name
                    && l.fields.len() == r.fields.len()
                    && iter::zip(&l.fields, &r.fields)
                        .all(|((ln, lt), (rn, rt))| ln == rn && lt.same_type(rt))
            }
            _ => discriminant(self) == discriminant(other),
        }
    }

    /// Checks if the value is NULL.
    #[inline(always)]
    pub fn is_null(&self) -> bool {
        match self {
            Value::Null
            | Value::Boolean(None, ..)
            | Value::Int8(None, ..)
            | Value::Int16(None, ..)
            | Value::Int32(None, ..)
            | Value::Int64(None, ..)
            | Value::Int128(None, ..)
            | Value::UInt8(None, ..)
            | Value::UInt16(None, ..)
            | Value::UInt32(None, ..)
            | Value::UInt64(None, ..)
            | Value::UInt128(None, ..)
            | Value::Float32(None, ..)
            | Value::Float64(None, ..)
            | Value::Decimal(None, ..)
            | Value::Char(None, ..)
            | Value::Varchar(None, ..)
            | Value::Blob(None, ..)
            | Value::Date(None, ..)
            | Value::Time(None, ..)
            | Value::Timestamp(None, ..)
            | Value::TimestampWithTimezone(None, ..)
            | Value::Interval(None, ..)
            | Value::Uuid(None, ..)
            | Value::Array(None, ..)
            | Value::List(None, ..)
            | Value::Map(None, ..)
            | Value::Json(None | Some(JsonValue::Null), ..)
            | Value::Struct(None, ..)
            | Value::Unknown(None, ..) => true,
            _ => false,
        }
    }

    /// Create a value retaining only the type information, with all data set to NULL.
    pub fn as_null(&self) -> Value<'a> {
        match self {
            Value::Null => Value::Null,
            Value::Boolean(..) => Value::Boolean(None),
            Value::Int8(..) => Value::Int8(None),
            Value::Int16(..) => Value::Int16(None),
            Value::Int32(..) => Value::Int32(None),
            Value::Int64(..) => Value::Int64(None),
            Value::Int128(..) => Value::Int128(None),
            Value::UInt8(..) => Value::UInt8(None),
            Value::UInt16(..) => Value::UInt16(None),
            Value::UInt32(..) => Value::UInt32(None),
            Value::UInt64(..) => Value::UInt64(None),
            Value::UInt128(..) => Value::UInt128(None),
            Value::Float32(..) => Value::Float32(None),
            Value::Float64(..) => Value::Float64(None),
            Value::Decimal(.., w, s) => Value::Decimal(None, *w, *s),
            Value::Char(..) => Value::Char(None),
            Value::Varchar(..) => Value::Varchar(None),
            Value::Blob(..) => Value::Blob(None),
            Value::Date(..) => Value::Date(None),
            Value::Time(..) => Value::Time(None),
            Value::Timestamp(..) => Value::Timestamp(None),
            Value::TimestampWithTimezone(..) => Value::TimestampWithTimezone(None),
            Value::Interval(..) => Value::Interval(None),
            Value::Uuid(..) => Value::Uuid(None),
            Value::Array(.., t) => Value::Array(None, t.clone()),
            Value::List(.., t) => Value::List(None, t.clone()),
            Value::Map(.., t) => Value::Map(None, t.clone()),
            Value::Json(..) => Value::Json(None),
            Value::Struct(.., t) => Value::Struct(None, t.clone()),
            Value::Unknown(..) => Value::Unknown(None),
        }
    }

    /// Attempts to convert the value to the type of another value, if they are compatible.
    pub fn try_as(self, target_type: &Value<'_>) -> Result<Value<'a>> {
        if self.same_type(target_type) {
            return Ok(self);
        }
        match target_type {
            Value::Boolean(..) => bool::try_from_value(self).map(AsValue::as_value),
            Value::Int8(..) => i8::try_from_value(self).map(AsValue::as_value),
            Value::Int16(..) => i16::try_from_value(self).map(AsValue::as_value),
            Value::Int32(..) => i32::try_from_value(self).map(AsValue::as_value),
            Value::Int64(..) => i64::try_from_value(self).map(AsValue::as_value),
            Value::Int128(..) => i128::try_from_value(self).map(AsValue::as_value),
            Value::UInt8(..) => u8::try_from_value(self).map(AsValue::as_value),
            Value::UInt16(..) => u16::try_from_value(self).map(AsValue::as_value),
            Value::UInt32(..) => u32::try_from_value(self).map(AsValue::as_value),
            Value::UInt64(..) => u64::try_from_value(self).map(AsValue::as_value),
            Value::UInt128(..) => u128::try_from_value(self).map(AsValue::as_value),
            Value::Float32(..) => f32::try_from_value(self).map(AsValue::as_value),
            Value::Float64(..) => f64::try_from_value(self).map(AsValue::as_value),
            Value::Decimal(..) => Decimal::try_from_value(self).map(AsValue::as_value),
            Value::Char(..) => char::try_from_value(self).map(AsValue::as_value),
            Value::Varchar(..) => String::try_from_value(self).map(AsValue::as_value),
            Value::Blob(..) => Box::<[u8]>::try_from_value(self).map(AsValue::as_value),
            Value::Date(..) => Date::try_from_value(self).map(AsValue::as_value),
            Value::Time(..) => Time::try_from_value(self).map(AsValue::as_value),
            Value::Timestamp(..) => PrimitiveDateTime::try_from_value(self).map(AsValue::as_value),
            Value::TimestampWithTimezone(..) => {
                OffsetDateTime::try_from_value(self).map(AsValue::as_value)
            }
            Value::Interval(..) => Interval::try_from_value(self).map(AsValue::as_value),
            Value::Uuid(..) => Uuid::try_from_value(self).map(AsValue::as_value),
            // Value::Array(.., ty, len) => {
            //     Box::<[Value]>::try_from_value(self).map(AsValue::as_value)
            // }
            // Value::List(..) => Box::<[Value]>::try_from_value(self).map(AsValue::as_value),
            // Value::Map(..) => Date::try_from_value(self).map(AsValue::as_value),
            _ => {
                return Err(anyhow!(
                    "Cannot convert value {:?} to {:?}",
                    self,
                    target_type
                ));
            }
        }
    }

    /// Checks if the value is a scalar type (not an array, struct, etc.).
    pub fn is_scalar(&self) -> bool {
        match self {
            Value::Boolean(..)
            | Value::Int8(..)
            | Value::Int16(..)
            | Value::Int32(..)
            | Value::Int64(..)
            | Value::Int128(..)
            | Value::UInt8(..)
            | Value::UInt16(..)
            | Value::UInt32(..)
            | Value::UInt64(..)
            | Value::UInt128(..)
            | Value::Float32(..)
            | Value::Float64(..)
            | Value::Decimal(..)
            | Value::Char(..)
            | Value::Varchar(..)
            | Value::Blob(..)
            | Value::Date(..)
            | Value::Time(..)
            | Value::Timestamp(..)
            | Value::TimestampWithTimezone(..)
            | Value::Interval(..)
            | Value::Uuid(..)
            | Value::Unknown(..) => true,
            _ => false,
        }
    }

    /// Converts this value into an owning [`ValueOwned`], cloning any borrowed payload.
    pub fn into_owned(self) -> ValueOwned {
        match self {
            Value::Null => Value::Null,
            Value::Boolean(v) => Value::Boolean(v),
            Value::Int8(v) => Value::Int8(v),
            Value::Int16(v) => Value::Int16(v),
            Value::Int32(v) => Value::Int32(v),
            Value::Int64(v) => Value::Int64(v),
            Value::Int128(v) => Value::Int128(v),
            Value::UInt8(v) => Value::UInt8(v),
            Value::UInt16(v) => Value::UInt16(v),
            Value::UInt32(v) => Value::UInt32(v),
            Value::UInt64(v) => Value::UInt64(v),
            Value::UInt128(v) => Value::UInt128(v),
            Value::Float32(v) => Value::Float32(v),
            Value::Float64(v) => Value::Float64(v),
            Value::Decimal(v, w, s) => Value::Decimal(v, w, s),
            Value::Char(v) => Value::Char(v),
            Value::Varchar(v) => Value::Varchar(v.map(|v| Cow::Owned(v.into_owned()))),
            Value::Blob(v) => Value::Blob(v.map(|v| Cow::Owned(v.into_owned()))),
            Value::Date(v) => Value::Date(v),
            Value::Time(v) => Value::Time(v),
            Value::Timestamp(v) => Value::Timestamp(v),
            Value::TimestampWithTimezone(v) => Value::TimestampWithTimezone(v),
            Value::Interval(v) => Value::Interval(v),
            Value::Uuid(v) => Value::Uuid(v),
            Value::Array(v, t) => Value::Array(
                v.map(|v| v.into_vec().into_iter().map(Value::into_owned).collect()),
                Box::new(CollectionType {
                    element: t.element.into_owned(),
                    len: t.len,
                }),
            ),
            Value::List(v, t) => Value::List(
                v.map(|v| v.into_vec().into_iter().map(Value::into_owned).collect()),
                Box::new(CollectionType {
                    element: t.element.into_owned(),
                    len: t.len,
                }),
            ),
            Value::Map(v, t) => Value::Map(
                v.map(|v| {
                    Box::new(
                        v.into_iter()
                            .map(|(k, v)| (k.into_owned(), v.into_owned()))
                            .collect(),
                    )
                }),
                Box::new(MapType {
                    key: t.key.into_owned(),
                    value: t.value.into_owned(),
                }),
            ),
            Value::Json(v) => Value::Json(v),
            Value::Struct(v, t) => Value::Struct(
                v.map(|v| {
                    v.into_vec()
                        .into_iter()
                        .map(|(k, v)| (k, v.into_owned()))
                        .collect()
                }),
                Box::new(StructType {
                    fields: t
                        .fields
                        .into_iter()
                        .map(|(k, v)| (k, v.into_owned()))
                        .collect(),
                    name: t.name,
                }),
            ),
            Value::Unknown(v) => Value::Unknown(v.map(|v| Cow::Owned(v.into_owned()))),
        }
    }
}

impl<'a> PartialEq for Value<'a> {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Null, Self::Null) => true,
            (Self::Boolean(l), Self::Boolean(r)) => l == r,
            (Self::Int8(l), Self::Int8(r)) => l == r,
            (Self::Int16(l), Self::Int16(r)) => l == r,
            (Self::Int32(l), Self::Int32(r)) => l == r,
            (Self::Int64(l), Self::Int64(r)) => l == r,
            (Self::Int128(l), Self::Int128(r)) => l == r,
            (Self::UInt8(l), Self::UInt8(r)) => l == r,
            (Self::UInt16(l), Self::UInt16(r)) => l == r,
            (Self::UInt32(l), Self::UInt32(r)) => l == r,
            (Self::UInt64(l), Self::UInt64(r)) => l == r,
            (Self::UInt128(l), Self::UInt128(r)) => l == r,
            (Self::Float32(l), Self::Float32(r)) => {
                l == r
                    || l.and_then(|l| r.and_then(|r| Some(l.is_nan() && r.is_nan())))
                        .unwrap_or_default()
            }
            (Self::Float64(l), Self::Float64(r)) => {
                l == r
                    || l.and_then(|l| r.and_then(|r| Some(l.is_nan() && r.is_nan())))
                        .unwrap_or_default()
            }
            (Self::Decimal(l, l_width, l_scale), Self::Decimal(r, r_width, r_scale)) => {
                l == r && l_width == r_width && l_scale == r_scale
            }
            (Self::Char(l), Self::Char(r)) => l == r,
            (Self::Varchar(l), Self::Varchar(r)) => l == r,
            (Self::Blob(l), Self::Blob(r)) => l == r,
            (Self::Date(l), Self::Date(r)) => l == r,
            (Self::Time(l), Self::Time(r)) => l == r,
            (Self::Timestamp(l), Self::Timestamp(r)) => l == r,
            (Self::TimestampWithTimezone(l), Self::TimestampWithTimezone(r)) => l == r,
            (Self::Interval(l), Self::Interval(r)) => l == r,
            (Self::Uuid(l), Self::Uuid(r)) => l == r,
            (Self::Array(l, lt), Self::Array(r, rt)) | (Self::List(l, lt), Self::List(r, rt)) => {
                lt.len == rt.len && lt.element == rt.element && l == r
            }
            (Self::Map(l, lt), Self::Map(r, rt)) => {
                lt.key == rt.key && lt.value == rt.value && l == r
            }
            (Self::Json(l), Self::Json(r)) => l == r,
            (Self::Struct(l, lt), Self::Struct(r, rt)) => {
                lt.name == rt.name && l == r && lt.fields == rt.fields
            }
            (Self::Unknown(l), Self::Unknown(r)) => l == r,
            _ => false,
        }
    }
}

impl<'a> Eq for Value<'a> {}

fn hash_f32_value<H: std::hash::Hasher>(value: f32, state: &mut H) {
    let bits = if value == 0.0 {
        0.0f32.to_bits()
    } else if value.is_nan() {
        f32::NAN.to_bits()
    } else {
        value.to_bits()
    };
    bits.hash(state);
}

fn hash_f64_value<H: std::hash::Hasher>(value: f64, state: &mut H) {
    let bits = if value == 0.0 {
        0.0f64.to_bits()
    } else if value.is_nan() {
        f64::NAN.to_bits()
    } else {
        value.to_bits()
    };
    bits.hash(state);
}

fn hash_map_value<'a, H: std::hash::Hasher>(
    value: &Option<Box<HashMap<Value<'a>, Value<'a>>>>,
    key_ty: &Value<'a>,
    value_ty: &Value<'a>,
    state: &mut H,
) {
    value.is_some().hash(state);
    if let Some(map) = value {
        let mut entry_hashes = map
            .iter()
            .map(|(key, value)| {
                let mut hasher = DefaultHasher::new();
                key.hash(&mut hasher);
                value.hash(&mut hasher);
                hasher.finish()
            })
            .collect::<Vec<_>>();
        entry_hashes.sort_unstable();
        entry_hashes.hash(state);
    }
    key_ty.hash(state);
    value_ty.hash(state);
}

impl<'a> Hash for Value<'a> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        use Value::*;
        discriminant(self).hash(state);
        match self {
            Null => {}
            Boolean(v) => v.hash(state),
            Int8(v) => v.hash(state),
            Int16(v) => v.hash(state),
            Int32(v) => v.hash(state),
            Int64(v) => v.hash(state),
            Int128(v) => v.hash(state),

            UInt8(v) => v.hash(state),
            UInt16(v) => v.hash(state),
            UInt32(v) => v.hash(state),
            UInt64(v) => v.hash(state),
            UInt128(v) => v.hash(state),
            Float32(Some(v)) => hash_f32_value(*v, state),
            Float32(None) => None::<u32>.hash(state),
            Float64(Some(v)) => hash_f64_value(*v, state),
            Float64(None) => None::<u64>.hash(state),
            Decimal(v, width, scale) => {
                v.hash(state);
                width.hash(state);
                scale.hash(state);
            }
            Char(v) => v.hash(state),
            Varchar(v) => v.hash(state),
            Blob(v) => v.hash(state),
            Date(v) => v.hash(state),
            Time(v) => v.hash(state),
            Timestamp(v) => v.hash(state),
            TimestampWithTimezone(v) => v.hash(state),
            Interval(v) => v.hash(state),
            Uuid(v) => v.hash(state),
            Array(v, t) | List(v, t) => {
                v.hash(state);
                t.element.hash(state);
                t.len.hash(state);
            }
            Map(v, t) => hash_map_value(v, &t.key, &t.value, state),
            Json(v) => v.hash(state),
            Struct(v, t) => {
                if let Some(v) = v {
                    v.hash(state);
                }
                t.fields.hash(state);
                t.name.hash(state);
            }
            Unknown(v) => v.hash(state),
        }
    }
}

impl<'a> Display for Value<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut out = DynQuery::default();
        self.write_query(&GenericSqlWriter::new(), &mut Default::default(), &mut out);
        let _ = f.write_str(out.buffer());
        Ok(())
    }
}

/// Internally decoded type info for macros.
#[derive(Default)]
pub struct TypeDecoded {
    /// Representative value establishing variant & metadata.
    pub value: ValueOwned,
    /// Nullability indicator.
    pub nullable: bool,
}

impl<'a> ToTokens for Value<'a> {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        let ts = match self {
            Value::Null => quote!(::tank::Value::Null),
            Value::Boolean(..) => quote!(::tank::Value::Boolean(None)),
            Value::Int8(..) => quote!(::tank::Value::Int8(None)),
            Value::Int16(..) => quote!(::tank::Value::Int16(None)),
            Value::Int32(..) => quote!(::tank::Value::Int32(None)),
            Value::Int64(..) => quote!(::tank::Value::Int64(None)),
            Value::Int128(..) => quote!(::tank::Value::Int128(None)),
            Value::UInt8(..) => quote!(::tank::Value::UInt8(None)),
            Value::UInt16(..) => quote!(::tank::Value::UInt16(None)),
            Value::UInt32(..) => quote!(::tank::Value::UInt32(None)),
            Value::UInt64(..) => quote!(::tank::Value::UInt64(None)),
            Value::UInt128(..) => quote!(::tank::Value::UInt128(None)),
            Value::Float32(..) => quote!(::tank::Value::Float32(None)),
            Value::Float64(..) => quote!(::tank::Value::Float64(None)),
            Value::Decimal(.., width, scale) => {
                quote!(::tank::Value::Decimal(None, #width, #scale))
            }
            Value::Char(..) => quote!(::tank::Value::Char(None)),
            Value::Varchar(..) => quote!(::tank::Value::Varchar(None)),
            Value::Blob(..) => quote!(::tank::Value::Blob(None)),
            Value::Date(..) => quote!(::tank::Value::Date(None)),
            Value::Time(..) => quote!(::tank::Value::Time(None)),
            Value::Timestamp(..) => quote!(::tank::Value::Timestamp(None)),
            Value::TimestampWithTimezone(..) => quote!(::tank::Value::TimestampWithTimezone(None)),
            Value::Interval(..) => quote!(::tank::Value::Interval(None)),
            Value::Uuid(..) => quote!(::tank::Value::Uuid(None)),
            Value::Array(_, t, ..) => {
                let (element, len) = (&t.element, t.len);
                quote!(::tank::Value::Array(
                    None,
                    Box::new(::tank::CollectionType { element: #element, len: #len }),
                ))
            }
            Value::List(_, t, ..) => {
                let (element, len) = (&t.element, t.len);
                quote!(::tank::Value::List(
                    None,
                    Box::new(::tank::CollectionType { element: #element, len: #len }),
                ))
            }
            Value::Map(_, t, ..) => {
                let (key, value) = (&t.key, &t.value);
                quote!(::tank::Value::Map(
                    None,
                    Box::new(::tank::MapType { key: #key, value: #value }),
                ))
            }
            Value::Json(..) => quote!(::tank::Value::Json(None)),
            Value::Struct(.., t) => {
                let fields = t.fields.iter().map(|(k, v)| quote!((#k.into(), #v)));
                let name = &t.name;
                quote!(::tank::Value::Struct(
                    None,
                    Box::new(::tank::StructType { fields: vec![#(#fields),*], name: #name }),
                ))
            }
            Value::Unknown(..) => quote!(::tank::Value::Unknown(None)),
        };
        tokens.extend(ts);
    }
}