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
use crate::queryable::{Queryable, DescriptorContext, Decoder};
use crate::queryable::{DescriptorMismatch};

use crate::errors::DecodeError;
use crate::codec;
use crate::descriptors::TypePos;
use crate::model::{Duration, LocalDate, LocalTime, LocalDatetime, Datetime};
use crate::model::{Json, Uuid, BigInt, Decimal, RelativeDuration};
use crate::serialization::decode::RawCodec;
use std::time::SystemTime;


fn check_scalar(ctx: &DescriptorContext, type_pos: TypePos, type_id: Uuid, name: &str) -> Result<(), DescriptorMismatch> {
    use crate::descriptors::Descriptor::{Scalar, BaseScalar};
    let desc = ctx.get(type_pos)?;
    match desc {
        Scalar(scalar) => {
            return check_scalar(ctx, scalar.base_type_pos, type_id, name);
        }
        BaseScalar(base) if base.id == type_id => {
            return Ok(());
        }
        _ => {}
    }
    Err(ctx.wrong_type(desc, name))
}

pub trait DecodeScalar: for<'a> RawCodec<'a> + Sized {
    fn uuid() -> Uuid;
    fn typename() -> &'static str;
}

impl<T: DecodeScalar> Queryable for T {
    fn decode(_decoder: &Decoder, buf: &[u8]) -> Result<Self, DecodeError> {
        RawCodec::decode(buf)
    }
    fn check_descriptor(ctx: &DescriptorContext, type_pos: TypePos)
        -> Result<(), DescriptorMismatch>
    {
        check_scalar(ctx, type_pos, T::uuid(), T::typename())
    }
}

impl DecodeScalar for String {
    fn uuid() -> Uuid { codec::STD_STR }
    fn typename() -> &'static str { "std::str" }
}

impl DecodeScalar for Json {
    fn uuid() -> Uuid { codec::STD_JSON }
    fn typename() -> &'static str { "std::json" }
}

/*
impl DecodeScalar for Vec<u8> {
    fn uuid() -> Uuid { codec::STD_BYTES }
    fn typename() -> &'static str { "std::bytes" }
}
*/

impl DecodeScalar for i16 {
    fn uuid() -> Uuid { codec::STD_INT16 }
    fn typename() -> &'static str { "std::int16" }
}

impl DecodeScalar for i32 {
    fn uuid() -> Uuid { codec::STD_INT32 }
    fn typename() -> &'static str { "std::int32" }
}

impl DecodeScalar for i64 {
    fn uuid() -> Uuid { codec::STD_INT64 }
    fn typename() -> &'static str { "std::int64" }
}

impl DecodeScalar for f32 {
    fn uuid() -> Uuid { codec::STD_FLOAT32 }
    fn typename() -> &'static str { "std::int32" }
}

impl DecodeScalar for f64 {
    fn uuid() -> Uuid { codec::STD_FLOAT64 }
    fn typename() -> &'static str { "std::int64" }
}

impl DecodeScalar for Uuid {
    fn uuid() -> Uuid { codec::STD_UUID }
    fn typename() -> &'static str { "std::uuid" }
}

impl DecodeScalar for bool {
    fn uuid() -> Uuid { codec::STD_BOOL }
    fn typename() -> &'static str { "std::bool" }
}

impl DecodeScalar for BigInt {
    fn uuid() -> Uuid { codec::STD_BIGINT }
    fn typename() -> &'static str { "std::bigint" }
}

#[cfg(feature="num-bigint")]
impl DecodeScalar for num_bigint::BigInt {
    fn uuid() -> Uuid { codec::STD_BIGINT }
    fn typename() -> &'static str { "std::bigint" }
}

impl DecodeScalar for Decimal {
    fn uuid() -> Uuid { codec::STD_DECIMAL }
    fn typename() -> &'static str { "std::decimal" }
}

#[cfg(feature="bigdecimal")]
impl DecodeScalar for bigdecimal::BigDecimal {
    fn uuid() -> Uuid { codec::STD_DECIMAL }
    fn typename() -> &'static str { "std::decimal" }
}

impl DecodeScalar for LocalDatetime {
    fn uuid() -> Uuid { codec::CAL_LOCAL_DATETIME }
    fn typename() -> &'static str { "cal::local_datetime" }
}

impl DecodeScalar for LocalDate {
    fn uuid() -> Uuid { codec::CAL_LOCAL_DATE }
    fn typename() -> &'static str { "cal::local_date" }
}

impl DecodeScalar for LocalTime {
    fn uuid() -> Uuid { codec::CAL_LOCAL_TIME }
    fn typename() -> &'static str { "cal::local_time" }
}

impl DecodeScalar for Duration {
    fn uuid() -> Uuid { codec::STD_DURATION }
    fn typename() -> &'static str { "std::duration" }
}

impl DecodeScalar for RelativeDuration {
    fn uuid() -> Uuid { codec::CAL_RELATIVE_DURATION }
    fn typename() -> &'static str { "cal::relative_duration" }
}

impl DecodeScalar for SystemTime {
    fn uuid() -> Uuid { codec::STD_DATETIME }
    fn typename() -> &'static str { "std::datetime" }
}

impl DecodeScalar for Datetime {
    fn uuid() -> Uuid { codec::STD_DATETIME }
    fn typename() -> &'static str { "std::datetime" }
}