• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

getdozer / dozer / 4195006582

pending completion
4195006582

Pull #918

github

GitHub
Merge 14ceac9a6 into dc166625f
Pull Request #918: ObjectStore - Reduce code duplication & more clone prevention

177 of 177 new or added lines in 5 files covered. (100.0%)

24368 of 37519 relevant lines covered (64.95%)

43913.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.74
/dozer-types/src/types/field.rs
1
use crate::errors::types::{DeserializationError, TypeError};
2
use chrono::{DateTime, FixedOffset, LocalResult, NaiveDate, TimeZone, Utc};
3
use ordered_float::OrderedFloat;
4
use rust_decimal::prelude::{FromPrimitive, ToPrimitive};
5
use rust_decimal::Decimal;
6
use serde::{self, Deserialize, Serialize};
7
use std::borrow::Cow;
8
use std::fmt::{Display, Formatter};
9

10
pub const DATE_FORMAT: &str = "%Y-%m-%d";
11
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Ord)]
28,662,477✔
12
pub enum Field {
13
    UInt(u64),
14
    Int(i64),
15
    Float(OrderedFloat<f64>),
16
    Boolean(bool),
17
    String(String),
18
    Text(String),
19
    Binary(Vec<u8>),
20
    Decimal(Decimal),
21
    Timestamp(DateTime<FixedOffset>),
22
    Date(NaiveDate),
23
    Bson(Vec<u8>),
24
    Null,
25
}
26

27
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Ord)]
2,208,845✔
28
pub enum FieldBorrow<'a> {
29
    UInt(u64),
30
    Int(i64),
31
    Float(OrderedFloat<f64>),
32
    Boolean(bool),
