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

getdozer / dozer / 4020902227

pending completion
4020902227

Pull #743

github

GitHub
Merge 57279c6b6 into a12da35a5
Pull Request #743: Chore clippy fix

165 of 165 new or added lines in 60 files covered. (100.0%)

23638 of 35485 relevant lines covered (66.61%)

38417.79 hits per line

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

89.87
/dozer-types/src/types/field.rs
1
use crate::errors::types::DeserializationError;
2
use chrono::{DateTime, FixedOffset, 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)]
12,084,984✔
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
    #[serde(with = "rust_decimal::serde::float")]
21
    Decimal(Decimal),
22
    Timestamp(DateTime<FixedOffset>),
23
    Date(NaiveDate),
24
    Bson(Vec<u8>),
25
    Null,
26
}
27

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

45
impl Field {
46
    fn data_encoding_len(&self) -> usize {
6,213,710✔
47
        match self {
6,213,710✔
48
            Field::UInt(_) => 8,
195,994✔
49
            Field::Int(_) => 8,
579,313✔
50
            Field::Float(_) => 8,
52,984✔
51
            Field::Boolean(_) => 1,
4,006✔
52
            Field::String(s) => s.len(),
5,301,136✔
53
            Field::Text(s) => s.len(),
4,015✔
54
            Field::Binary(b) => b.len(),
4,015✔
55
            Field::Decimal(_) => 16,
4,681✔
56
            Field::Timestamp(_) => 8,
4,195✔
57
            Field::Date(_) => 10,
4,204✔
58
            Field::Bson(b) => b.len(),
2,012✔
59
            Field::Null => 0,
57,155✔
60
        }
61
    }
6,213,710✔
62

63
    fn encode_data(&self) -> Cow<[u8]> {
6,100,387✔
64
        match self {
6,100,387✔
65
            Field::UInt(i) => Cow::Owned(i.to_be_bytes().into()),
136,574✔
66
            Field::Int(i) => Cow::Owned(i.to_be_bytes().into()),
574,883✔
67
            Field::Float(f) => Cow::Owned(f.to_be_bytes().into()),
31,553✔
68
            Field::Boolean(b) => Cow::Owned(if *b { [1] } else { [0] }.into()),
2,015✔
69
            Field::String(s) => Cow::Borrowed(s.as_bytes()),
5,298,830✔
70
            Field::Text(s) => Cow::Borrowed(s.as_bytes()),
2,015✔
71
            Field::Binary(b) => Cow::Borrowed(b.as_slice()),
2,015✔
72
            Field::Decimal(d) => Cow::Owned(d.serialize().into()),
2,681✔
73
            Field::Timestamp(t) => Cow::Owned(t.timestamp_millis().to_be_bytes().into()),
2,213✔
74
            Field::Date(t) => Cow::Owned(t.to_string().into()),
2,213✔
75
            Field::Bson(b) => Cow::Borrowed(b),
1,012✔
76
            Field::Null => Cow::Owned([].into()),
44,383✔
77
        }
78
    }
6,100,387✔
79

80
    pub fn encode_buf(&self, destination: &mut [u8]) {
6,100,770✔
81
        let prefix = self.get_type_prefix();
6,100,770✔
82
        let data = self.encode_data();
6,100,770✔
83
        destination[0] = prefix;
6,100,770✔
84
        destination[1..].copy_from_slice(&data);
6,100,770✔
85
    }
6,100,770✔
86

87
    pub fn encoding_len(&self) -> usize {
6,212,680✔
88
        self.data_encoding_len() + 1
6,212,680✔
89
    }
6,212,680✔
90

91
    pub fn encode(&self) -> Vec<u8> {
5,986,983✔
92
        let mut result = vec![0; self.encoding_len()];
5,986,983✔
93
        self.encode_buf(&mut result);
5,986,983✔
94
        result
5,986,983✔
95
    }
5,986,983✔
96

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

114
    pub fn decode(buf: &[u8]) -> Result<Field, DeserializationError> {
94,099✔
115
        Self::decode_borrow(buf).map(|field| field.to_owned())
94,099✔
116
    }
94,099✔
117

118
    pub fn decode_borrow(buf: &[u8]) -> Result<FieldBorrow, DeserializationError> {
5,778,787✔
119
        let first_byte = *buf.first().ok_or(DeserializationError::EmptyInput)?;
5,778,787✔
120
        let val = &buf[1..];
5,778,787✔
121
        match first_byte {
5,778,787✔
122
            0 => Ok(FieldBorrow::UInt(u64::from_be_bytes(
123
                val.try_into()
3,639,719✔
124
                    .map_err(|_| DeserializationError::BadDataLength)?,
3,639,719✔
125
            ))),
126
            1 => Ok(FieldBorrow::Int(i64::from_be_bytes(
127
                val.try_into()
96,104✔
128
                    .map_err(|_| DeserializationError::BadDataLength)?,
96,104✔
129
            ))),
130
            2 => Ok(FieldBorrow::Float(OrderedFloat(f64::from_be_bytes(
131
                val.try_into()
700,418✔
132
                    .map_err(|_| DeserializationError::BadDataLength)?,
700,418✔
133
            )))),
134
            3 => Ok(FieldBorrow::Boolean(val[0] == 1)),
812✔
135
            4 => Ok(FieldBorrow::String(std::str::from_utf8(val)?)),
1,028✔
136
            5 => Ok(FieldBorrow::Text(std::str::from_utf8(val)?)),
812✔
137
            6 => Ok(FieldBorrow::Binary(val)),
812✔
138
            7 => Ok(FieldBorrow::Decimal(Decimal::deserialize(
139
                val.try_into()
1,370✔
140
                    .map_err(|_| DeserializationError::BadDataLength)?,
1,370✔
141
            ))),
142
            8 => Ok(FieldBorrow::Timestamp(DateTime::from(
143
                Utc.timestamp_millis(i64::from_be_bytes(
144
                    val.try_into()
1,010✔
145
                        .map_err(|_| DeserializationError::BadDataLength)?,
1,010✔
146
                )),
147
            ))),
148
            9 => Ok(FieldBorrow::Date(NaiveDate::parse_from_str(
149
                std::str::from_utf8(val)?,
1,010✔
150
                DATE_FORMAT,
1,010✔
151
            )?)),
×
152
            10 => Ok(FieldBorrow::Bson(val)),
406✔
153
            11 => Ok(FieldBorrow::Null),
1,335,286✔
154
            other => Err(DeserializationError::UnrecognisedFieldType(other)),
×
155
        }
156
    }
5,778,787✔
157

158
    fn get_type_prefix(&self) -> u8 {
6,099,969✔
159
        match self {
6,099,969✔
160
            Field::UInt(_) => 0,
136,095✔
161
            Field::Int(_) => 1,
574,890✔
162
            Field::Float(_) => 2,
31,551✔
163
            Field::Boolean(_) => 3,
2,013✔
164
            Field::String(_) => 4,
5,298,900✔
165
            Field::Text(_) => 5,
2,013✔
166
            Field::Binary(_) => 6,
2,013✔
167
            Field::Decimal(_) => 7,
2,679✔
168
            Field::Timestamp(_) => 8,
2,211✔
169
            Field::Date(_) => 9,
2,211✔
170
            Field::Bson(_) => 10,
1,011✔
171
            Field::Null => 11,
44,382✔
172
        }
173
    }
6,099,969✔
174

175
    pub fn as_uint(&self) -> Option<u64> {
6,720,888✔
176
        match self {
6,720,888✔
177
            Field::UInt(i) => Some(*i),
5,993,812✔
178
            _ => None,
727,076✔
179
        }
180
    }
6,720,888✔
181

182
    pub fn as_int(&self) -> Option<i64> {
435✔
183
        match self {
435✔
184
            Field::Int(i) => Some(*i),
424✔
185
            _ => None,
11✔
186
        }
187
    }
435✔
188

189
    pub fn as_float(&self) -> Option<f64> {
1,472,142✔
190
        match self {
1,472,142✔
191
            Field::Float(f) => Some(f.0),
1,472,131✔
192
            _ => None,
11✔
193
        }
194
    }
1,472,142✔
195

196
    pub fn as_boolean(&self) -> Option<bool> {
12✔
197
        match self {
12✔
198
            Field::Boolean(b) => Some(*b),
1✔
199
            _ => None,
11✔
200
        }
201
    }
12✔
202

203
    pub fn as_string(&self) -> Option<&str> {
2,954,910✔
204
        match self {
2,954,910✔
205
            Field::String(s) => Some(s),
2,954,899✔
206
            _ => None,
11✔
207
        }
208
    }
2,954,910✔
209

210
    pub fn as_text(&self) -> Option<&str> {
21✔
211
        match self {
21✔
212
            Field::Text(s) => Some(s),
10✔
213
            _ => None,
11✔
214
        }
215
    }
21✔
216

217
    pub fn as_binary(&self) -> Option<&[u8]> {
12✔
218
        match self {
12✔
219
            Field::Binary(b) => Some(b),
1✔
220
            _ => None,
11✔
221
        }
222
    }
12✔
223

224
    pub fn as_decimal(&self) -> Option<Decimal> {
12✔
225
        match self {
12✔
226
            Field::Decimal(d) => Some(*d),
1✔
227
            _ => None,
11✔
228
        }
229
    }
12✔
230

231
    pub fn as_timestamp(&self) -> Option<DateTime<FixedOffset>> {
736,077✔
232
        match self {
736,077✔
233
            Field::Timestamp(t) => Some(*t),
736,066✔
234
            _ => None,
11✔
235
        }
236
    }
736,077✔
237

238
    pub fn as_date(&self) -> Option<NaiveDate> {
12✔
239
        match self {
12✔
240
            Field::Date(d) => Some(*d),
1✔
241
            _ => None,
11✔
242
        }
243
    }
12✔
244

245
    pub fn as_bson(&self) -> Option<&[u8]> {
12✔
246
        match self {
12✔
247
            Field::Bson(b) => Some(b),
1✔
248
            _ => None,
11✔
249
        }
250
    }
12✔
251

252
    pub fn as_null(&self) -> Option<()> {
12✔
253
        match self {
12✔
254
            Field::Null => Some(()),
1✔
255
            _ => None,
11✔
256
        }
257
    }
12✔
258

259
    pub fn to_uint(&self) -> Option<u64> {
372✔
260
        match self {
372✔
261
            Field::UInt(i) => Some(*i),
361✔
262
            Field::Int(i) => u64::from_i64(*i),
1✔
263
            Field::String(s) => s.parse::<u64>().ok(),
1✔
264
            Field::Null => Some(0_u64),
1✔
265
            _ => None,
8✔
266
        }
267
    }
372✔
268

269
    pub fn to_int(&self) -> Option<i64> {
624✔
270
        match self {
624✔
271
            Field::Int(i) => Some(*i),
451✔
272
            Field::UInt(u) => i64::from_u64(*u),
10✔
273
            Field::String(s) => s.parse::<i64>().ok(),
10✔
274
            Field::Null => Some(0_i64),
145✔
275
            _ => None,
8✔
276
        }
277
    }
624✔
278

279
    pub fn to_float(&self) -> Option<f64> {
633✔
280
        match self {
633✔
281
            Field::Float(f) => Some(f.0),
442✔
282
            Field::Decimal(d) => d.to_f64(),
10✔
283
            Field::UInt(u) => f64::from_u64(*u),
10✔
284
            Field::Int(i) => f64::from_i64(*i),
10✔
285
            Field::Null => Some(0_f64),
145✔
286
            Field::String(s) => s.parse::<f64>().ok(),
10✔
287
            _ => None,
6✔
288
        }
289
    }
633✔
290

291
    pub fn to_boolean(&self) -> Option<bool> {
66✔
292
        match self {
66✔
293
            Field::Boolean(b) => Some(*b),
10✔
294
            Field::Null => Some(false),
1✔
295
            Field::Int(i) => Some(*i > 0_i64),
10✔
296
            Field::UInt(i) => Some(*i > 0_u64),
10✔
297
            Field::Float(i) => Some(i.0 > 0_f64),
10✔
298
            Field::Decimal(i) => Some(i.gt(&Decimal::from(0_u64))),
19✔
299
            _ => None,
6✔
300
        }
301
    }
66✔
302

303
    pub fn to_string(&self) -> Option<String> {
426✔
304
        match self {
426✔
305
            Field::String(s) => Some(s.to_owned()),
298✔
306
            Field::Text(t) => Some(t.to_owned()),
46✔
307
            Field::Int(i) => Some(format!("{i}")),
10✔
308
            Field::UInt(i) => Some(format!("{i}")),
10✔
309
            Field::Float(i) => Some(format!("{i}")),
10✔
310
            Field::Decimal(i) => Some(format!("{i}")),
10✔
311
            Field::Boolean(i) => Some(if *i {
10✔
312
                "TRUE".to_string()
10✔
313
            } else {
314
                "FALSE".to_string()
×
315
            }),
316
            Field::Date(d) => Some(d.format("%Y-%m-%d").to_string()),
10✔
317
            Field::Timestamp(t) => Some(t.to_rfc3339()),
10✔
318
            Field::Binary(b) => Some(format!("{b:X?}")),
1✔
319
            Field::Null => Some("".to_string()),
10✔
320
            _ => None,
1✔
321
        }
322
    }
426✔
323

324
    pub fn to_text(&self) -> Option<String> {
93✔
325
        match self {
93✔
326
            Field::String(s) => Some(s.to_owned()),
10✔
327
            Field::Text(t) => Some(t.to_owned()),
10✔
328
            Field::Int(i) => Some(format!("{i}")),
10✔
329
            Field::UInt(i) => Some(format!("{i}")),
10✔
330
            Field::Float(i) => Some(format!("{i}")),
10✔
331
            Field::Decimal(i) => Some(format!("{i}")),
10✔
332
            Field::Boolean(i) => Some(if *i {
10✔
333
                "TRUE".to_string()
10✔
334
            } else {
335
                "FALSE".to_string()
×
336
            }),
337
            Field::Date(d) => Some(d.format("%Y-%m-%d").to_string()),
10✔
338
            Field::Timestamp(t) => Some(t.to_rfc3339()),
10✔
339
            Field::Binary(b) => Some(format!("{b:X?}")),
1✔
340
            Field::Null => Some("".to_string()),
1✔
341
            _ => None,
1✔
342
        }
343
    }
93✔
344

345
    pub fn to_binary(&self) -> Option<&[u8]> {
12✔
346
        match self {
12✔
347
            Field::Binary(b) => Some(b),
1✔
348
            _ => None,
11✔
349
        }
350
    }
12✔
351

352
    pub fn to_decimal(&self) -> Option<Decimal> {
588✔
353
        match self {
588✔
354
            Field::Decimal(d) => Some(*d),
433✔
355
            Field::Float(f) => Decimal::from_f64_retain(f.0),
1✔
356
            Field::Int(i) => Decimal::from_i64(*i),
1✔
357
            Field::UInt(u) => Decimal::from_u64(*u),
1✔
358
            Field::Null => Some(Decimal::from(0)),
145✔
359
            Field::String(s) => Decimal::from_str_exact(s).ok(),
1✔
360
            _ => None,
6✔
361
        }
362
    }
588✔
363

364
    pub fn to_timestamp(&self) -> Option<DateTime<FixedOffset>> {
300✔
365
        match self {
300✔
366
            Field::Timestamp(t) => Some(*t),
217✔
367
            Field::String(s) => DateTime::parse_from_rfc3339(s.as_str()).ok(),
1✔
368
            Field::Null => Some(DateTime::from(Utc.timestamp_millis(0))),
73✔
369
            _ => None,
9✔
370
        }
×
371
    }
300✔
372

×
373
    pub fn to_date(&self) -> Option<NaiveDate> {
300✔
374
        match self {
300✔
375
            Field::Date(d) => Some(*d),
217✔
376
            Field::String(s) => NaiveDate::parse_from_str(s, "%Y-%m-%d").ok(),
1✔
377
            Field::Null => Some(Utc.timestamp_millis(0).naive_utc().date()),
73✔
378
            _ => None,
9✔
379
        }
380
    }
300✔
381

×
382
    pub fn to_bson(&self) -> Option<&[u8]> {
12✔
383
        match self {
12✔
384
            Field::Bson(b) => Some(b),
1✔
385
            _ => None,
11✔
386
        }
387
    }
12✔
388

×
389
    pub fn to_null(&self) -> Option<()> {
12✔
390
        match self {
12✔
391
            Field::Null => Some(()),
1✔
392
            _ => None,
11✔
393
        }
394
    }
12✔
395
}
396

