2121
2222//! BSON definition
2323
24- use std:: fmt:: { self , Display , Debug } ;
24+ use std:: fmt:: { self , Debug , Display } ;
2525use std:: ops:: { Deref , DerefMut } ;
2626
2727use chrono:: { DateTime , Timelike , Utc } ;
2828use chrono:: offset:: TimeZone ;
29- use serde_json:: Value ;
3029use hex:: { FromHex , ToHex } ;
30+ use serde_json:: Value ;
3131
3232use oid;
3333use ordered:: OrderedDocument ;
34- use spec:: { ElementType , BinarySubtype } ;
34+ use spec:: { BinarySubtype , ElementType } ;
3535
3636/// Possible BSON value types.
3737#[ derive( Clone , PartialEq ) ]
@@ -121,15 +121,15 @@ impl Display for Bson {
121121 & Bson :: FloatingPoint ( f) => write ! ( fmt, "{}" , f) ,
122122 & Bson :: String ( ref s) => write ! ( fmt, "\" {}\" " , s) ,
123123 & Bson :: Array ( ref vec) => {
124- try! ( write ! ( fmt, "[" ) ) ;
124+ write ! ( fmt, "[" ) ? ;
125125
126126 let mut first = true ;
127127 for bson in vec. iter ( ) {
128128 if !first {
129- try! ( write ! ( fmt, ", " ) ) ;
129+ write ! ( fmt, ", " ) ? ;
130130 }
131131
132- try! ( write ! ( fmt, "{}" , bson) ) ;
132+ write ! ( fmt, "{}" , bson) ? ;
133133 first = false ;
134134 }
135135
@@ -139,8 +139,7 @@ impl Display for Bson {
139139 & Bson :: Boolean ( b) => write ! ( fmt, "{}" , b) ,
140140 & Bson :: Null => write ! ( fmt, "null" ) ,
141141 & Bson :: RegExp ( ref pat, ref opt) => write ! ( fmt, "/{}/{}" , pat, opt) ,
142- & Bson :: JavaScriptCode ( ref s) |
143- & Bson :: JavaScriptCodeWithScope ( ref s, _) => fmt. write_str ( & s) ,
142+ & Bson :: JavaScriptCode ( ref s) | & Bson :: JavaScriptCodeWithScope ( ref s, _) => fmt. write_str ( & s) ,
144143 & Bson :: I32 ( i) => write ! ( fmt, "{}" , i) ,
145144 & Bson :: I64 ( i) => write ! ( fmt, "{}" , i) ,
146145 & Bson :: TimeStamp ( i) => {
@@ -149,9 +148,7 @@ impl Display for Bson {
149148
150149 write ! ( fmt, "Timestamp({}, {})" , time, inc)
151150 }
152- & Bson :: Binary ( t, ref vec) => {
153- write ! ( fmt, "BinData({}, 0x{})" , u8 :: from( t) , vec. to_hex( ) )
154- }
151+ & Bson :: Binary ( t, ref vec) => write ! ( fmt, "BinData({}, 0x{})" , u8 :: from( t) , vec. to_hex( ) ) ,
155152 & Bson :: ObjectId ( ref id) => write ! ( fmt, "ObjectId(\" {}\" )" , id) ,
156153 & Bson :: UtcDatetime ( date_time) => write ! ( fmt, "Date(\" {}\" )" , date_time) ,
157154 & Bson :: Symbol ( ref sym) => write ! ( fmt, "Symbol(\" {}\" )" , sym) ,
@@ -274,19 +271,16 @@ impl From<Value> for Bson {
274271 fn from ( a : Value ) -> Bson {
275272 match a {
276273 Value :: Number ( x) => {
277- x. as_i64 ( )
278- . map ( Bson :: from)
279- . or_else ( || x. as_u64 ( ) . map ( Bson :: from) )
280- . or_else ( || x. as_f64 ( ) . map ( Bson :: from) )
281- . unwrap_or_else ( || panic ! ( "Invalid number value: {}" , x) )
274+ x. as_i64 ( ) . map ( Bson :: from)
275+ . or_else ( || x. as_u64 ( ) . map ( Bson :: from) )
276+ . or_else ( || x. as_f64 ( ) . map ( Bson :: from) )
277+ . unwrap_or_else ( || panic ! ( "Invalid number value: {}" , x) )
282278 }
283279 Value :: String ( x) => x. into ( ) ,
284280 Value :: Bool ( x) => x. into ( ) ,
285281 Value :: Array ( x) => Bson :: Array ( x. into_iter ( ) . map ( Bson :: from) . collect ( ) ) ,
286282 Value :: Object ( x) => {
287- Bson :: from_extended_document ( x. into_iter ( )
288- . map ( |( k, v) | ( k. clone ( ) , v. into ( ) ) )
289- . collect ( ) )
283+ Bson :: from_extended_document ( x. into_iter ( ) . map ( |( k, v) | ( k. clone ( ) , v. into ( ) ) ) . collect ( ) )
290284 }
291285 Value :: Null => Bson :: Null ,
292286 }
@@ -308,7 +302,7 @@ impl Into<Value> for Bson {
308302 "$options" : opt
309303 } )
310304 }
311- Bson :: JavaScriptCode ( code) => json ! ( { "$code" : code} ) ,
305+ Bson :: JavaScriptCode ( code) => json ! ( { "$code" : code } ) ,
312306 Bson :: JavaScriptCodeWithScope ( code, scope) => {
313307 json ! ( {
314308 "$code" : code,
@@ -341,7 +335,7 @@ impl Into<Value> for Bson {
341335 } )
342336 }
343337 // FIXME: Don't know what is the best way to encode Symbol type
344- Bson :: Symbol ( v) => json ! ( { "$symbol" : v} ) ,
338+ Bson :: Symbol ( v) => json ! ( { "$symbol" : v } ) ,
345339 }
346340 }
347341}
@@ -371,21 +365,21 @@ impl Bson {
371365
372366 /// Clones the bson and returns the representative serde_json Value.
373367 /// The json will be in [extended JSON format](https://docs.mongodb.com/manual/reference/mongodb-extended-json/).
374- #[ deprecated( since= "0.5.1" , note= "use bson.clone().into() instead" ) ]
368+ #[ deprecated( since = "0.5.1" , note = "use bson.clone().into() instead" ) ]
375369 pub fn to_json ( & self ) -> Value {
376370 self . clone ( ) . into ( )
377371 }
378372
379373 /// Consumes the bson and returns the representative serde_json Value.
380374 /// The json will be in [extended JSON format](https://docs.mongodb.com/manual/reference/mongodb-extended-json/).
381- #[ deprecated( since= "0.5.1" , note= "use bson.into() instead" ) ]
375+ #[ deprecated( since = "0.5.1" , note = "use bson.into() instead" ) ]
382376 pub fn into_json ( self ) -> Value {
383377 self . into ( )
384378 }
385379
386380 /// Consumes the serde_json Value and returns the representative bson.
387381 /// The json should be in [extended JSON format](https://docs.mongodb.com/manual/reference/mongodb-extended-json/).
388- #[ deprecated( since= "0.5.1" , note= "use json.into() instead" ) ]
382+ #[ deprecated( since = "0.5.1" , note = "use json.into() instead" ) ]
389383 pub fn from_json ( val : Value ) -> Bson {
390384 val. into ( )
391385 }
@@ -456,37 +450,28 @@ impl Bson {
456450 if values. len ( ) == 2 {
457451 if let ( Ok ( pat) , Ok ( opt) ) = ( values. get_str ( "$regex" ) , values. get_str ( "$options" ) ) {
458452 return Bson :: RegExp ( pat. to_owned ( ) , opt. to_owned ( ) ) ;
459-
460- } else if let ( Ok ( code) , Ok ( scope) ) =
461- ( values. get_str ( "$code" ) , values. get_document ( "$scope" ) ) {
453+ } else if let ( Ok ( code) , Ok ( scope) ) = ( values. get_str ( "$code" ) , values. get_document ( "$scope" ) ) {
462454 return Bson :: JavaScriptCodeWithScope ( code. to_owned ( ) , scope. to_owned ( ) ) ;
463-
464455 } else if let ( Ok ( t) , Ok ( i) ) = ( values. get_i32 ( "t" ) , values. get_i32 ( "i" ) ) {
465456 let timestamp = ( ( t as i64 ) << 32 ) + ( i as i64 ) ;
466457 return Bson :: TimeStamp ( timestamp) ;
467-
468458 } else if let ( Ok ( t) , Ok ( i) ) = ( values. get_i64 ( "t" ) , values. get_i64 ( "i" ) ) {
469459 let timestamp = ( t << 32 ) + i;
470460 return Bson :: TimeStamp ( timestamp) ;
471-
472461 } else if let ( Ok ( hex) , Ok ( t) ) = ( values. get_str ( "$binary" ) , values. get_i64 ( "type" ) ) {
473462 let ttype = t as u8 ;
474463 return Bson :: Binary ( From :: from ( ttype) ,
475464 FromHex :: from_hex ( hex. as_bytes ( ) ) . unwrap ( ) ) ;
476465 }
477-
478466 } else if values. len ( ) == 1 {
479467 if let Ok ( code) = values. get_str ( "$code" ) {
480468 return Bson :: JavaScriptCode ( code. to_owned ( ) ) ;
481-
482469 } else if let Ok ( hex) = values. get_str ( "$oid" ) {
483470 return Bson :: ObjectId ( oid:: ObjectId :: with_string ( hex) . unwrap ( ) ) ;
484-
485- } else if let Ok ( long) = values
486- . get_document ( "$date" )
487- . and_then ( |inner| inner. get_i64 ( "$numberLong" ) ) {
488- return Bson :: UtcDatetime ( Utc . timestamp ( long / 1000 ,
489- ( ( long % 1000 ) * 1000000 ) as u32 ) ) ;
471+ } else if let Ok ( long) = values. get_document ( "$date" )
472+ . and_then ( |inner| inner. get_i64 ( "$numberLong" ) )
473+ {
474+ return Bson :: UtcDatetime ( Utc . timestamp ( long / 1000 , ( ( long % 1000 ) * 1000000 ) as u32 ) ) ;
490475 } else if let Ok ( sym) = values. get_str ( "$symbol" ) {
491476 return Bson :: Symbol ( sym. to_owned ( ) ) ;
492477 }
0 commit comments