2121
2222//! BSON definition
2323
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+ } ;
2628
27- use chrono:: offset:: TimeZone ;
28- use chrono:: { DateTime , Timelike , Utc } ;
29+ use chrono:: { offset:: TimeZone , DateTime , Timelike , Utc } ;
2930use hex;
30- use serde_json:: { Value , json } ;
31+ use serde_json:: { json , Value } ;
3132
3233#[ cfg( feature = "decimal128" ) ]
3334use 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+ } ;
3740
3841/// Possible BSON value types.
3942#[ derive( Clone , PartialEq ) ]
@@ -50,11 +53,12 @@ pub enum Bson {
5053 Boolean ( bool ) ,
5154 /// Null value
5255 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.
5862 RegExp ( String , String ) ,
5963 /// JavaScript code
6064 JavaScriptCode ( String ) ,
@@ -112,7 +116,9 @@ impl Debug for Bson {
112116
113117 write ! ( f, "TimeStamp({}, {})" , time, inc)
114118 }
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+ }
116122 Bson :: ObjectId ( ref id) => write ! ( f, "ObjectId({:?})" , id) ,
117123 Bson :: UtcDatetime ( date_time) => write ! ( f, "UtcDatetime({:?})" , date_time) ,
118124 Bson :: Symbol ( ref sym) => write ! ( f, "Symbol({:?})" , sym) ,
@@ -146,7 +152,9 @@ impl Display for Bson {
146152 Bson :: Boolean ( b) => write ! ( fmt, "{}" , b) ,
147153 Bson :: Null => write ! ( fmt, "null" ) ,
148154 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+ }
150158 Bson :: I32 ( i) => write ! ( fmt, "{}" , i) ,
151159 Bson :: I64 ( i) => write ! ( fmt, "{}" , i) ,
152160 Bson :: TimeStamp ( i) => {
@@ -155,7 +163,9 @@ impl Display for Bson {
155163
156164 write ! ( fmt, "Timestamp({}, {})" , time, inc)
157165 }
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+ }
159169 Bson :: ObjectId ( ref id) => write ! ( fmt, "ObjectId(\" {}\" )" , id) ,
160170 Bson :: UtcDatetime ( date_time) => write ! ( fmt, "Date(\" {}\" )" , date_time) ,
161171 Bson :: Symbol ( ref sym) => write ! ( fmt, "Symbol(\" {}\" )" , sym) ,
@@ -221,7 +231,7 @@ impl From<(BinarySubtype, Vec<u8>)> for Bson {
221231
222232impl < T > From < & T > for Bson
223233where
224- T : Clone + Into < Bson >
234+ T : Clone + Into < Bson > ,
225235{
226236 fn from ( t : & T ) -> Bson {
227237 t. clone ( ) . into ( )
@@ -230,7 +240,7 @@ where
230240
231241impl < T > From < Vec < T > > for Bson
232242where
233- T : Into < Bson >
243+ T : Into < Bson > ,
234244{
235245 fn from ( v : Vec < T > ) -> Bson {
236246 Bson :: Array ( v. into_iter ( ) . map ( |val| val. into ( ) ) . collect ( ) )
@@ -239,7 +249,7 @@ where
239249
240250impl < T > From < & [ T ] > for Bson
241251where
242- T : Clone + Into < Bson >
252+ T : Clone + Into < Bson > ,
243253{
244254 fn from ( s : & [ T ] ) -> Bson {
245255 Bson :: Array ( s. into_iter ( ) . cloned ( ) . map ( |val| val. into ( ) ) . collect ( ) )
@@ -307,15 +317,18 @@ impl From<DateTime<Utc>> for Bson {
307317impl From < Value > for Bson {
308318 fn from ( a : Value ) -> Bson {
309319 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) ) ,
315326 Value :: String ( x) => x. into ( ) ,
316327 Value :: Bool ( x) => x. into ( ) ,
317328 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+ }
319332 Value :: Null => Bson :: Null ,
320333 }
321334 }
@@ -489,7 +502,9 @@ impl Bson {
489502 if values. len ( ) == 2 {
490503 if let ( Ok ( pat) , Ok ( opt) ) = ( values. get_str ( "$regex" ) , values. get_str ( "$options" ) ) {
491504 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+ {
493508 return Bson :: JavaScriptCodeWithScope ( code. to_owned ( ) , scope. to_owned ( ) ) ;
494509 } else if let ( Ok ( t) , Ok ( i) ) = ( values. get_i32 ( "t" ) , values. get_i32 ( "i" ) ) {
495510 let timestamp = ( ( t as i64 ) << 32 ) + ( i as i64 ) ;
@@ -499,17 +514,24 @@ impl Bson {
499514 return Bson :: TimeStamp ( timestamp) ;
500515 } else if let ( Ok ( hex) , Ok ( t) ) = ( values. get_str ( "$binary" ) , values. get_i64 ( "type" ) ) {
501516 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+ ) ;
503522 }
504523 } else if values. len ( ) == 1 {
505524 if let Ok ( code) = values. get_str ( "$code" ) {
506525 return Bson :: JavaScriptCode ( code. to_owned ( ) ) ;
507526 } else if let Ok ( hex) = values. get_str ( "$oid" ) {
508527 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" ) )
511531 {
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+ ) ;
513535 } else if let Ok ( sym) = values. get_str ( "$symbol" ) {
514536 return Bson :: Symbol ( sym. to_owned ( ) ) ;
515537 } else if let Ok ( dec) = values. get_str ( "$numberDecimal" ) {
@@ -528,7 +550,9 @@ impl Bson {
528550 if values. len ( ) == 2 {
529551 if let ( Ok ( pat) , Ok ( opt) ) = ( values. get_str ( "$regex" ) , values. get_str ( "$options" ) ) {
530552 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+ {
532556 return Bson :: JavaScriptCodeWithScope ( code. to_owned ( ) , scope. to_owned ( ) ) ;
533557 } else if let ( Ok ( t) , Ok ( i) ) = ( values. get_i32 ( "t" ) , values. get_i32 ( "i" ) ) {
534558 let timestamp = ( ( t as i64 ) << 32 ) + ( i as i64 ) ;
@@ -538,17 +562,24 @@ impl Bson {
538562 return Bson :: TimeStamp ( timestamp) ;
539563 } else if let ( Ok ( hex) , Ok ( t) ) = ( values. get_str ( "$binary" ) , values. get_i64 ( "type" ) ) {
540564 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+ ) ;
542570 }
543571 } else if values. len ( ) == 1 {
544572 if let Ok ( code) = values. get_str ( "$code" ) {
545573 return Bson :: JavaScriptCode ( code. to_owned ( ) ) ;
546574 } else if let Ok ( hex) = values. get_str ( "$oid" ) {
547575 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" ) )
550579 {
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+ ) ;
552583 } else if let Ok ( sym) = values. get_str ( "$symbol" ) {
553584 return Bson :: Symbol ( sym. to_owned ( ) ) ;
554585 }
@@ -568,7 +599,8 @@ impl Bson {
568599 }
569600 }
570601
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
572604 pub fn as_f64_mut ( & mut self ) -> Option < & mut f64 > {
573605 match * self {
574606 Bson :: FloatingPoint ( ref mut v) => Some ( v) ,
@@ -696,7 +728,8 @@ impl Bson {
696728 }
697729 }
698730
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
700733 pub fn as_utc_date_time_mut ( & mut self ) -> Option < & mut DateTime < Utc > > {
701734 match * self {
702735 Bson :: UtcDatetime ( ref mut v) => Some ( v) ,
0 commit comments