@@ -245,15 +245,19 @@ impl Bson {
245
245
& Bson :: Null => json:: Json :: Null ,
246
246
& Bson :: RegExp ( ref pat, ref opt) => {
247
247
let mut re = json:: Object :: new ( ) ;
248
- re. insert ( "pattern " . to_owned ( ) , json:: Json :: String ( pat. clone ( ) ) ) ;
249
- re. insert ( "options" . to_owned ( ) , json:: Json :: String ( opt. clone ( ) ) ) ;
248
+ re. insert ( "$regex " . to_owned ( ) , json:: Json :: String ( pat. clone ( ) ) ) ;
249
+ re. insert ( "$ options" . to_owned ( ) , json:: Json :: String ( opt. clone ( ) ) ) ;
250
250
251
251
json:: Json :: Object ( re)
252
252
} ,
253
- & Bson :: JavaScriptCode ( ref code) => json:: Json :: String ( code. clone ( ) ) ,
253
+ & Bson :: JavaScriptCode ( ref code) => {
254
+ let mut obj = json:: Object :: new ( ) ;
255
+ obj. insert ( "$code" . to_owned ( ) , json:: Json :: String ( code. clone ( ) ) ) ;
256
+ json:: Json :: Object ( obj)
257
+ } ,
254
258
& Bson :: JavaScriptCodeWithScope ( ref code, ref scope) => {
255
259
let mut obj = json:: Object :: new ( ) ;
256
- obj. insert ( "code" . to_owned ( ) , json:: Json :: String ( code. clone ( ) ) ) ;
260
+ obj. insert ( "$ code" . to_owned ( ) , json:: Json :: String ( code. clone ( ) ) ) ;
257
261
258
262
let scope_obj =
259
263
scope. iter ( ) . map ( |( k, v) | ( k. clone ( ) , v. to_json ( ) ) ) . collect ( ) ;
@@ -264,17 +268,38 @@ impl Bson {
264
268
} ,
265
269
& Bson :: I32 ( v) => json:: Json :: I64 ( v as i64 ) ,
266
270
& Bson :: I64 ( v) => json:: Json :: I64 ( v) ,
267
- & Bson :: TimeStamp ( v) => json:: Json :: I64 ( v) ,
271
+ & Bson :: TimeStamp ( v) => {
272
+ let time = v >> 32 ;
273
+ let inc = v & 0x0000FFFF ;
274
+
275
+ let mut obj = json:: Object :: new ( ) ;
276
+ obj. insert ( "t" . to_owned ( ) , json:: Json :: I64 ( time) ) ;
277
+ obj. insert ( "i" . to_owned ( ) , json:: Json :: I64 ( inc) ) ;
278
+
279
+ json:: Json :: Object ( obj)
280
+ } ,
268
281
& Bson :: Binary ( t, ref v) => {
269
282
let mut obj = json:: Object :: new ( ) ;
270
283
let tval: u8 = From :: from ( t) ;
271
284
obj. insert ( "type" . to_owned ( ) , json:: Json :: I64 ( tval as i64 ) ) ;
272
- obj. insert ( "data " . to_owned ( ) , json:: Json :: String ( v. to_hex ( ) ) ) ;
285
+ obj. insert ( "$binary " . to_owned ( ) , json:: Json :: String ( v. to_hex ( ) ) ) ;
273
286
274
287
json:: Json :: Object ( obj)
275
288
} ,
276
- & Bson :: ObjectId ( ref v) => json:: Json :: String ( v. bytes ( ) . to_hex ( ) ) ,
277
- & Bson :: UtcDatetime ( ref v) => json:: Json :: String ( v. to_string ( ) ) ,
289
+ & Bson :: ObjectId ( ref v) => {
290
+ let mut obj = json:: Object :: new ( ) ;
291
+ obj. insert ( "$oid" . to_owned ( ) , json:: Json :: String ( v. to_string ( ) ) ) ;
292
+
293
+ json:: Json :: Object ( obj)
294
+ } ,
295
+ & Bson :: UtcDatetime ( ref v) => {
296
+ let mut obj = json:: Object :: new ( ) ;
297
+ let mut inner = json:: Object :: new ( ) ;
298
+ inner. insert ( "$numberLong" . to_owned ( ) , json:: Json :: I64 ( ( v. timestamp ( ) * 1000 ) +
299
+ ( v. nanosecond ( ) / 1000000 ) as i64 ) ) ;
300
+ obj. insert ( "$date" . to_owned ( ) , json:: Json :: Object ( inner) ) ;
301
+ json:: Json :: Object ( obj)
302
+ }
278
303
}
279
304
}
280
305
@@ -287,7 +312,8 @@ impl Bson {
287
312
& json:: Json :: String ( ref x) => Bson :: String ( x. clone ( ) ) ,
288
313
& json:: Json :: Boolean ( x) => Bson :: Boolean ( x) ,
289
314
& json:: Json :: Array ( ref x) => Bson :: Array ( x. iter ( ) . map ( Bson :: from_json) . collect ( ) ) ,
290
- & json:: Json :: Object ( ref x) => Bson :: Document ( x. iter ( ) . map ( |( k, v) | ( k. clone ( ) , Bson :: from_json ( v) ) ) . collect ( ) ) ,
315
+ & json:: Json :: Object ( ref x) => Bson :: from_extended_document (
316
+ x. iter ( ) . map ( |( k, v) | ( k. clone ( ) , Bson :: from_json ( v) ) ) . collect ( ) ) ,
291
317
& json:: Json :: Null => Bson :: Null ,
292
318
}
293
319
}
@@ -344,42 +370,48 @@ impl Bson {
344
370
}
345
371
}
346
372
347
- pub fn from_extended_document ( values : Document ) -> Result < Bson , Error > {
373
+ pub fn from_extended_document ( values : Document ) -> Bson {
348
374
if values. contains_key ( "$regex" ) {
349
375
if let Some ( & Bson :: String ( ref pat) ) = values. get ( "$regex" ) {
350
376
if let Some ( & Bson :: String ( ref opt) ) = values. get ( "$options" ) {
351
- return Ok ( Bson :: RegExp ( pat. to_owned ( ) , opt. to_owned ( ) ) ) ;
377
+ return Bson :: RegExp ( pat. to_owned ( ) , opt. to_owned ( ) ) ;
352
378
}
353
379
}
354
380
355
381
} else if let Some ( & Bson :: String ( ref code) ) = values. get ( "$code" ) {
356
382
if let Some ( & Bson :: Document ( ref scope) ) = values. get ( "$scope" ) {
357
- return Ok ( Bson :: JavaScriptCodeWithScope ( code. to_owned ( ) , scope. to_owned ( ) ) ) ;
383
+ return Bson :: JavaScriptCodeWithScope ( code. to_owned ( ) , scope. to_owned ( ) ) ;
358
384
} else {
359
- return Ok ( Bson :: JavaScriptCode ( code. to_owned ( ) ) ) ;
385
+ return Bson :: JavaScriptCode ( code. to_owned ( ) ) ;
360
386
}
361
387
362
388
} else if let Some ( & Bson :: I32 ( t) ) = values. get ( "t" ) {
363
389
if let Some ( & Bson :: I32 ( i) ) = values. get ( "i" ) {
364
390
let timestamp = ( ( t as i64 ) << 32 ) + ( i as i64 ) ;
365
- return Ok ( Bson :: TimeStamp ( timestamp) )
391
+ return Bson :: TimeStamp ( timestamp)
392
+ }
393
+
394
+ } else if let Some ( & Bson :: I64 ( t) ) = values. get ( "t" ) {
395
+ if let Some ( & Bson :: I64 ( i) ) = values. get ( "i" ) {
396
+ let timestamp = ( t << 32 ) + i;
397
+ return Bson :: TimeStamp ( timestamp)
366
398
}
367
399
368
400
} else if let Some ( & Bson :: String ( ref hex) ) = values. get ( "$binary" ) {
369
401
if let Some ( & Bson :: I64 ( t) ) = values. get ( "type" ) {
370
402
let ttype = t as u8 ;
371
- return Ok ( Bson :: Binary ( From :: from ( ttype) , hex. from_hex ( ) . unwrap ( ) ) ) ;
403
+ return Bson :: Binary ( From :: from ( ttype) , hex. from_hex ( ) . unwrap ( ) ) ;
372
404
}
373
405
374
406
} else if let Some ( & Bson :: String ( ref hex) ) = values. get ( "$oid" ) {
375
- return Ok ( Bson :: ObjectId ( oid:: ObjectId :: with_string ( hex) . unwrap ( ) ) ) ;
407
+ return Bson :: ObjectId ( oid:: ObjectId :: with_string ( hex) . unwrap ( ) ) ;
376
408
377
409
} else if let Some ( & Bson :: Document ( ref doc) ) = values. get ( "$date" ) {
378
410
if let Some ( & Bson :: I64 ( long) ) = doc. get ( "$numberLong" ) {
379
- return Ok ( Bson :: UtcDatetime ( UTC . timestamp ( long / 1000 , ( long % 1000 ) as u32 * 1000000 ) ) ) ;
411
+ return Bson :: UtcDatetime ( UTC . timestamp ( long / 1000 , ( long % 1000 ) as u32 * 1000000 ) ) ;
380
412
}
381
413
}
382
414
383
- Ok ( Bson :: Document ( values) )
415
+ Bson :: Document ( values)
384
416
}
385
417
}
0 commit comments