×
397
impl Display for Field {
×
398
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
×
399
        match self {
×
400
            Field::UInt(v) => f.write_str(&format!("{v} (unsigned int)")),
×
401
            Field::Int(v) => f.write_str(&format!("{v} (signed int)")),
×
402
            Field::Float(v) => f.write_str(&format!("{v} (Float)")),
×
403
            Field::Boolean(v) => f.write_str(&format!("{v}")),
×
404
            Field::String(v) => f.write_str(&v.to_string()),
×
405
            Field::Text(v) => f.write_str(&v.to_string()),
×
406
            Field::Binary(v) => f.write_str(&format!("{v:x?}")),
×
407
            Field::Decimal(v) => f.write_str(&format!("{v} (Decimal)")),
×
408
            Field::Timestamp(v) => f.write_str(&format!("{v}")),
×
409
            Field::Date(v) => f.write_str(&format!("{v}")),
×
410
            Field::Bson(v) => f.write_str(&format!("{v:x?}")),
×
411
            Field::Null => f.write_str("NULL"),
×
412
        }
413
    }
×
414
}
415

×
416
impl<'a> FieldBorrow<'a> {
×
417
    pub fn to_owned(self) -> Field {
94,121✔
418
        match self {
94,121✔
419
            FieldBorrow::Int(i) => Field::Int(i),
92,542✔
420
            FieldBorrow::UInt(i) => Field::UInt(i),
193✔
421
            FieldBorrow::Float(f) => Field::Float(f),
400✔
422
            FieldBorrow::Boolean(b) => Field::Boolean(b),
4✔
423
            FieldBorrow::String(s) => Field::String(s.to_owned()),
4✔
424
            FieldBorrow::Text(s) => Field::Text(s.to_owned()),
4✔
425
            FieldBorrow::Binary(b) => Field::Binary(b.to_owned()),
4✔
426
            FieldBorrow::Decimal(d) => Field::Decimal(d),
562✔
427
            FieldBorrow::Timestamp(t) => Field::Timestamp(t),
202✔
428
            FieldBorrow::Date(d) => Field::Date(d),
202✔
429
            FieldBorrow::Bson(b) => Field::Bson(b.to_owned()),
2✔
430
            FieldBorrow::Null => Field::Null,
2✔
431
        }
432
    }
94,121✔
433
}
×
434

