gel_protocol/serialization/decode/queryable/
scalars.rs1use bytes::Bytes;
2
3use crate::queryable::DescriptorMismatch;
4use crate::queryable::{Decoder, DescriptorContext, Queryable};
5
6use crate::codec;
7use crate::descriptors::TypePos;
8use crate::errors::DecodeError;
9use crate::model::{BigInt, Decimal, Json, RelativeDuration, Uuid};
10use crate::model::{ConfigMemory, DateDuration};
11use crate::model::{Datetime, Duration, LocalDate, LocalDatetime, LocalTime};
12use crate::serialization::decode::RawCodec;
13use std::time::SystemTime;
14
15pub(crate) fn check_scalar(
16 ctx: &DescriptorContext,
17 type_pos: TypePos,
18 type_id: Uuid,
19 name: &str,
20) -> Result<(), DescriptorMismatch> {
21 use crate::descriptors::Descriptor::{BaseScalar, Scalar};
22 let desc = ctx.get(type_pos)?;
23 match desc {
24 Scalar(scalar) if scalar.base_type_pos.is_some() => {
25 return check_scalar(ctx, scalar.base_type_pos.unwrap(), type_id, name);
26 }
27 Scalar(scalar) if *scalar.id == type_id => {
28 return Ok(());
29 }
30 BaseScalar(base) if *base.id == type_id => {
31 return Ok(());
32 }
33 _ => {}
34 }
35 Err(ctx.wrong_type(desc, name))
36}
37
38pub trait DecodeScalar: for<'a> RawCodec<'a> + Sized {
39 fn uuid() -> Uuid;
40 fn typename() -> &'static str;
41}
42
43impl<T: DecodeScalar> Queryable for T {
44 type Args = ();
45
46 fn decode(_decoder: &Decoder, _args: &(), buf: &[u8]) -> Result<Self, DecodeError> {
47 RawCodec::decode(buf)
48 }
49 fn check_descriptor(
50 ctx: &DescriptorContext,
51 type_pos: TypePos,
52 ) -> Result<(), DescriptorMismatch> {
53 check_scalar(ctx, type_pos, T::uuid(), T::typename())
54 }
55}
56
57impl DecodeScalar for String {
58 fn uuid() -> Uuid {
59 codec::STD_STR
60 }
61 fn typename() -> &'static str {
62 "std::str"
63 }
64}
65
66impl DecodeScalar for Bytes {
67 fn uuid() -> Uuid {
68 codec::STD_BYTES
69 }
70 fn typename() -> &'static str {
71 "std::bytes"
72 }
73}
74
75impl DecodeScalar for Json {
76 fn uuid() -> Uuid {
77 codec::STD_JSON
78 }
79 fn typename() -> &'static str {
80 "std::json"
81 }
82}
83
84impl DecodeScalar for i16 {
92 fn uuid() -> Uuid {
93 codec::STD_INT16
94 }
95 fn typename() -> &'static str {
96 "std::int16"
97 }
98}
99
100impl DecodeScalar for i32 {
101 fn uuid() -> Uuid {
102 codec::STD_INT32
103 }
104 fn typename() -> &'static str {
105 "std::int32"
106 }
107}
108
109impl DecodeScalar for i64 {
110 fn uuid() -> Uuid {
111 codec::STD_INT64
112 }
113 fn typename() -> &'static str {
114 "std::int64"
115 }
116}
117
118impl DecodeScalar for f32 {
119 fn uuid() -> Uuid {
120 codec::STD_FLOAT32
121 }
122 fn typename() -> &'static str {
123 "std::float32"
124 }
125}
126
127impl DecodeScalar for f64 {
128 fn uuid() -> Uuid {
129 codec::STD_FLOAT64
130 }
131 fn typename() -> &'static str {
132 "std::float64"
133 }
134}
135
136impl DecodeScalar for Uuid {
137 fn uuid() -> Uuid {
138 codec::STD_UUID
139 }
140 fn typename() -> &'static str {
141 "std::uuid"
142 }
143}
144
145impl DecodeScalar for bool {
146 fn uuid() -> Uuid {
147 codec::STD_BOOL
148 }
149 fn typename() -> &'static str {
150 "std::bool"
151 }
152}
153
154impl DecodeScalar for BigInt {
155 fn uuid() -> Uuid {
156 codec::STD_BIGINT
157 }
158 fn typename() -> &'static str {
159 "std::bigint"
160 }
161}
162
163#[cfg(feature = "num-bigint")]
164impl DecodeScalar for num_bigint::BigInt {
165 fn uuid() -> Uuid {
166 codec::STD_BIGINT
167 }
168 fn typename() -> &'static str {
169 "std::bigint"
170 }
171}
172
173impl DecodeScalar for Decimal {
174 fn uuid() -> Uuid {
175 codec::STD_DECIMAL
176 }
177 fn typename() -> &'static str {
178 "std::decimal"
179 }
180}
181
182#[cfg(feature = "bigdecimal")]
183impl DecodeScalar for bigdecimal::BigDecimal {
184 fn uuid() -> Uuid {
185 codec::STD_DECIMAL
186 }
187 fn typename() -> &'static str {
188 "std::decimal"
189 }
190}
191
192impl DecodeScalar for LocalDatetime {
193 fn uuid() -> Uuid {
194 codec::CAL_LOCAL_DATETIME
195 }
196 fn typename() -> &'static str {
197 "cal::local_datetime"
198 }
199}
200
201#[cfg(feature = "chrono")]
202impl DecodeScalar for chrono::NaiveDateTime {
203 fn uuid() -> Uuid {
204 codec::CAL_LOCAL_DATETIME
205 }
206 fn typename() -> &'static str {
207 "cal::local_datetime"
208 }
209}
210
211impl DecodeScalar for LocalDate {
212 fn uuid() -> Uuid {
213 codec::CAL_LOCAL_DATE
214 }
215 fn typename() -> &'static str {
216 "cal::local_date"
217 }
218}
219
220#[cfg(feature = "chrono")]
221impl DecodeScalar for chrono::NaiveDate {
222 fn uuid() -> Uuid {
223 codec::CAL_LOCAL_DATE
224 }
225 fn typename() -> &'static str {
226 "cal::local_date"
227 }
228}
229
230impl DecodeScalar for LocalTime {
231 fn uuid() -> Uuid {
232 codec::CAL_LOCAL_TIME
233 }
234 fn typename() -> &'static str {
235 "cal::local_time"
236 }
237}
238
239#[cfg(feature = "chrono")]
240impl DecodeScalar for chrono::NaiveTime {
241 fn uuid() -> Uuid {
242 codec::CAL_LOCAL_TIME
243 }
244 fn typename() -> &'static str {
245 "cal::local_time"
246 }
247}
248
249impl DecodeScalar for Duration {
250 fn uuid() -> Uuid {
251 codec::STD_DURATION
252 }
253 fn typename() -> &'static str {
254 "std::duration"
255 }
256}
257
258impl DecodeScalar for RelativeDuration {
259 fn uuid() -> Uuid {
260 codec::CAL_RELATIVE_DURATION
261 }
262 fn typename() -> &'static str {
263 "cal::relative_duration"
264 }
265}
266
267impl DecodeScalar for SystemTime {
268 fn uuid() -> Uuid {
269 codec::STD_DATETIME
270 }
271 fn typename() -> &'static str {
272 "std::datetime"
273 }
274}
275
276impl DecodeScalar for Datetime {
277 fn uuid() -> Uuid {
278 codec::STD_DATETIME
279 }
280 fn typename() -> &'static str {
281 "std::datetime"
282 }
283}
284
285#[cfg(feature = "chrono")]
286impl DecodeScalar for chrono::DateTime<chrono::Utc> {
287 fn uuid() -> Uuid {
288 codec::STD_DATETIME
289 }
290 fn typename() -> &'static str {
291 "std::datetime"
292 }
293}
294
295impl DecodeScalar for ConfigMemory {
296 fn uuid() -> Uuid {
297 codec::CFG_MEMORY
298 }
299 fn typename() -> &'static str {
300 "cfg::memory"
301 }
302}
303
304impl DecodeScalar for DateDuration {
305 fn uuid() -> Uuid {
306 codec::CAL_DATE_DURATION
307 }
308 fn typename() -> &'static str {
309 "cal::date_duration"
310 }
311}