21
21
22
22
//! BSON definition
23
23
24
- use std:: fmt:: { self , Display , Debug } ;
24
+ use std:: fmt:: { self , Debug , Display } ;
25
25
use std:: ops:: { Deref , DerefMut } ;
26
26
27
27
use chrono:: { DateTime , Timelike , Utc } ;
28
28
use chrono:: offset:: TimeZone ;
29
- use serde_json:: Value ;
30
29
use hex:: { FromHex , ToHex } ;
30
+ use serde_json:: Value ;
31
31
32
32
use oid;
33
33
use ordered:: OrderedDocument ;
34
- use spec:: { ElementType , BinarySubtype } ;
34
+ use spec:: { BinarySubtype , ElementType } ;
35
35
36
36
/// Possible BSON value types.
37
37
#[ derive( Clone , PartialEq ) ]
@@ -121,15 +121,15 @@ impl Display for Bson {
121
121
& Bson :: FloatingPoint ( f) => write ! ( fmt, "{}" , f) ,
122
122
& Bson :: String ( ref s) => write ! ( fmt, "\" {}\" " , s) ,
123
123
& Bson :: Array ( ref vec) => {
124
- try! ( write ! ( fmt, "[" ) ) ;
124
+ write ! ( fmt, "[" ) ? ;
125
125
126
126
let mut first = true ;
127
127
for bson in vec. iter ( ) {
128
128
if !first {
129
- try! ( write ! ( fmt, ", " ) ) ;
129
+ write ! ( fmt, ", " ) ? ;
130
130
}
131
131
132
- try! ( write ! ( fmt, "{}" , bson) ) ;
132
+ write ! ( fmt, "{}" , bson) ? ;
133
133
first = false ;
134
134
}
135
135
@@ -139,8 +139,7 @@ impl Display for Bson {
139
139
& Bson :: Boolean ( b) => write ! ( fmt, "{}" , b) ,
140
140
& Bson :: Null => write ! ( fmt, "null" ) ,
141
141
& 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) ,
144
143
& Bson :: I32 ( i) => write ! ( fmt, "{}" , i) ,
145
144
& Bson :: I64 ( i) => write ! ( fmt, "{}" , i) ,
146
145
& Bson :: TimeStamp ( i) => {
@@ -149,9 +148,7 @@ impl Display for Bson {
149
148
150
149
write ! ( fmt, "Timestamp({}, {})" , time, inc)
151
150
}
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( ) ) ,
155
152
& Bson :: ObjectId ( ref id) => write ! ( fmt, "ObjectId(\" {}\" )" , id) ,
156
153
& Bson :: UtcDatetime ( date_time) => write ! ( fmt, "Date(\" {}\" )" , date_time) ,
157
154
& Bson :: Symbol ( ref sym) => write ! ( fmt, "Symbol(\" {}\" )" , sym) ,
@@ -274,19 +271,16 @@ impl From<Value> for Bson {
274
271
fn from ( a : Value ) -> Bson {
275
272
match a {
276
273
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) )
282
278
}
283
279
Value :: String ( x) => x. into ( ) ,
284
280
Value :: Bool ( x) => x. into ( ) ,
285
281
Value :: Array ( x) => Bson :: Array ( x. into_iter ( ) . map ( Bson :: from) . collect ( ) ) ,
286
282
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 ( ) )
290
284
}
291
285
Value :: Null => Bson :: Null ,
292
286
}
@@ -308,7 +302,7 @@ impl Into<Value> for Bson {
308
302
"$options" : opt
309
303
} )
310
304
}
311
- Bson :: JavaScriptCode ( code) => json ! ( { "$code" : code} ) ,
305
+ Bson :: JavaScriptCode ( code) => json ! ( { "$code" : code } ) ,
312
306
Bson :: JavaScriptCodeWithScope ( code, scope) => {
313
307
json ! ( {
314
308
"$code" : code,
@@ -341,7 +335,7 @@ impl Into<Value> for Bson {
341
335
} )
342
336
}
343
337
// 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 } ) ,
345
339
}
346
340
}
347
341
}
@@ -371,21 +365,21 @@ impl Bson {
371
365
372
366
/// Clones the bson and returns the representative serde_json Value.
373
367
/// 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" ) ]
375
369
pub fn to_json ( & self ) -> Value {
376
370
self . clone ( ) . into ( )
377
371
}
378
372
379
373
/// Consumes the bson and returns the representative serde_json Value.
380
374
/// 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" ) ]
382
376
pub fn into_json ( self ) -> Value {
383
377
self . into ( )
384
378
}
385
379
386
380
/// Consumes the serde_json Value and returns the representative bson.
387
381
/// 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" ) ]
389
383
pub fn from_json ( val : Value ) -> Bson {
390
384
val. into ( )
391
385
}
@@ -456,37 +450,28 @@ impl Bson {
456
450
if values. len ( ) == 2 {
457
451
if let ( Ok ( pat) , Ok ( opt) ) = ( values. get_str ( "$regex" ) , values. get_str ( "$options" ) ) {
458
452
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" ) ) {
462
454
return Bson :: JavaScriptCodeWithScope ( code. to_owned ( ) , scope. to_owned ( ) ) ;
463
-
464
455
} else if let ( Ok ( t) , Ok ( i) ) = ( values. get_i32 ( "t" ) , values. get_i32 ( "i" ) ) {
465
456
let timestamp = ( ( t as i64 ) << 32 ) + ( i as i64 ) ;
466
457
return Bson :: TimeStamp ( timestamp) ;
467
-
468
458
} else if let ( Ok ( t) , Ok ( i) ) = ( values. get_i64 ( "t" ) , values. get_i64 ( "i" ) ) {
469
459
let timestamp = ( t << 32 ) + i;
470
460
return Bson :: TimeStamp ( timestamp) ;
471
-
472
461
} else if let ( Ok ( hex) , Ok ( t) ) = ( values. get_str ( "$binary" ) , values. get_i64 ( "type" ) ) {
473
462
let ttype = t as u8 ;
474
463
return Bson :: Binary ( From :: from ( ttype) ,
475
464
FromHex :: from_hex ( hex. as_bytes ( ) ) . unwrap ( ) ) ;
476
465
}
477
-
478
466
} else if values. len ( ) == 1 {
479
467
if let Ok ( code) = values. get_str ( "$code" ) {
480
468
return Bson :: JavaScriptCode ( code. to_owned ( ) ) ;
481
-
482
469
} else if let Ok ( hex) = values. get_str ( "$oid" ) {
483
470
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 ) ) ;
490
475
} else if let Ok ( sym) = values. get_str ( "$symbol" ) {
491
476
return Bson :: Symbol ( sym. to_owned ( ) ) ;
492
477
}
0 commit comments