435
#[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq)]
403,763✔
436
pub enum FieldType {
437
    UInt,
438
    Int,
439
    Float,
440
    Boolean,
441
    String,
442
    Text,
443
    Binary,
444
    Decimal,
445
    Timestamp,
446
    Date,
447
    Bson,
448
}
449

×
450
impl Display for FieldType {
×
451
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
×
452
        match self {
×
453
            FieldType::UInt => f.write_str("unsigned int"),
×
454
            FieldType::Int => f.write_str("int"),
×
455
            FieldType::Float => f.write_str("float"),
×
456
            FieldType::Boolean => f.write_str("boolean"),
×
457
            FieldType::String => f.write_str("string"),
×
458
            FieldType::Text => f.write_str("text"),
×
459
            FieldType::Binary => f.write_str("binary"),
×
460
            FieldType::Decimal => f.write_str("decimal"),
×
461
            FieldType::Timestamp => f.write_str("timestamp"),
×
462
            FieldType::Date => f.write_str("date"),
×
463
            FieldType::Bson => f.write_str("bson"),
×
464
        }
465
    }
×
466
}
467

468
/// Can't put it in `tests` module because of <https://github.com/rust-lang/cargo/issues/8379>
×
469
/// and we need this function in `dozer-cache`.
×
470
pub fn field_test_cases() -> impl Iterator<Item = Field> {
428✔
471
    [
428✔
472
        Field::Int(0_i64),
428✔
473
        Field::Int(1_i64),
428✔
474
        Field::UInt(0_u64),
428✔
475
        Field::UInt(1_u64),
428✔
476
        Field::Float(OrderedFloat::from(0_f64)),
428✔
477
        Field::Float(OrderedFloat::from(1_f64)),
428✔
478
        Field::Boolean(true),
428✔
479
        Field::Boolean(false),
428✔
480
        Field::String("".to_string()),
428✔
481
        Field::String("1".to_string()),
428✔
482
        Field::Text("".to_string()),
428✔
483
        Field::Text("1".to_string()),
428✔
484
        Field::Binary(vec![]),
428✔
485
        Field::Binary(vec![1]),
428✔
486
        Field::Decimal(Decimal::new(0, 0)),
428✔
487
        Field::Decimal(Decimal::new(1, 0)),
428✔
488
        Field::Timestamp(DateTime::from(Utc.timestamp_millis(0))),
428✔
489
        Field::Timestamp(DateTime::parse_from_rfc3339("2020-01-01T00:00:00Z").unwrap()),
428✔
490
        Field::Date(NaiveDate::from_ymd(1970, 1, 1)),
428✔
491
        Field::Date(NaiveDate::from_ymd(2020, 1, 1)),
428✔
492
        Field::Bson(vec![
428✔
493
            // BSON representation of `{"abc":"foo"}`
428✔
494
            123, 34, 97, 98, 99, 34, 58, 34, 102, 111, 111, 34, 125,
428✔
495
        ]),
428✔
496
        Field::Null,
428✔
497
    ]
428✔
498
    .into_iter()
428✔
499
}
428✔
500

501
#[cfg(test)]
502
pub mod tests {
503
    use super::*;
×
504

×
505
    #[test]
1✔
506
    fn data_encoding_len_must_agree_with_encode() {
1✔
507
        for field in field_test_cases() {
23✔
508
            let bytes = field.encode_data();
22✔
509
            assert_eq!(bytes.len(), field.data_encoding_len());
22✔
510
        }
511
    }
1✔
512

×
513
    #[test]
1✔
514
    fn test_as_conversion() {
1✔
515
        let field = Field::UInt(1);
1✔
516
        assert!(field.as_uint().is_some());
1✔
517
        assert!(field.as_int().is_none());
1✔
518
        assert!(field.as_float().is_none());
1✔
519
        assert!(field.as_boolean().is_none());
1✔
520
        assert!(field.as_string().is_none());
1✔
521
        assert!(field.as_text().is_none());
1✔
522
        assert!(field.as_binary().is_none());
1✔
523
        assert!(field.as_decimal().is_none());
1✔
524
        assert!(field.as_timestamp().is_none());
1✔
525
        assert!(field.as_date().is_none());
1✔
526
        assert!(field.as_bson().is_none());
1✔
527
        assert!(field.as_null().is_none());
1✔
528

×
529
        let field = Field::Int(1);
1✔
530
        assert!(field.as_uint().is_none());
1✔
531
        assert!(field.as_int().is_some());
1✔
532
        assert!(field.as_float().is_none());
1✔
533
        assert!(field.as_boolean().is_none());
1✔
534
        assert!(field.as_string().is_none());
1✔
535
        assert!(field.as_text().is_none());
1✔
536
        assert!(field.as_binary().is_none());
1✔
537
        assert!(field.as_decimal().is_none());
1✔
538
        assert!(field.as_timestamp().is_none());
1✔
539
        assert!(field.as_date().is_none());
1✔
540
        assert!(field.as_bson().is_none());
1✔
541
        assert!(field.as_null().is_none());
1✔
542

×
543
        let field = Field::Float(OrderedFloat::from(1.0));
1✔
544
        assert!(field.as_uint().is_none());
1✔
545
        assert!(field.as_int().is_none());
1✔
546
        assert!(field.as_float().is_some());
1✔
547
        assert!(field.as_boolean().is_none());
1✔
548
        assert!(field.as_string().is_none());
1✔
549
        assert!(field.as_text().is_none());
1✔
550
        assert!(field.as_binary().is_none());
1✔
551
        assert!(field.as_decimal().is_none());
1✔
552
        assert!(field.as_timestamp().is_none());
1✔
553
        assert!(field.as_date().is_none());
1✔
554
        assert!(field.as_bson().is_none());
1✔
555
        assert!(field.as_null().is_none());
1✔
556

×
557
        let field = Field::Boolean(true);
1✔
558
        assert!(field.as_uint().is_none());
1✔
559
        assert!(field.as_int().is_none());
1✔
560
        assert!(field.as_float().is_none());
1✔
561
        assert!(field.as_boolean().is_some());
1✔
562
        assert!(field.as_string().is_none());
1✔
563
        assert!(field.as_text().is_none());
1✔
564
        assert!(field.as_binary().is_none());
1✔
565
        assert!(field.as_decimal().is_none());
1✔
566
        assert!(field.as_timestamp().is_none());
1✔
567
        assert!(field.as_date().is_none());
1✔
568
        assert!(field.as_bson().is_none());
1✔
569
        assert!(field.as_null().is_none());
1✔
570

×
571
        let field = Field::String("".to_string());
1✔
572
        assert!(field.as_uint().is_none());
1✔
573
        assert!(field.as_int().is_none());
1✔
574
        assert!(field.as_float().is_none());
1✔
575
        assert!(field.as_boolean().is_none());
1✔
576
        assert!(field.as_string().is_some());
1✔
577
        assert!(field.as_text().is_none());
1✔
578
        assert!(field.as_binary().is_none());
1✔
579
        assert!(field.as_decimal().is_none());
1✔
580
        assert!(field.as_timestamp().is_none());
1✔
581
        assert!(field.as_date().is_none());
1✔
582
        assert!(field.as_bson().is_none());
1✔
583
        assert!(field.as_null().is_none());
1✔
584

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

×
599
        let field = Field::Binary(vec![]);
1✔
600
        assert!(field.as_uint().is_none());
1✔
601
        assert!(field.as_int().is_none());
1✔
602
        assert!(field.as_float().is_none());
1✔
603
        assert!(field.as_boolean().is_none());
1✔
604
        assert!(field.as_string().is_none());
1✔
605
        assert!(field.as_text().is_none());
1✔
606
        assert!(field.as_binary().is_some());
1✔
607
        assert!(field.as_decimal().is_none());
1✔
608
        assert!(field.as_timestamp().is_none());
1✔
609
        assert!(field.as_date().is_none());
1✔
610
        assert!(field.as_bson().is_none());
1✔
611
        assert!(field.as_null().is_none());
1✔
612

×
613
        let field = Field::Decimal(Decimal::from(1));
1✔
614
        assert!(field.as_uint().is_none());
1✔
615
        assert!(field.as_int().is_none());
1✔
616
        assert!(field.as_float().is_none());
1✔
617
        assert!(field.as_boolean().is_none());
1✔
618
        assert!(field.as_string().is_none());
1✔
619
        assert!(field.as_text().is_none());
1✔
620
        assert!(field.as_binary().is_none());
1✔
621
        assert!(field.as_decimal().is_some());
1✔
622
        assert!(field.as_timestamp().is_none());
1✔
623
        assert!(field.as_date().is_none());
1✔
624
        assert!(field.as_bson().is_none());
1✔
625
        assert!(field.as_null().is_none());
1✔
626

×
627
        let field = Field::Timestamp(DateTime::from(Utc.timestamp_millis(0)));
1✔
628
        assert!(field.as_uint().is_none());
1✔
629
        assert!(field.as_int().is_none());
1✔
630
        assert!(field.as_float().is_none());
1✔
631
        assert!(field.as_boolean().is_none());
1✔
632
        assert!(field.as_string().is_none());
1✔
633
        assert!(field.as_text().is_none());
1✔
634
        assert!(field.as_binary().is_none());
1✔
635
        assert!(field.as_decimal().is_none());
1✔
636
        assert!(field.as_timestamp().is_some());
1✔
637
        assert!(field.as_date().is_none());
1✔
638
        assert!(field.as_bson().is_none());
1✔
639
        assert!(field.as_null().is_none());
1✔
640

×
641
        let field = Field::Date(NaiveDate::from_ymd(1970, 1, 1));
1✔
642
        assert!(field.as_uint().is_none());
1✔
643
        assert!(field.as_int().is_none());
1✔
644
        assert!(field.as_float().is_none());
1✔
645
        assert!(field.as_boolean().is_none());
1✔
646
        assert!(field.as_string().is_none());
1✔
647
        assert!(field.as_text().is_none());
1✔
648
        assert!(field.as_binary().is_none());
1✔
649
        assert!(field.as_decimal().is_none());
1✔
650
        assert!(field.as_timestamp().is_none());
1✔
651
        assert!(field.as_date().is_some());
1✔
652
        assert!(field.as_bson().is_none());
1✔
653
        assert!(field.as_null().is_none());
1✔
654

×
655
        let field = Field::Bson(vec![]);
1✔
656
        assert!(field.as_uint().is_none());
1✔
657
        assert!(field.as_int().is_none());
1✔
658
        assert!(field.as_float().is_none());
1✔
659
        assert!(field.as_boolean().is_none());
1✔
660
        assert!(field.as_string().is_none());
1✔
661
        assert!(field.as_text().is_none());
1✔
662
        assert!(field.as_binary().is_none());
1✔
663
        assert!(field.as_decimal().is_none());
1✔
664
        assert!(field.as_timestamp().is_none());
1✔
665
        assert!(field.as_date().is_none());
1✔
666
        assert!(field.as_bson().is_some());
1✔
667
        assert!(field.as_null().is_none());
1✔
668

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

×
684
    #[test]
1✔
685
    fn test_to_conversion() {
1✔
686
        let field = Field::UInt(1);
1✔
687
        assert!(field.to_uint().is_some());
1✔
688
        assert!(field.to_int().is_some());
1✔
689
        assert!(field.to_float().is_some());
1✔
690
        assert!(field.to_boolean().is_some());
1✔
691
        assert!(field.to_string().is_some());
1✔
692
        assert!(field.to_text().is_some());
1✔
693
        assert!(field.to_binary().is_none());
1✔
694
        assert!(field.to_decimal().is_some());
1✔
695
        assert!(field.to_timestamp().is_none());
1✔
696
        assert!(field.to_date().is_none());
1✔
697
        assert!(field.to_bson().is_none());
1✔
698
        assert!(field.to_null().is_none());
1✔
699

×
700
        let field = Field::Int(1);
1✔
701
        assert!(field.to_uint().is_some());
1✔
702
        assert!(field.to_int().is_some());
1✔
703
        assert!(field.to_float().is_some());
1✔
704
        assert!(field.to_boolean().is_some());
1✔
705
        assert!(field.to_string().is_some());
1✔
706
        assert!(field.to_text().is_some());
1✔
707
        assert!(field.to_binary().is_none());
1✔
708
        assert!(field.to_decimal().is_some());
1✔
709
        assert!(field.to_timestamp().is_none());
1✔
710
        assert!(field.to_date().is_none());
1✔
711
        assert!(field.to_bson().is_none());
1✔
712
        assert!(field.to_null().is_none());
1✔
713

×
714
        let field = Field::Float(OrderedFloat::from(1.0));
1✔
715
        assert!(field.to_uint().is_none());
1✔
716
        assert!(field.to_int().is_none());
1✔
717
        assert!(field.to_float().is_some());
1✔
718
        assert!(field.to_boolean().is_some());
1✔
719
        assert!(field.to_string().is_some());
1✔
720
        assert!(field.to_text().is_some());
1✔
721
        assert!(field.to_binary().is_none());
1✔
722
        assert!(field.to_decimal().is_some());
1✔
723
        assert!(field.to_timestamp().is_none());
1✔
724
        assert!(field.to_date().is_none());
1✔
725
        assert!(field.to_bson().is_none());
1✔
726
        assert!(field.to_null().is_none());
1✔
727

×
728
        let field = Field::Boolean(true);
1✔
729
        assert!(field.to_uint().is_none());
1✔
730
        assert!(field.to_int().is_none());
1✔
731
        assert!(field.to_float().is_none());
1✔
732
        assert!(field.to_boolean().is_some());
1✔
733
        assert!(field.to_string().is_some());
1✔
734
        assert!(field.to_text().is_some());
1✔
735
        assert!(field.to_binary().is_none());
1✔
736
        assert!(field.to_decimal().is_none());
1✔
737
        assert!(field.to_timestamp().is_none());
1✔
738
        assert!(field.to_date().is_none());
1✔
739
        assert!(field.to_bson().is_none());
1✔
740
        assert!(field.to_null().is_none());
1✔
741

×
742
        let field = Field::String("".to_string());
1✔
743
        assert!(field.to_uint().is_none());
1✔
744
        assert!(field.to_int().is_none());
1✔
745
        assert!(field.to_float().is_none());
1✔
746
        assert!(field.to_boolean().is_none());
1✔
747
        assert!(field.to_string().is_some());
1✔
748
        assert!(field.to_text().is_some());
1✔
749
        assert!(field.to_binary().is_none());
1✔
750
        assert!(field.to_decimal().is_none());
1✔
751
        assert!(field.to_timestamp().is_none());
1✔
752
        assert!(field.to_date().is_none());
1✔
753
        assert!(field.to_bson().is_none());
1✔
754
        assert!(field.to_null().is_none());
1✔
755

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

×
770
        let field = Field::Binary(vec![]);
1✔
771
        assert!(field.to_uint().is_none());
1✔
772
        assert!(field.to_int().is_none());
1✔
773
        assert!(field.to_float().is_none());
1✔
774
        assert!(field.to_boolean().is_none());
1✔
775
        assert!(field.to_string().is_some());
1✔
776
        assert!(field.to_text().is_some());
1✔
777
        assert!(field.to_binary().is_some());
1✔
778
        assert!(field.to_decimal().is_none());
1✔
779
        assert!(field.to_timestamp().is_none());
1✔
780
        assert!(field.to_date().is_none());
1✔
781
        assert!(field.to_bson().is_none());
1✔
782
        assert!(field.to_null().is_none());
1✔
783

×
784
        let field = Field::Decimal(Decimal::from(1));
1✔
785
        assert!(field.to_uint().is_none());
1✔
786
        assert!(field.to_int().is_none());
1✔
787
        assert!(field.to_float().is_some());
1✔
788
        assert!(field.to_boolean().is_some());
1✔
789
        assert!(field.to_string().is_some());
1✔
790
        assert!(field.to_text().is_some());
1✔
791
        assert!(field.to_binary().is_none());
1✔
792
        assert!(field.to_decimal().is_some());
1✔
793
        assert!(field.to_timestamp().is_none());
1✔
794
        assert!(field.to_date().is_none());
1✔
795
        assert!(field.to_bson().is_none());
1✔
796
        assert!(field.to_null().is_none());
1✔
797

×
798
        let field = Field::Timestamp(DateTime::from(Utc.timestamp_millis(0)));
1✔
799
        assert!(field.to_uint().is_none());
1✔
800
        assert!(field.to_int().is_none());
1✔
801
        assert!(field.to_float().is_none());
1✔
802
        assert!(field.to_boolean().is_none());
1✔
803
        assert!(field.to_string().is_some());
1✔
804
        assert!(field.to_text().is_some());
1✔
805
        assert!(field.to_binary().is_none());
1✔
806
        assert!(field.to_decimal().is_none());
1✔
807
        assert!(field.to_timestamp().is_some());
1✔
808
        assert!(field.to_date().is_none());
1✔
809
        assert!(field.to_bson().is_none());
1✔
810
        assert!(field.to_null().is_none());
1✔
811

×
812
        let field = Field::Date(NaiveDate::from_ymd(1970, 1, 1));
1✔
813
        assert!(field.to_uint().is_none());
1✔
814
        assert!(field.to_int().is_none());
1✔
815
        assert!(field.to_float().is_none());
1✔
816
        assert!(field.to_boolean().is_none());
1✔
817
        assert!(field.to_string().is_some());
1✔
818
        assert!(field.to_text().is_some());
1✔
819
        assert!(field.to_binary().is_none());
1✔
820
        assert!(field.to_decimal().is_none());
1✔
821
        assert!(field.to_timestamp().is_none());
1✔
822
        assert!(field.to_date().is_some());
1✔
823
        assert!(field.to_bson().is_none());
1✔
824
        assert!(field.to_null().is_none());
1✔
825

×
826
        let field = Field::Bson(vec![]);
1✔
827
        assert!(field.to_uint().is_none());
1✔
828
        assert!(field.to_int().is_none());
1✔
829
        assert!(field.to_float().is_none());
1✔
830
        assert!(field.to_boolean().is_none());
1✔
831
        assert!(field.to_string().is_none());
1✔
832
        assert!(field.to_text().is_none());
1✔
833
        assert!(field.to_binary().is_none());
1✔
834
        assert!(field.to_decimal().is_none());
1✔
835
        assert!(field.to_timestamp().is_none());
1✔
836
        assert!(field.to_date().is_none());
1✔
837
        assert!(field.to_bson().is_some());
1✔
838
        assert!(field.to_null().is_none());
1✔
839

×
840
        let field = Field::Null;
1✔
841
        assert!(field.to_uint().is_some());
1✔
842
        assert!(field.to_int().is_some());
1✔
843
        assert!(field.to_float().is_some());
1✔
844
        assert!(field.to_boolean().is_some());
1✔
845
        assert!(field.to_string().is_some());
1✔
846
        assert!(field.to_text().is_some());
1✔
847
        assert!(field.to_binary().is_none());
1✔
848
        assert!(field.to_decimal().is_some());
1✔
849
        assert!(field.to_timestamp().is_some());
1✔
850
        assert!(field.to_date().is_some());
1✔
851
        assert!(field.to_bson().is_none());
1✔
852
        assert!(field.to_null().is_some());
1✔
853
    }
1✔
854
}
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

© 2026 Coveralls, Inc