33
    String(&'a str),
34
    Text(&'a str),
35
    Binary(&'a [u8]),
36
    Decimal(Decimal),
37
    Timestamp(DateTime<FixedOffset>),
38
    Date(NaiveDate),
39
    Bson(&'a [u8]),
40
    Null,
41
}
42

43
impl Field {
44
    fn data_encoding_len(&self) -> usize {
7,662,160✔
45
        match self {
7,662,160✔
46
            Field::UInt(_) => 8,
219,640✔
47
            Field::Int(_) => 8,
1,257,100✔
48
            Field::Float(_) => 8,
123,750✔
49
            Field::Boolean(_) => 1,
4,450✔
50
            Field::String(s) => s.len(),
5,966,960✔
51
            Field::Text(s) => s.len(),
4,460✔
52
            Field::Binary(b) => b.len(),
4,460✔
53
            Field::Decimal(_) => 16,
5,200✔
54
            Field::Timestamp(_) => 8,
4,680✔
55
            Field::Date(_) => 10,
4,680✔
56
            Field::Bson(b) => b.len(),
2,235✔
57
            Field::Null => 0,
64,545✔
58
        }
59
    }
7,662,160✔
60

61
    fn encode_data(&self) -> Cow<[u8]> {
7,527,078✔
62
        match self {
7,527,078✔
63
            Field::UInt(i) => Cow::Owned(i.to_be_bytes().into()),
146,368✔
64
            Field::Int(i) => Cow::Owned(i.to_be_bytes().into()),
1,252,448✔
65
            Field::Float(f) => Cow::Owned(f.to_be_bytes().into()),
99,048✔
66
            Field::Boolean(b) => Cow::Owned(if *b { [1] } else { [0] }.into()),
2,238✔
67
            Field::String(s) => Cow::Borrowed(s.as_bytes()),
5,964,858✔
68
            Field::Text(s) => Cow::Borrowed(s.as_bytes()),
2,238✔
69
            Field::Binary(b) => Cow::Borrowed(b.as_slice()),
2,238✔
70
            Field::Decimal(d) => Cow::Owned(d.serialize().into()),
2,978✔
71
            Field::Timestamp(t) => Cow::Owned(t.timestamp_millis().to_be_bytes().into()),
2,458✔
72
            Field::Date(t) => Cow::Owned(t.to_string().into()),
2,458✔
73
            Field::Bson(b) => Cow::Borrowed(b),
1,124✔
74
            Field::Null => Cow::Owned([].into()),
48,634✔
75
        }
76
    }
7,527,088✔
77

78
    pub fn encode_buf(&self, destination: &mut [u8]) {
7,530,876✔
79
        let prefix = self.get_type_prefix();
7,530,876✔
80
        let data = self.encode_data();
7,530,876✔
81
        destination[0] = prefix;
7,530,876✔
82
        destination[1..].copy_from_slice(&data);
7,530,876✔
83
    }
7,530,876✔
84

85
    pub fn encoding_len(&self) -> usize {
7,660,528✔
86
        self.data_encoding_len() + 1
7,660,528✔
87
    }
7,660,528✔
88

89
    pub fn encode(&self) -> Vec<u8> {
7,392,296✔
90
        let mut result = vec![0; self.encoding_len()];
7,392,296✔
91
        self.encode_buf(&mut result);
7,392,296✔
92
        result
7,392,296✔
93
    }
7,392,296✔
94

95
    pub fn borrow(&self) -> FieldBorrow {
9,922✔
96
        match self {
9,922✔
97
            Field::UInt(i) => FieldBorrow::UInt(*i),
902✔
98
            Field::Int(i) => FieldBorrow::Int(*i),
902✔
99
            Field::Float(f) => FieldBorrow::Float(*f),
902✔
100
            Field::Boolean(b) => FieldBorrow::Boolean(*b),
902✔
101
            Field::String(s) => FieldBorrow::String(s),
902✔
102
            Field::Text(s) => FieldBorrow::Text(s),
902✔
103
            Field::Binary(b) => FieldBorrow::Binary(b),
902✔
104
            Field::Decimal(d) => FieldBorrow::Decimal(*d),
902✔
105
            Field::Timestamp(t) => FieldBorrow::Timestamp(*t),
902✔
106
            Field::Date(t) => FieldBorrow::Date(*t),
902✔
107
            Field::Bson(b) => FieldBorrow::Bson(b),
451✔
108
            Field::Null => FieldBorrow::Null,
451✔
109
        }
110
    }
9,922✔
111

112
    pub fn decode(buf: &[u8]) -> Result<Field, DeserializationError> {
230,602✔
113
        Self::decode_borrow(buf).map(|field| field.to_owned())
230,602✔
114
    }
230,602✔
115

116
    pub fn decode_borrow(buf: &[u8]) -> Result<FieldBorrow, DeserializationError> {
9,580,512✔
117
        let first_byte = *buf.first().ok_or(DeserializationError::EmptyInput)?;
9,580,512✔
118
        let val = &buf[1..];
9,580,512✔
119
        match first_byte {
9,580,512✔
120
            0 => Ok(FieldBorrow::UInt(u64::from_be_bytes(
121
                val.try_into()
5,999,082✔
122
                    .map_err(|_| DeserializationError::BadDataLength)?,
5,999,082✔
123
            ))),
124
            1 => Ok(FieldBorrow::Int(i64::from_be_bytes(
125
                val.try_into()
123,972✔
126
                    .map_err(|_| DeserializationError::BadDataLength)?,
123,972✔
127
            ))),
128
            2 => Ok(FieldBorrow::Float(OrderedFloat(f64::from_be_bytes(
129
                val.try_into()
1,042,882✔
130
                    .map_err(|_| DeserializationError::BadDataLength)?,
1,042,882✔
131
            )))),
132
            3 => Ok(FieldBorrow::Boolean(val[0] == 1)),
902✔
133
            4 => Ok(FieldBorrow::String(std::str::from_utf8(val)?)),
110,002✔
134
            5 => Ok(FieldBorrow::Text(std::str::from_utf8(val)?)),
902✔
135
            6 => Ok(FieldBorrow::Binary(val)),
902✔
136
            7 => Ok(FieldBorrow::Decimal(Decimal::deserialize(
137
                val.try_into()
1,522✔
138
                    .map_err(|_| DeserializationError::BadDataLength)?,
1,522✔
139
            ))),
140
            8 => {
141
                let timestamp = Utc.timestamp_millis_opt(i64::from_be_bytes(
1,122✔
142
                    val.try_into()
1,122✔
143
                        .map_err(|_| DeserializationError::BadDataLength)?,
1,122✔
144
                ));
145

146
                match timestamp {
1,122✔
147
                    LocalResult::Single(v) => Ok(FieldBorrow::Timestamp(DateTime::from(v))),
1,122✔
148
                    LocalResult::Ambiguous(_, _) => Err(DeserializationError::Custom(Box::new(
×
149
                        TypeError::AmbiguousTimestamp,
×
150
                    ))),
×
151
                    LocalResult::None => Err(DeserializationError::Custom(Box::new(
×
152
                        TypeError::InvalidTimestamp,
×
153
                    ))),
×
154
                }
155
            }
156
            9 => Ok(FieldBorrow::Date(NaiveDate::parse_from_str(
157
                std::str::from_utf8(val)?,
1,122✔
158
                DATE_FORMAT,
1,122✔
159
            )?)),
×
160
            10 => Ok(FieldBorrow::Bson(val)),
451✔
161
            11 => Ok(FieldBorrow::Null),
2,297,651✔
162
            other => Err(DeserializationError::UnrecognisedFieldType(other)),
×
163
        }
164
    }
9,580,922✔
165

166
    fn get_type_prefix(&self) -> u8 {
7,529,096✔
167
        match self {
7,529,096✔
168
            Field::UInt(_) => 0,
148,426✔
169
            Field::Int(_) => 1,
1,252,446✔
170
            Field::Float(_) => 2,
99,046✔
171
            Field::Boolean(_) => 3,
2,226✔
172
            Field::String(_) => 4,
5,964,836✔
173
            Field::Text(_) => 5,
2,236✔
174
            Field::Binary(_) => 6,
2,236✔
175
            Field::Decimal(_) => 7,
2,976✔
176
            Field::Timestamp(_) => 8,
2,456✔
177
            Field::Date(_) => 9,
2,456✔
178
            Field::Bson(_) => 10,
1,123✔
179
            Field::Null => 11,
48,633✔
180
        }
181
    }
7,529,096✔
182

183
    pub fn as_uint(&self) -> Option<u64> {
10,447,112✔
184
        match self {
10,447,112✔
185
            Field::UInt(i) => Some(*i),
9,212,171✔
186
            _ => None,
1,234,941✔
187
        }
188
    }
10,447,112✔
189

190
    pub fn as_int(&self) -> Option<i64> {
462✔
191
        match self {
462✔
192
            Field::Int(i) => Some(*i),
451✔
193
            _ => None,
11✔
194
        }
195
    }
462✔
196

197
    pub fn as_float(&self) -> Option<f64> {
2,489,872✔
198
        match self {
2,489,872✔
199
            Field::Float(f) => Some(f.0),
2,489,861✔
200
            _ => None,
11✔
201
        }
202
    }
2,489,872✔
203

204
    pub fn as_boolean(&self) -> Option<bool> {
12✔
205
        match self {
12✔
206
            Field::Boolean(b) => Some(*b),
1✔
207
            _ => None,
11✔
208
        }
209
    }
12✔
210

211
    pub fn as_string(&self) -> Option<&str> {
4,989,972✔
212
        match self {
4,989,972✔
213
            Field::String(s) => Some(s),
4,989,961✔
214
            _ => None,
11✔
215
        }
216
    }
4,989,972✔
217

218
    pub fn as_text(&self) -> Option<&str> {
42✔
219
        match self {
42✔
220
            Field::Text(s) => Some(s),
31✔
221
            _ => None,
11✔
222
        }
223
    }
42✔
224

225
    pub fn as_binary(&self) -> Option<&[u8]> {
12✔
226
        match self {
12✔
227
            Field::Binary(b) => Some(b),
1✔
228
            _ => None,
11✔
229
        }
230
    }
12✔
231

232
    pub fn as_decimal(&self) -> Option<Decimal> {
12✔
233
        match self {
12✔
234
            Field::Decimal(d) => Some(*d),
1✔
235
            _ => None,
11✔
236
        }
237
    }
12✔
238

239
    pub fn as_timestamp(&self) -> Option<DateTime<FixedOffset>> {
1,244,942✔
240
        match self {
1,244,942✔
241
            Field::Timestamp(t) => Some(*t),
1,244,931✔
242
            _ => None,
11✔
243
        }
244
    }
1,244,942✔
245

246
    pub fn as_date(&self) -> Option<NaiveDate> {
12✔
247
        match self {
12✔
248
            Field::Date(d) => Some(*d),
1✔
249
            _ => None,
11✔
250
        }
251
    }
12✔
252

253
    pub fn as_bson(&self) -> Option<&[u8]> {
12✔
254
        match self {
12✔
255
            Field::Bson(b) => Some(b),
1✔
256
            _ => None,
11✔
257
        }
258
    }
12✔
259

260
    pub fn as_null(&self) -> Option<()> {
12✔
261
        match self {
12✔
262
            Field::Null => Some(()),
1✔
263
            _ => None,
11✔
264
        }
265
    }
12✔
266

267
    pub fn to_uint(&self) -> Option<u64> {
412✔
268
        match self {
412✔
269
            Field::UInt(i) => Some(*i),
401✔
270
            Field::Int(i) => u64::from_i64(*i),
1✔
271
            Field::String(s) => s.parse::<u64>().ok(),
1✔
272
            Field::Null => Some(0_u64),
1✔
273
            _ => None,
8✔
274
        }
275
    }
412✔
276

277
    pub fn to_int(&self) -> Option<i64> {
492,592✔
278
        match self {
492,592✔
279
            Field::Int(i) => Some(*i),
492,401✔
280
            Field::UInt(u) => i64::from_u64(*u),
11✔
281
            Field::String(s) => s.parse::<i64>().ok(),
11✔
282
            Field::Null => Some(0_i64),
161✔
283
            _ => None,
8✔
284
        }
285
    }
492,592✔
286

287
    pub fn to_float(&self) -> Option<f64> {
702✔
288
        match self {
702✔
289
            Field::Float(f) => Some(f.0),
491✔
290
            Field::Decimal(d) => d.to_f64(),
11✔
291
            Field::UInt(u) => f64::from_u64(*u),
11✔
292
            Field::Int(i) => f64::from_i64(*i),
11✔
293
            Field::Null => Some(0_f64),
161✔
294
            Field::String(s) => s.parse::<f64>().ok(),
11✔
295
            _ => None,
6✔
296
        }
297
    }
702✔
298

299
    pub fn to_boolean(&self) -> Option<bool> {
72✔
300
        match self {
72✔
301
            Field::Boolean(b) => Some(*b),
11✔
302
            Field::Null => Some(false),
1✔
303
            Field::Int(i) => Some(*i > 0_i64),
11✔
304
            Field::UInt(i) => Some(*i > 0_u64),
11✔
305
            Field::Float(i) => Some(i.0 > 0_f64),
11✔
306
            Field::Decimal(i) => Some(i.gt(&Decimal::from(0_u64))),
21✔
307
            _ => None,
6✔
308
        }
309
    }
72✔
310

311
    pub fn to_string(&self) -> Option<String> {
782✔
312
        match self {
782✔
313
            Field::String(s) => Some(s.to_owned()),
581✔
314
            Field::Text(t) => Some(t.to_owned()),
51✔
315
            Field::Int(i) => Some(format!("{i}")),
71✔
316
            Field::UInt(i) => Some(format!("{i}")),
11✔
317
            Field::Float(i) => Some(format!("{i}")),
11✔
318
            Field::Decimal(i) => Some(format!("{i}")),
11✔
319
            Field::Boolean(i) => Some(if *i {
11✔
320
                "TRUE".to_string()
11✔
321
            } else {
322
                "FALSE".to_string()
×
323
            }),
324
            Field::Date(d) => Some(d.format("%Y-%m-%d").to_string()),
11✔
325
            Field::Timestamp(t) => Some(t.to_rfc3339()),
11✔
326
            Field::Binary(b) => Some(format!("{b:X?}")),
1✔
327
            Field::Null => Some("".to_string()),
11✔
328
            _ => None,
1✔
329
        }
330
    }
782✔
331

332
    pub fn to_text(&self) -> Option<String> {
102✔
333
        match self {
102✔
334
            Field::String(s) => Some(s.to_owned()),
11✔
335
            Field::Text(t) => Some(t.to_owned()),
11✔
336
            Field::Int(i) => Some(format!("{i}")),
11✔
337
            Field::UInt(i) => Some(format!("{i}")),
11✔
338
            Field::Float(i) => Some(format!("{i}")),
11✔
339
            Field::Decimal(i) => Some(format!("{i}")),
11✔
340
            Field::Boolean(i) => Some(if *i {
11✔
341
                "TRUE".to_string()
11✔
342
            } else {
343
                "FALSE".to_string()
×
344
            }),
345
            Field::Date(d) => Some(d.format("%Y-%m-%d").to_string()),
11✔
346
            Field::Timestamp(t) => Some(t.to_rfc3339()),
11✔
347
            Field::Binary(b) => Some(format!("{b:X?}")),
1✔
348
            Field::Null => Some("".to_string()),
1✔
349
            _ => None,
1✔
350
        }
351
    }
102✔
352

353
    pub fn to_binary(&self) -> Option<&[u8]> {
12✔
354
        match self {
12✔
355
            Field::Binary(b) => Some(b),
1✔
356
            _ => None,
11✔
357
        }
358
    }
12✔
359

360
    pub fn to_decimal(&self) -> Option<Decimal> {
652✔
361
        match self {
652✔
362
            Field::Decimal(d) => Some(*d),
481✔
363
            Field::Float(f) => Decimal::from_f64_retain(f.0),
1✔
364
            Field::Int(i) => Decimal::from_i64(*i),
1✔
365
            Field::UInt(u) => Decimal::from_u64(*u),
1✔
366
            Field::Null => Some(Decimal::from(0)),
161✔
367
            Field::String(s) => Decimal::from_str_exact(s).ok(),
1✔
368
            _ => None,
6✔
369
        }
370
    }
652✔
371

372
    pub fn to_timestamp(&self) -> Result<Option<DateTime<FixedOffset>>, TypeError> {
332✔
373
        match self {
332✔
374
            Field::Timestamp(t) => Ok(Some(*t)),
241✔
375
            Field::String(s) => Ok(DateTime::parse_from_rfc3339(s.as_str()).ok()),
1✔
376
            Field::Null => match Utc.timestamp_millis_opt(0) {
81✔
377
                LocalResult::None => Err(TypeError::InvalidTimestamp),
×
378
                LocalResult::Single(v) => Ok(Some(DateTime::from(v))),
81✔
379
                LocalResult::Ambiguous(_, _) => Err(TypeError::AmbiguousTimestamp),
×
380
            },
381
            _ => Ok(None),
9✔
382
        }
383
    }
332✔
384

385
    pub fn to_date(&self) -> Result<Option<NaiveDate>, TypeError> {
332✔
386
        match self {
332✔
387
            Field::Date(d) => Ok(Some(*d)),
241✔
388
            Field::String(s) => Ok(NaiveDate::parse_from_str(s, "%Y-%m-%d").ok()),
1✔
389
            Field::Null => match Utc.timestamp_millis_opt(0) {
81✔
390
                LocalResult::None => Err(TypeError::InvalidTimestamp),
×
391
                LocalResult::Single(v) => Ok(Some(v.naive_utc().date())),
81✔
392
                LocalResult::Ambiguous(_, _) => Err(TypeError::AmbiguousTimestamp),
×
393
            },
394
            _ => Ok(None),
9✔
395
        }
396
    }
332✔
397

398
    pub fn to_bson(&self) -> Option<&[u8]> {
12✔
399
        match self {
12✔
400
            Field::Bson(b) => Some(b),
1✔
401
            _ => None,
11✔
402
        }
403
    }
12✔
404

405
    pub fn to_null(&self) -> Option<()> {
12✔
406
        match self {
12✔
407
            Field::Null => Some(()),
1✔
408
            _ => None,
11✔
409
        }
410
    }
12✔
411
}
412

413
impl Display for Field {
414
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
×
415
        match self {
×
416
            Field::UInt(v) => f.write_str(&format!("{v} (unsigned int)")),
×
417
            Field::Int(v) => f.write_str(&format!("{v} (signed int)")),
×
418
            Field::Float(v) => f.write_str(&format!("{v} (Float)")),
×
419
            Field::Boolean(v) => f.write_str(&format!("{v}")),
×
420
            Field::String(v) => f.write_str(&v.to_string()),
×
421
            Field::Text(v) => f.write_str(&v.to_string()),
×
422
            Field::Binary(v) => f.write_str(&format!("{v:x?}")),
×
423
            Field::Decimal(v) => f.write_str(&format!("{v} (Decimal)")),
×
424
            Field::Timestamp(v) => f.write_str(&format!("{v}")),
×
425
            Field::Date(v) => f.write_str(&format!("{v}")),
×
426
            Field::Bson(v) => f.write_str(&format!("{v:x?}")),
×
427
            Field::Null => f.write_str("NULL"),
×
428
        }
429
    }
×
430
}
431

432
impl<'a> FieldBorrow<'a> {
433
    pub fn to_owned(self) -> Field {
230,624✔
434
        match self {
230,624✔
435
            FieldBorrow::Int(i) => Field::Int(i),
120,014✔
436
            FieldBorrow::UInt(i) => Field::UInt(i),
214✔
437
            FieldBorrow::Float(f) => Field::Float(f),
444✔
438
            FieldBorrow::Boolean(b) => Field::Boolean(b),
4✔
439
            FieldBorrow::String(s) => Field::String(s.to_owned()),
108,864✔
440
            FieldBorrow::Text(s) => Field::Text(s.to_owned()),
4✔
441
            FieldBorrow::Binary(b) => Field::Binary(b.to_owned()),
4✔
442
            FieldBorrow::Decimal(d) => Field::Decimal(d),
624✔
443
            FieldBorrow::Timestamp(t) => Field::Timestamp(t),
224✔
444
            FieldBorrow::Date(d) => Field::Date(d),
224✔
445
            FieldBorrow::Bson(b) => Field::Bson(b.to_owned()),
2✔
446
            FieldBorrow::Null => Field::Null,
2✔
447
        }
448
    }
230,624✔
449
}
450

451
#[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq)]
841,292✔
452
pub enum FieldType {
453
    UInt,
454
    Int,
455
    Float,
456
    Boolean,
457
    String,
458
    Text,
459
    Binary,
460
    Decimal,
461
    Timestamp,
462
    Date,
463
    Bson,
464
}
465

466
impl Display for FieldType {
467
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
×
468
        match self {
×
469
            FieldType::UInt => f.write_str("unsigned int"),
×
470
            FieldType::Int => f.write_str("int"),
×
471
            FieldType::Float => f.write_str("float"),
×
472
            FieldType::Boolean => f.write_str("boolean"),
×
473
            FieldType::String => f.write_str("string"),
×
474
            FieldType::Text => f.write_str("text"),
×
475
            FieldType::Binary => f.write_str("binary"),
×
476
            FieldType::Decimal => f.write_str("decimal"),
×
477
            FieldType::Timestamp => f.write_str("timestamp"),
×
478
            FieldType::Date => f.write_str("date"),
×
479
            FieldType::Bson => f.write_str("bson"),
×
480
        }
481
    }
×
482
}
483

484
/// Can't put it in `tests` module because of <https://github.com/rust-lang/cargo/issues/8379>
485
/// and we need this function in `dozer-cache`.
486
pub fn field_test_cases() -> impl Iterator<Item = Field> {
476✔
487
    [
476✔
488
        Field::Int(0_i64),
476✔
489
        Field::Int(1_i64),
476✔
490
        Field::UInt(0_u64),
476✔
491
        Field::UInt(1_u64),
476✔
492
        Field::Float(OrderedFloat::from(0_f64)),
476✔
493
        Field::Float(OrderedFloat::from(1_f64)),
476✔
494
        Field::Boolean(true),
476✔
495
        Field::Boolean(false),
476✔
496
        Field::String("".to_string()),
476✔
497
        Field::String("1".to_string()),
476✔
498
        Field::Text("".to_string()),
476✔
499
        Field::Text("1".to_string()),
476✔
500
        Field::Binary(vec![]),
476✔
501
        Field::Binary(vec![1]),
476✔
502
        Field::Decimal(Decimal::new(0, 0)),
476✔
503
        Field::Decimal(Decimal::new(1, 0)),
476✔
504
        Field::Timestamp(DateTime::from(Utc.timestamp_millis_opt(0).unwrap())),
476✔
505
        Field::Timestamp(DateTime::parse_from_rfc3339("2020-01-01T00:00:00Z").unwrap()),
476✔
506
        Field::Date(NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()),
476✔
507
        Field::Date(NaiveDate::from_ymd_opt(2020, 1, 1).unwrap()),
476✔
508
        Field::Bson(vec![
476✔
509
            // BSON representation of `{"abc":"foo"}`
476✔
510
            123, 34, 97, 98, 99, 34, 58, 34, 102, 111, 111, 34, 125,
476✔
511
        ]),
476✔
512
        Field::Null,
476✔
513
    ]
476✔
514
    .into_iter()
476✔
515
}
476✔
516

517
#[cfg(test)]
518
pub mod tests {
519
    use super::*;
520

521
    #[test]
1✔
522
    fn data_encoding_len_must_agree_with_encode() {
1✔
523
        for field in field_test_cases() {
23✔
524
            let bytes = field.encode_data();
22✔
525
            assert_eq!(bytes.len(), field.data_encoding_len());
22✔
526
        }
527
    }
1✔
528

529
    #[test]
1✔
530
    fn test_as_conversion() {
1✔
531
        let field = Field::UInt(1);
1✔
532
        assert!(field.as_uint().is_some());
1✔
533
        assert!(field.as_int().is_none());
1✔
534
        assert!(field.as_float().is_none());
1✔
535
        assert!(field.as_boolean().is_none());
1✔
536
        assert!(field.as_string().is_none());
1✔
537
        assert!(field.as_text().is_none());
1✔
538
        assert!(field.as_binary().is_none());
1✔
539
        assert!(field.as_decimal().is_none());
1✔
540
        assert!(field.as_timestamp().is_none());
1✔
541
        assert!(field.as_date().is_none());
1✔
542
        assert!(field.as_bson().is_none());
1✔
543
        assert!(field.as_null().is_none());
1✔
544

545
        let field = Field::Int(1);
1✔
546
        assert!(field.as_uint().is_none());
1✔
547
        assert!(field.as_int().is_some());
1✔
548
        assert!(field.as_float().is_none());
1✔
549
        assert!(field.as_boolean().is_none());
1✔
550
        assert!(field.as_string().is_none());
1✔
551
        assert!(field.as_text().is_none());
1✔
552
        assert!(field.as_binary().is_none());
1✔
553
        assert!(field.as_decimal().is_none());
1✔
554
        assert!(field.as_timestamp().is_none());
1✔
555
        assert!(field.as_date().is_none());
1✔
556
        assert!(field.as_bson().is_none());
1✔
557
        assert!(field.as_null().is_none());
1✔
558

559
        let field = Field::Float(OrderedFloat::from(1.0));
1✔
560
        assert!(field.as_uint().is_none());
1✔
561
        assert!(field.as_int().is_none());
1✔
562
        assert!(field.as_float().is_some());
1✔
563
        assert!(field.as_boolean().is_none());
1✔
564
        assert!(field.as_string().is_none());
1✔
565
        assert!(field.as_text().is_none());
1✔
566
        assert!(field.as_binary().is_none());
1✔
567
        assert!(field.as_decimal().is_none());
1✔
568
        assert!(field.as_timestamp().is_none());
1✔
569
        assert!(field.as_date().is_none());
1✔
570
        assert!(field.as_bson().is_none());
1✔
571
        assert!(field.as_null().is_none());
1✔
572

573
        let field = Field::Boolean(true);
1✔
574
        assert!(field.as_uint().is_none());
1✔
575
        assert!(field.as_int().is_none());
1✔
576
        assert!(field.as_float().is_none());
1✔
577
        assert!(field.as_boolean().is_some());
1✔
578
        assert!(field.as_string().is_none());
1✔
579
        assert!(field.as_text().is_none());
1✔
580
        assert!(field.as_binary().is_none());
1✔
581
        assert!(field.as_decimal().is_none());
1✔
582
        assert!(field.as_timestamp().is_none());
1✔
583
        assert!(field.as_date().is_none());
1✔
584
        assert!(field.as_bson().is_none());
1✔
585
        assert!(field.as_null().is_none());
1✔
586

587
        let field = Field::String("".to_string());
1✔
588
        assert!(field.as_uint().is_none());
1✔
589
        assert!(field.as_int().is_none());
1✔
590
        assert!(field.as_float().is_none());
1✔
591
        assert!(field.as_boolean().is_none());
1✔
592
        assert!(field.as_string().is_some());
1✔
593
        assert!(field.as_text().is_none());
1✔
594
        assert!(field.as_binary().is_none());
1✔
595
        assert!(field.as_decimal().is_none());
1✔
596
        assert!(field.as_timestamp().is_none());
1✔
597
        assert!(field.as_date().is_none());
1✔
598
        assert!(field.as_bson().is_none());
1✔
599
        assert!(field.as_null().is_none());
1✔
600

601
        let field = Field::Text("".to_string());
1✔
602
        assert!(field.as_uint().is_none());
1✔
603
        assert!(field.as_int().is_none());
1✔
604
        assert!(field.as_float().is_none());
1✔
605
        assert!(field.as_boolean().is_none());
1✔
606
        assert!(field.as_string().is_none());
1✔
607
        assert!(field.as_text().is_some());
1✔
608
        assert!(field.as_binary().is_none());
1✔
609
        assert!(field.as_decimal().is_none());
1✔
610
        assert!(field.as_timestamp().is_none());
1✔
611
        assert!(field.as_date().is_none());
1✔
612
        assert!(field.as_bson().is_none());
1✔
613
        assert!(field.as_null().is_none());
1✔
614

615
        let field = Field::Binary(vec![]);
1✔
616
        assert!(field.as_uint().is_none());
1✔
617
        assert!(field.as_int().is_none());
1✔
618
        assert!(field.as_float().is_none());
1✔
619
        assert!(field.as_boolean().is_none());
1✔
620
        assert!(field.as_string().is_none());
1✔
621
        assert!(field.as_text().is_none());
1✔
622
        assert!(field.as_binary().is_some());
1✔
623
        assert!(field.as_decimal().is_none());
1✔
624
        assert!(field.as_timestamp().is_none());
1✔
625
        assert!(field.as_date().is_none());
1✔
626
        assert!(field.as_bson().is_none());
1✔
627
        assert!(field.as_null().is_none());
1✔
628

629
        let field = Field::Decimal(Decimal::from(1));
1✔
630
        assert!(field.as_uint().is_none());
1✔
631
        assert!(field.as_int().is_none());
1✔
632
        assert!(field.as_float().is_none());
1✔
633
        assert!(field.as_boolean().is_none());
1✔
634
        assert!(field.as_string().is_none());
1✔
635
        assert!(field.as_text().is_none());
1✔
636
        assert!(field.as_binary().is_none());
1✔
637
        assert!(field.as_decimal().is_some());
1✔
638
        assert!(field.as_timestamp().is_none());
1✔
639
        assert!(field.as_date().is_none());
1✔
640
        assert!(field.as_bson().is_none());
1✔
641
        assert!(field.as_null().is_none());
1✔
642

643
        let field = Field::Timestamp(DateTime::from(Utc.timestamp_millis_opt(0).unwrap()));
1✔
644
        assert!(field.as_uint().is_none());
1✔
645
        assert!(field.as_int().is_none());
1✔
646
        assert!(field.as_float().is_none());
1✔
647
        assert!(field.as_boolean().is_none());
1✔
648
        assert!(field.as_string().is_none());
1✔
649
        assert!(field.as_text().is_none());
1✔
650
        assert!(field.as_binary().is_none());
1✔
651
        assert!(field.as_decimal().is_none());
1✔
652
        assert!(field.as_timestamp().is_some());
1✔
653
        assert!(field.as_date().is_none());
1✔
654
        assert!(field.as_bson().is_none());
1✔
655
        assert!(field.as_null().is_none());
1✔
656

657
        let field = Field::Date(NaiveDate::from_ymd_opt(1970, 1, 1).unwrap());
1✔
658
        assert!(field.as_uint().is_none());
1✔
659
        assert!(field.as_int().is_none());
1✔
660
        assert!(field.as_float().is_none());
1✔
661
        assert!(field.as_boolean().is_none());
1✔
662
        assert!(field.as_string().is_none());
1✔
663
        assert!(field.as_text().is_none());
1✔
664
        assert!(field.as_binary().is_none());
1✔
665
        assert!(field.as_decimal().is_none());
1✔
666
        assert!(field.as_timestamp().is_none());
1✔
667
        assert!(field.as_date().is_some());
1✔
668
        assert!(field.as_bson().is_none());
1✔
669
        assert!(field.as_null().is_none());
1✔
670

671
        let field = Field::Bson(vec![]);
1✔
672
        assert!(field.as_uint().is_none());
1✔
673
        assert!(field.as_int().is_none());
1✔
674
        assert!(field.as_float().is_none());
1✔
675
        assert!(field.as_boolean().is_none());
1✔
676
        assert!(field.as_string().is_none());
1✔
677
        assert!(field.as_text().is_none());
1✔
678
        assert!(field.as_binary().is_none());
1✔
679
        assert!(field.as_decimal().is_none());
1✔
680
        assert!(field.as_timestamp().is_none());
1✔
681
        assert!(field.as_date().is_none());
1✔
682
        assert!(field.as_bson().is_some());
1✔
683
        assert!(field.as_null().is_none());
1✔
684

685
        let field = Field::Null;
1✔
686
        assert!(field.as_uint().is_none());
1✔
687
        assert!(field.as_int().is_none());
1✔
688
        assert!(field.as_float().is_none());
1✔
689
        assert!(field.as_boolean().is_none());
1✔
690
        assert!(field.as_string().is_none());
1✔
691
        assert!(field.as_text().is_none());
1✔
692
        assert!(field.as_binary().is_none());
1✔
693
        assert!(field.as_decimal().is_none());
1✔
694
        assert!(field.as_timestamp().is_none());
1✔
695
        assert!(field.as_date().is_none());
1✔
696
        assert!(field.as_bson().is_none());
1✔
697
        assert!(field.as_null().is_some());
1✔
698
    }
1✔
699

700
    #[test]
1✔
701
    fn test_to_conversion() {
1✔
702
        let field = Field::UInt(1);
1✔
703
        assert!(field.to_uint().is_some());
1✔
704
        assert!(field.to_int().is_some());
1✔
705
        assert!(field.to_float().is_some());
1✔
706
        assert!(field.to_boolean().is_some());
1✔
707
        assert!(field.to_string().is_some());
1✔
708
        assert!(field.to_text().is_some());
1✔
709
        assert!(field.to_binary().is_none());
1✔
710
        assert!(field.to_decimal().is_some());
1✔
711
        assert!(field.to_timestamp().unwrap().is_none());
1✔
712
        assert!(field.to_date().unwrap().is_none());
1✔
713
        assert!(field.to_bson().is_none());
1✔
714
        assert!(field.to_null().is_none());
1✔
715

716
        let field = Field::Int(1);
1✔
717
        assert!(field.to_uint().is_some());
1✔
718
        assert!(field.to_int().is_some());
1✔
719
        assert!(field.to_float().is_some());
1✔
720
        assert!(field.to_boolean().is_some());
1✔
721
        assert!(field.to_string().is_some());
1✔
722
        assert!(field.to_text().is_some());
1✔
723
        assert!(field.to_binary().is_none());
1✔
724
        assert!(field.to_decimal().is_some());
1✔
725
        assert!(field.to_timestamp().unwrap().is_none());
1✔
726
        assert!(field.to_date().unwrap().is_none());
1✔
727
        assert!(field.to_bson().is_none());
1✔
728
        assert!(field.to_null().is_none());
1✔
729

730
        let field = Field::Float(OrderedFloat::from(1.0));
1✔
731
        assert!(field.to_uint().is_none());
1✔
732
        assert!(field.to_int().is_none());
1✔
733
        assert!(field.to_float().is_some());
1✔
734
        assert!(field.to_boolean().is_some());
1✔
735
        assert!(field.to_string().is_some());
1✔
736
        assert!(field.to_text().is_some());
1✔
737
        assert!(field.to_binary().is_none());
1✔
738
        assert!(field.to_decimal().is_some());
1✔
739
        assert!(field.to_timestamp().unwrap().is_none());
1✔
740
        assert!(field.to_date().unwrap().is_none());
1✔
741
        assert!(field.to_bson().is_none());
1✔
742
        assert!(field.to_null().is_none());
1✔
743

744
        let field = Field::Boolean(true);
1✔
745
        assert!(field.to_uint().is_none());
1✔
746
        assert!(field.to_int().is_none());
1✔
747
        assert!(field.to_float().is_none());
1✔
748
        assert!(field.to_boolean().is_some());
1✔
749
        assert!(field.to_string().is_some());
1✔
750
        assert!(field.to_text().is_some());
1✔
751
        assert!(field.to_binary().is_none());
1✔
752
        assert!(field.to_decimal().is_none());
1✔
753
        assert!(field.to_timestamp().unwrap().is_none());
1✔
754
        assert!(field.to_date().unwrap().is_none());
1✔
755
        assert!(field.to_bson().is_none());
1✔
756
        assert!(field.to_null().is_none());
1✔
757

758
        let field = Field::String("".to_string());
1✔
759
        assert!(field.to_uint().is_none());
1✔
760
        assert!(field.to_int().is_none());
1✔
761
        assert!(field.to_float().is_none());
1✔
762
        assert!(field.to_boolean().is_none());
1✔
763
        assert!(field.to_string().is_some());
1✔
764
        assert!(field.to_text().is_some());
1✔
765
        assert!(field.to_binary().is_none());
1✔
766
        assert!(field.to_decimal().is_none());
1✔
767
        assert!(field.to_timestamp().unwrap().is_none());
1✔
768
        assert!(field.to_date().unwrap().is_none());
1✔
769
        assert!(field.to_bson().is_none());
1✔
770
        assert!(field.to_null().is_none());
1✔
771

772
        let field = Field::Text("".to_string());
1✔
773
        assert!(field.to_uint().is_none());
1✔
774
        assert!(field.to_int().is_none());
1✔
775
        assert!(field.to_float().is_none());
1✔
776
        assert!(field.to_boolean().is_none());
1✔
777
        assert!(field.to_string().is_some());
1✔
778
        assert!(field.to_text().is_some());
1✔
779
        assert!(field.to_binary().is_none());
1✔
780
        assert!(field.to_decimal().is_none());
1✔
781
        assert!(field.to_timestamp().unwrap().is_none());
1✔
782
        assert!(field.to_date().unwrap().is_none());
1✔
783
        assert!(field.to_bson().is_none());
1✔
784
        assert!(field.to_null().is_none());
1✔
785

786
        let field = Field::Binary(vec![]);
1✔
787
        assert!(field.to_uint().is_none());
1✔
788
        assert!(field.to_int().is_none());
1✔
789
        assert!(field.to_float().is_none());
1✔
790
        assert!(field.to_boolean().is_none());
1✔
791
        assert!(field.to_string().is_some());
1✔
792
        assert!(field.to_text().is_some());
1✔
793
        assert!(field.to_binary().is_some());
1✔
794
        assert!(field.to_decimal().is_none());
1✔
795
        assert!(field.to_timestamp().unwrap().is_none());
1✔
796
        assert!(field.to_date().unwrap().is_none());
1✔
797
        assert!(field.to_bson().is_none());
1✔
798
        assert!(field.to_null().is_none());
1✔
799

800
        let field = Field::Decimal(Decimal::from(1));
1✔
801
        assert!(field.to_uint().is_none());
1✔
802
        assert!(field.to_int().is_none());
1✔
803
        assert!(field.to_float().is_some());
1✔
804
        assert!(field.to_boolean().is_some());
1✔
805
        assert!(field.to_string().is_some());
1✔
806
        assert!(field.to_text().is_some());
1✔
807
        assert!(field.to_binary().is_none());
1✔
808
        assert!(field.to_decimal().is_some());
1✔
809
        assert!(field.to_timestamp().unwrap().is_none());
1✔
810
        assert!(field.to_date().unwrap().is_none());
1✔
811
        assert!(field.to_bson().is_none());
1✔
812
        assert!(field.to_null().is_none());
1✔
813

814
        let field = Field::Timestamp(DateTime::from(Utc.timestamp_millis_opt(0).unwrap()));
1✔
815
        assert!(field.to_uint().is_none());
1✔
816
        assert!(field.to_int().is_none());
1✔
817
        assert!(field.to_float().is_none());
1✔
818
        assert!(field.to_boolean().is_none());
1✔
819
        assert!(field.to_string().is_some());
1✔
820
        assert!(field.to_text().is_some());
1✔
821
        assert!(field.to_binary().is_none());
1✔
822
        assert!(field.to_decimal().is_none());
1✔
823
        assert!(field.to_timestamp().unwrap().is_some());
1✔
824
        assert!(field.to_date().unwrap().is_none());
1✔
825
        assert!(field.to_bson().is_none());
1✔
826
        assert!(field.to_null().is_none());
1✔
827

828
        let field = Field::Date(NaiveDate::from_ymd_opt(1970, 1, 1).unwrap());
1✔
829
        assert!(field.to_uint().is_none());
1✔
830
        assert!(field.to_int().is_none());
1✔
831
        assert!(field.to_float().is_none());
1✔
832
        assert!(field.to_boolean().is_none());
1✔
833
        assert!(field.to_string().is_some());
1✔
834
        assert!(field.to_text().is_some());
1✔
835
        assert!(field.to_binary().is_none());
1✔
836
        assert!(field.to_decimal().is_none());
1✔
837
        assert!(field.to_timestamp().unwrap().is_none());
1✔
838
        assert!(field.to_date().unwrap().is_some());
1✔
839
        assert!(field.to_bson().is_none());
1✔
840
        assert!(field.to_null().is_none());
1✔
841

842
        let field = Field::Bson(vec![]);
1✔
843
        assert!(field.to_uint().is_none());
1✔
844
        assert!(field.to_int().is_none());
1✔
845
        assert!(field.to_float().is_none());
1✔
846
        assert!(field.to_boolean().is_none());
1✔
847
        assert!(field.to_string().is_none());
1✔
848
        assert!(field.to_text().is_none());
1✔
849
        assert!(field.to_binary().is_none());
1✔
850
        assert!(field.to_decimal().is_none());
1✔
851
        assert!(field.to_timestamp().unwrap().is_none());
1✔
852
        assert!(field.to_date().unwrap().is_none());
1✔
853
        assert!(field.to_bson().is_some());
1✔
854
        assert!(field.to_null().is_none());
1✔
855

856
        let field = Field::Null;
1✔
857
        assert!(field.to_uint().is_some());
1✔
858
        assert!(field.to_int().is_some());
1✔
859
        assert!(field.to_float().is_some());
1✔
860
        assert!(field.to_boolean().is_some());
1✔
861
        assert!(field.to_string().is_some());
1✔
862
        assert!(field.to_text().is_some());
1✔
863
        assert!(field.to_binary().is_none());
1✔
864
        assert!(field.to_decimal().is_some());
1✔
865
        assert!(field.to_timestamp().unwrap().is_some());
1✔
866
        assert!(field.to_date().unwrap().is_some());
1✔
867
        assert!(field.to_bson().is_none());
1✔
868
        assert!(field.to_null().is_some());
1✔
869
    }
1✔
870
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc