21
21
22
22
//! BSON definition
23
23
24
- use std:: fmt:: { self , Debug , Display } ;
25
- use std:: ops:: { Deref , DerefMut } ;
24
+ use std:: {
25
+ fmt:: { self , Debug , Display } ,
26
+ ops:: { Deref , DerefMut } ,
27
+ } ;
26
28
27
- use chrono:: offset:: TimeZone ;
28
- use chrono:: { DateTime , Timelike , Utc } ;
29
+ use chrono:: { offset:: TimeZone , DateTime , Timelike , Utc } ;
29
30
use hex;
30
- use serde_json:: { Value , json } ;
31
+ use serde_json:: { json , Value } ;
31
32
32
33
#[ cfg( feature = "decimal128" ) ]
33
34
use crate :: decimal128:: Decimal128 ;
34
- use crate :: oid;
35
- use crate :: ordered:: OrderedDocument ;
36
- use crate :: spec:: { BinarySubtype , ElementType } ;
35
+ use crate :: {
36
+ oid,
37
+ ordered:: OrderedDocument ,
38
+ spec:: { BinarySubtype , ElementType } ,
39
+ } ;
37
40
38
41
/// Possible BSON value types.
39
42
#[ derive( Clone , PartialEq ) ]
@@ -50,11 +53,12 @@ pub enum Bson {
50
53
Boolean ( bool ) ,
51
54
/// Null value
52
55
Null ,
53
- /// Regular expression - The first cstring is the regex pattern, the second is the regex options string.
54
- /// Options are identified by characters, which must be stored in alphabetical order.
55
- /// Valid options are 'i' for case insensitive matching, 'm' for multiline matching, 'x' for verbose mode,
56
- /// 'l' to make \w, \W, etc. locale dependent, 's' for dotall mode ('.' matches everything), and 'u' to
57
- /// make \w, \W, etc. match unicode.
56
+ /// Regular expression - The first cstring is the regex pattern, the second is the regex
57
+ /// options string. Options are identified by characters, which must be stored in
58
+ /// alphabetical order. Valid options are 'i' for case insensitive matching, 'm' for
59
+ /// multiline matching, 'x' for verbose mode, 'l' to make \w, \W, etc. locale dependent,
60
+ /// 's' for dotall mode ('.' matches everything), and 'u' to make \w, \W, etc. match
61
+ /// unicode.
58
62
RegExp ( String , String ) ,
59
63
/// JavaScript code
60
64
JavaScriptCode ( String ) ,
@@ -112,7 +116,9 @@ impl Debug for Bson {
112
116
113
117
write ! ( f, "TimeStamp({}, {})" , time, inc)
114
118
}
115
- Bson :: Binary ( t, ref vec) => write ! ( f, "BinData({}, 0x{})" , u8 :: from( t) , hex:: encode( vec) ) ,
119
+ Bson :: Binary ( t, ref vec) => {
120
+ write ! ( f, "BinData({}, 0x{})" , u8 :: from( t) , hex:: encode( vec) )
121
+ }
116
122
Bson :: ObjectId ( ref id) => write ! ( f, "ObjectId({:?})" , id) ,
117
123
Bson :: UtcDatetime ( date_time) => write ! ( f, "UtcDatetime({:?})" , date_time) ,
118
124
Bson :: Symbol ( ref sym) => write ! ( f, "Symbol({:?})" , sym) ,
@@ -146,7 +152,9 @@ impl Display for Bson {
146
152
Bson :: Boolean ( b) => write ! ( fmt, "{}" , b) ,
147
153
Bson :: Null => write ! ( fmt, "null" ) ,
148
154
Bson :: RegExp ( ref pat, ref opt) => write ! ( fmt, "/{}/{}" , pat, opt) ,
149
- Bson :: JavaScriptCode ( ref s) | Bson :: JavaScriptCodeWithScope ( ref s, _) => fmt. write_str ( & s) ,
155
+ Bson :: JavaScriptCode ( ref s) | Bson :: JavaScriptCodeWithScope ( ref s, _) => {
156
+ fmt. write_str ( & s)
157
+ }
150
158
Bson :: I32 ( i) => write ! ( fmt, "{}" , i) ,
151
159
Bson :: I64 ( i) => write ! ( fmt, "{}" , i) ,
152
160
Bson :: TimeStamp ( i) => {
@@ -155,7 +163,9 @@ impl Display for Bson {
155
163
156
164
write ! ( fmt, "Timestamp({}, {})" , time, inc)
157
165
}
158
- Bson :: Binary ( t, ref vec) => write ! ( fmt, "BinData({}, 0x{})" , u8 :: from( t) , hex:: encode( vec) ) ,
166
+ Bson :: Binary ( t, ref vec) => {
167
+ write ! ( fmt, "BinData({}, 0x{})" , u8 :: from( t) , hex:: encode( vec) )
168
+ }
159
169
Bson :: ObjectId ( ref id) => write ! ( fmt, "ObjectId(\" {}\" )" , id) ,
160
170
Bson :: UtcDatetime ( date_time) => write ! ( fmt, "Date(\" {}\" )" , date_time) ,
161
171
Bson :: Symbol ( ref sym) => write ! ( fmt, "Symbol(\" {}\" )" , sym) ,
@@ -221,7 +231,7 @@ impl From<(BinarySubtype, Vec<u8>)> for Bson {
221
231
222
232
impl < T > From < & T > for Bson
223
233
where
224
- T : Clone + Into < Bson >
234
+ T : Clone + Into < Bson > ,
225
235
{
226
236
fn from ( t : & T ) -> Bson {
227
237
t. clone ( ) . into ( )
@@ -230,7 +240,7 @@ where
230
240
231
241
impl < T > From < Vec < T > > for Bson
232
242
where
233
- T : Into < Bson >
243
+ T : Into < Bson > ,
234
244
{
235
245
fn from ( v : Vec < T > ) -> Bson {
236
246
Bson :: Array ( v. into_iter ( ) . map ( |val| val. into ( ) ) . collect ( ) )
@@ -239,7 +249,7 @@ where
239
249
240
250
impl < T > From < & [ T ] > for Bson
241
251
where
242
- T : Clone + Into < Bson >
252
+ T : Clone + Into < Bson > ,
243
253
{
244
254
fn from ( s : & [ T ] ) -> Bson {
245
255
Bson :: Array ( s. into_iter ( ) . cloned ( ) . map ( |val| val. into ( ) ) . collect ( ) )
@@ -307,15 +317,18 @@ impl From<DateTime<Utc>> for Bson {
307
317
impl From < Value > for Bson {
308
318
fn from ( a : Value ) -> Bson {
309
319
match a {
310
- Value :: Number ( x) => x. as_i64 ( )
311
- . map ( Bson :: from)
312
- . or_else ( || x. as_u64 ( ) . map ( Bson :: from) )
313
- . or_else ( || x. as_f64 ( ) . map ( Bson :: from) )
314
- . unwrap_or_else ( || panic ! ( "Invalid number value: {}" , x) ) ,
320
+ Value :: Number ( x) => x
321
+ . as_i64 ( )
322
+ . map ( Bson :: from)
323
+ . or_else ( || x. as_u64 ( ) . map ( Bson :: from) )
324
+ . or_else ( || x. as_f64 ( ) . map ( Bson :: from) )
325
+ . unwrap_or_else ( || panic ! ( "Invalid number value: {}" , x) ) ,
315
326
Value :: String ( x) => x. into ( ) ,
316
327
Value :: Bool ( x) => x. into ( ) ,
317
328
Value :: Array ( x) => Bson :: Array ( x. into_iter ( ) . map ( Bson :: from) . collect ( ) ) ,
318
- Value :: Object ( x) => Bson :: from_extended_document ( x. into_iter ( ) . map ( |( k, v) | ( k, v. into ( ) ) ) . collect ( ) ) ,
329
+ Value :: Object ( x) => {
330
+ Bson :: from_extended_document ( x. into_iter ( ) . map ( |( k, v) | ( k, v. into ( ) ) ) . collect ( ) )
331
+ }
319
332
Value :: Null => Bson :: Null ,
320
333
}
321
334
}
@@ -489,7 +502,9 @@ impl Bson {
489
502
if values. len ( ) == 2 {
490
503
if let ( Ok ( pat) , Ok ( opt) ) = ( values. get_str ( "$regex" ) , values. get_str ( "$options" ) ) {
491
504
return Bson :: RegExp ( pat. to_owned ( ) , opt. to_owned ( ) ) ;
492
- } else if let ( Ok ( code) , Ok ( scope) ) = ( values. get_str ( "$code" ) , values. get_document ( "$scope" ) ) {
505
+ } else if let ( Ok ( code) , Ok ( scope) ) =
506
+ ( values. get_str ( "$code" ) , values. get_document ( "$scope" ) )
507
+ {
493
508
return Bson :: JavaScriptCodeWithScope ( code. to_owned ( ) , scope. to_owned ( ) ) ;
494
509
} else if let ( Ok ( t) , Ok ( i) ) = ( values. get_i32 ( "t" ) , values. get_i32 ( "i" ) ) {
495
510
let timestamp = ( ( t as i64 ) << 32 ) + ( i as i64 ) ;
@@ -499,17 +514,24 @@ impl Bson {
499
514
return Bson :: TimeStamp ( timestamp) ;
500
515
} else if let ( Ok ( hex) , Ok ( t) ) = ( values. get_str ( "$binary" ) , values. get_i64 ( "type" ) ) {
501
516
let ttype = t as u8 ;
502
- return Bson :: Binary ( From :: from ( ttype) , hex:: decode ( hex. as_bytes ( ) ) . expect ( "$binary value is not a valid Hex encoded bytes" ) ) ;
517
+ return Bson :: Binary (
518
+ From :: from ( ttype) ,
519
+ hex:: decode ( hex. as_bytes ( ) )
520
+ . expect ( "$binary value is not a valid Hex encoded bytes" ) ,
521
+ ) ;
503
522
}
504
523
} else if values. len ( ) == 1 {
505
524
if let Ok ( code) = values. get_str ( "$code" ) {
506
525
return Bson :: JavaScriptCode ( code. to_owned ( ) ) ;
507
526
} else if let Ok ( hex) = values. get_str ( "$oid" ) {
508
527
return Bson :: ObjectId ( oid:: ObjectId :: with_string ( hex) . unwrap ( ) ) ;
509
- } else if let Ok ( long) = values. get_document ( "$date" )
510
- . and_then ( |inner| inner. get_i64 ( "$numberLong" ) )
528
+ } else if let Ok ( long) = values
529
+ . get_document ( "$date" )
530
+ . and_then ( |inner| inner. get_i64 ( "$numberLong" ) )
511
531
{
512
- return Bson :: UtcDatetime ( Utc . timestamp ( long / 1000 , ( ( long % 1000 ) * 1_000_000 ) as u32 ) ) ;
532
+ return Bson :: UtcDatetime (
533
+ Utc . timestamp ( long / 1000 , ( ( long % 1000 ) * 1_000_000 ) as u32 ) ,
534
+ ) ;
513
535
} else if let Ok ( sym) = values. get_str ( "$symbol" ) {
514
536
return Bson :: Symbol ( sym. to_owned ( ) ) ;
515
537
} else if let Ok ( dec) = values. get_str ( "$numberDecimal" ) {
@@ -528,7 +550,9 @@ impl Bson {
528
550
if values. len ( ) == 2 {
529
551
if let ( Ok ( pat) , Ok ( opt) ) = ( values. get_str ( "$regex" ) , values. get_str ( "$options" ) ) {
530
552
return Bson :: RegExp ( pat. to_owned ( ) , opt. to_owned ( ) ) ;
531
- } else if let ( Ok ( code) , Ok ( scope) ) = ( values. get_str ( "$code" ) , values. get_document ( "$scope" ) ) {
553
+ } else if let ( Ok ( code) , Ok ( scope) ) =
554
+ ( values. get_str ( "$code" ) , values. get_document ( "$scope" ) )
555
+ {
532
556
return Bson :: JavaScriptCodeWithScope ( code. to_owned ( ) , scope. to_owned ( ) ) ;
533
557
} else if let ( Ok ( t) , Ok ( i) ) = ( values. get_i32 ( "t" ) , values. get_i32 ( "i" ) ) {
534
558
let timestamp = ( ( t as i64 ) << 32 ) + ( i as i64 ) ;
@@ -538,17 +562,24 @@ impl Bson {
538
562
return Bson :: TimeStamp ( timestamp) ;
539
563
} else if let ( Ok ( hex) , Ok ( t) ) = ( values. get_str ( "$binary" ) , values. get_i64 ( "type" ) ) {
540
564
let ttype = t as u8 ;
541
- return Bson :: Binary ( From :: from ( ttype) , hex:: decode ( hex. as_bytes ( ) ) . expect ( "$binary value is not a valid Hex encoded bytes" ) ) ;
565
+ return Bson :: Binary (
566
+ From :: from ( ttype) ,
567
+ hex:: decode ( hex. as_bytes ( ) )
568
+ . expect ( "$binary value is not a valid Hex encoded bytes" ) ,
569
+ ) ;
542
570
}
543
571
} else if values. len ( ) == 1 {
544
572
if let Ok ( code) = values. get_str ( "$code" ) {
545
573
return Bson :: JavaScriptCode ( code. to_owned ( ) ) ;
546
574
} else if let Ok ( hex) = values. get_str ( "$oid" ) {
547
575
return Bson :: ObjectId ( oid:: ObjectId :: with_string ( hex) . unwrap ( ) ) ;
548
- } else if let Ok ( long) = values. get_document ( "$date" )
549
- . and_then ( |inner| inner. get_i64 ( "$numberLong" ) )
576
+ } else if let Ok ( long) = values
577
+ . get_document ( "$date" )
578
+ . and_then ( |inner| inner. get_i64 ( "$numberLong" ) )
550
579
{
551
- return Bson :: UtcDatetime ( Utc . timestamp ( long / 1000 , ( ( long % 1000 ) * 1_000_000 ) as u32 ) ) ;
580
+ return Bson :: UtcDatetime (
581
+ Utc . timestamp ( long / 1000 , ( ( long % 1000 ) * 1_000_000 ) as u32 ) ,
582
+ ) ;
552
583
} else if let Ok ( sym) = values. get_str ( "$symbol" ) {
553
584
return Bson :: Symbol ( sym. to_owned ( ) ) ;
554
585
}
@@ -568,7 +599,8 @@ impl Bson {
568
599
}
569
600
}
570
601
571
- /// If `Bson` is `FloatingPoint`, return a mutable reference to its value. Returns `None` otherwise
602
+ /// If `Bson` is `FloatingPoint`, return a mutable reference to its value. Returns `None`
603
+ /// otherwise
572
604
pub fn as_f64_mut ( & mut self ) -> Option < & mut f64 > {
573
605
match * self {
574
606
Bson :: FloatingPoint ( ref mut v) => Some ( v) ,
@@ -696,7 +728,8 @@ impl Bson {
696
728
}
697
729
}
698
730
699
- /// If `Bson` is `UtcDateTime`, return a mutable reference to its value. Returns `None` otherwise
731
+ /// If `Bson` is `UtcDateTime`, return a mutable reference to its value. Returns `None`
732
+ /// otherwise
700
733
pub fn as_utc_date_time_mut ( & mut self ) -> Option < & mut DateTime < Utc > > {
701
734
match * self {
702
735
Bson :: UtcDatetime ( ref mut v) => Some ( v) ,
0 commit comments