@@ -252,12 +252,12 @@ impl Document {
252
252
match self . get ( key) {
253
253
Some ( & Bson :: Double ( v) ) => Ok ( v) ,
254
254
Some ( bson) => Err ( Error :: value_access_unexpected_type (
255
- key,
256
255
bson. element_type ( ) ,
257
256
ElementType :: Double ,
258
257
) ) ,
259
- None => Err ( Error :: value_access_not_present ( key ) ) ,
258
+ None => Err ( Error :: value_access_not_present ( ) ) ,
260
259
}
260
+ . map_err ( |e| e. with_key ( key) )
261
261
}
262
262
263
263
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -267,12 +267,12 @@ impl Document {
267
267
match self . get_mut ( key) {
268
268
Some ( & mut Bson :: Double ( ref mut v) ) => Ok ( v) ,
269
269
Some ( bson) => Err ( Error :: value_access_unexpected_type (
270
- key,
271
270
bson. element_type ( ) ,
272
271
ElementType :: Double ,
273
272
) ) ,
274
- None => Err ( Error :: value_access_not_present ( key ) ) ,
273
+ None => Err ( Error :: value_access_not_present ( ) ) ,
275
274
}
275
+ . map_err ( |e| e. with_key ( key) )
276
276
}
277
277
278
278
/// Returns a reference to the value for the given key if one is present and is of type
@@ -282,12 +282,12 @@ impl Document {
282
282
match self . get ( key) {
283
283
Some ( Bson :: Decimal128 ( v) ) => Ok ( v) ,
284
284
Some ( bson) => Err ( Error :: value_access_unexpected_type (
285
- key,
286
285
bson. element_type ( ) ,
287
286
ElementType :: Decimal128 ,
288
287
) ) ,
289
- None => Err ( Error :: value_access_not_present ( key ) ) ,
288
+ None => Err ( Error :: value_access_not_present ( ) ) ,
290
289
}
290
+ . map_err ( |e| e. with_key ( key) )
291
291
}
292
292
293
293
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -297,42 +297,42 @@ impl Document {
297
297
match self . get_mut ( key) {
298
298
Some ( & mut Bson :: Decimal128 ( ref mut v) ) => Ok ( v) ,
299
299
Some ( bson) => Err ( Error :: value_access_unexpected_type (
300
- key,
301
300
bson. element_type ( ) ,
302
301
ElementType :: Decimal128 ,
303
302
) ) ,
304
- None => Err ( Error :: value_access_not_present ( key ) ) ,
303
+ None => Err ( Error :: value_access_not_present ( ) ) ,
305
304
}
305
+ . map_err ( |e| e. with_key ( key) )
306
306
}
307
307
308
308
/// Returns a reference to the value for the given key if one is present and is of type
309
309
/// [`ElementType::String`].
310
310
pub fn get_str ( & self , key : impl AsRef < str > ) -> Result < & str > {
311
311
let key = key. as_ref ( ) ;
312
312
match self . get ( key) {
313
- Some ( Bson :: String ( v) ) => Ok ( v) ,
313
+ Some ( Bson :: String ( v) ) => Ok ( v. as_str ( ) ) ,
314
314
Some ( bson) => Err ( Error :: value_access_unexpected_type (
315
- key,
316
315
bson. element_type ( ) ,
317
316
ElementType :: String ,
318
317
) ) ,
319
- None => Err ( Error :: value_access_not_present ( key ) ) ,
318
+ None => Err ( Error :: value_access_not_present ( ) ) ,
320
319
}
320
+ . map_err ( |e| e. with_key ( key) )
321
321
}
322
322
323
323
/// Returns a mutable reference to the value for the given key if one is present and is of type
324
324
/// [`ElementType::String`].
325
325
pub fn get_str_mut ( & mut self , key : impl AsRef < str > ) -> Result < & mut str > {
326
326
let key = key. as_ref ( ) ;
327
327
match self . get_mut ( key) {
328
- Some ( & mut Bson :: String ( ref mut v) ) => Ok ( v) ,
328
+ Some ( & mut Bson :: String ( ref mut v) ) => Ok ( v. as_mut_str ( ) ) ,
329
329
Some ( bson) => Err ( Error :: value_access_unexpected_type (
330
- key,
331
330
bson. element_type ( ) ,
332
331
ElementType :: String ,
333
332
) ) ,
334
- None => Err ( Error :: value_access_not_present ( key ) ) ,
333
+ None => Err ( Error :: value_access_not_present ( ) ) ,
335
334
}
335
+ . map_err ( |e| e. with_key ( key) )
336
336
}
337
337
338
338
/// Returns a reference to the value for the given key if one is present and is of type
@@ -342,12 +342,12 @@ impl Document {
342
342
match self . get ( key) {
343
343
Some ( Bson :: Array ( v) ) => Ok ( v) ,
344
344
Some ( bson) => Err ( Error :: value_access_unexpected_type (
345
- key,
346
345
bson. element_type ( ) ,
347
346
ElementType :: Array ,
348
347
) ) ,
349
- None => Err ( Error :: value_access_not_present ( key ) ) ,
348
+ None => Err ( Error :: value_access_not_present ( ) ) ,
350
349
}
350
+ . map_err ( |e| e. with_key ( key) )
351
351
}
352
352
353
353
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -357,12 +357,12 @@ impl Document {
357
357
match self . get_mut ( key) {
358
358
Some ( & mut Bson :: Array ( ref mut v) ) => Ok ( v) ,
359
359
Some ( bson) => Err ( Error :: value_access_unexpected_type (
360
- key,
361
360
bson. element_type ( ) ,
362
361
ElementType :: Array ,
363
362
) ) ,
364
- None => Err ( Error :: value_access_not_present ( key ) ) ,
363
+ None => Err ( Error :: value_access_not_present ( ) ) ,
365
364
}
365
+ . map_err ( |e| e. with_key ( key) )
366
366
}
367
367
368
368
/// Returns a reference to the value for the given key if one is present and is of type
@@ -372,12 +372,12 @@ impl Document {
372
372
match self . get ( key) {
373
373
Some ( Bson :: Document ( v) ) => Ok ( v) ,
374
374
Some ( bson) => Err ( Error :: value_access_unexpected_type (
375
- key,
376
375
bson. element_type ( ) ,
377
376
ElementType :: EmbeddedDocument ,
378
377
) ) ,
379
- None => Err ( Error :: value_access_not_present ( key ) ) ,
378
+ None => Err ( Error :: value_access_not_present ( ) ) ,
380
379
}
380
+ . map_err ( |e| e. with_key ( key) )
381
381
}
382
382
383
383
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -387,12 +387,12 @@ impl Document {
387
387
match self . get_mut ( key) {
388
388
Some ( & mut Bson :: Document ( ref mut v) ) => Ok ( v) ,
389
389
Some ( bson) => Err ( Error :: value_access_unexpected_type (
390
- key,
391
390
bson. element_type ( ) ,
392
391
ElementType :: EmbeddedDocument ,
393
392
) ) ,
394
- None => Err ( Error :: value_access_not_present ( key ) ) ,
393
+ None => Err ( Error :: value_access_not_present ( ) ) ,
395
394
}
395
+ . map_err ( |e| e. with_key ( key) )
396
396
}
397
397
398
398
/// Returns a reference to the value for the given key if one is present and is of type
@@ -402,12 +402,12 @@ impl Document {
402
402
match self . get ( key) {
403
403
Some ( & Bson :: Boolean ( v) ) => Ok ( v) ,
404
404
Some ( bson) => Err ( Error :: value_access_unexpected_type (
405
- key,
406
405
bson. element_type ( ) ,
407
406
ElementType :: Boolean ,
408
407
) ) ,
409
- None => Err ( Error :: value_access_not_present ( key ) ) ,
408
+ None => Err ( Error :: value_access_not_present ( ) ) ,
410
409
}
410
+ . map_err ( |e| e. with_key ( key) )
411
411
}
412
412
413
413
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -417,12 +417,12 @@ impl Document {
417
417
match self . get_mut ( key) {
418
418
Some ( & mut Bson :: Boolean ( ref mut v) ) => Ok ( v) ,
419
419
Some ( bson) => Err ( Error :: value_access_unexpected_type (
420
- key,
421
420
bson. element_type ( ) ,
422
421
ElementType :: Boolean ,
423
422
) ) ,
424
- None => Err ( Error :: value_access_not_present ( key ) ) ,
423
+ None => Err ( Error :: value_access_not_present ( ) ) ,
425
424
}
425
+ . map_err ( |e| e. with_key ( key) )
426
426
}
427
427
428
428
/// Returns [`Bson::Null`] if the given key corresponds to a [`Bson::Null`] value.
@@ -431,12 +431,12 @@ impl Document {
431
431
match self . get ( key) {
432
432
Some ( & Bson :: Null ) => Ok ( Bson :: Null ) ,
433
433
Some ( bson) => Err ( Error :: value_access_unexpected_type (
434
- key,
435
434
bson. element_type ( ) ,
436
435
ElementType :: Null ,
437
436
) ) ,
438
- None => Err ( Error :: value_access_not_present ( key ) ) ,
437
+ None => Err ( Error :: value_access_not_present ( ) ) ,
439
438
}
439
+ . map_err ( |e| e. with_key ( key) )
440
440
}
441
441
442
442
/// Returns the value for the given key if one is present and is of type [`ElementType::Int32`].
@@ -445,12 +445,12 @@ impl Document {
445
445
match self . get ( key) {
446
446
Some ( & Bson :: Int32 ( v) ) => Ok ( v) ,
447
447
Some ( bson) => Err ( Error :: value_access_unexpected_type (
448
- key,
449
448
bson. element_type ( ) ,
450
449
ElementType :: Int32 ,
451
450
) ) ,
452
- None => Err ( Error :: value_access_not_present ( key ) ) ,
451
+ None => Err ( Error :: value_access_not_present ( ) ) ,
453
452
}
453
+ . map_err ( |e| e. with_key ( key) )
454
454
}
455
455
456
456
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -460,12 +460,12 @@ impl Document {
460
460
match self . get_mut ( key) {
461
461
Some ( & mut Bson :: Int32 ( ref mut v) ) => Ok ( v) ,
462
462
Some ( bson) => Err ( Error :: value_access_unexpected_type (
463
- key,
464
463
bson. element_type ( ) ,
465
464
ElementType :: Int32 ,
466
465
) ) ,
467
- None => Err ( Error :: value_access_not_present ( key ) ) ,
466
+ None => Err ( Error :: value_access_not_present ( ) ) ,
468
467
}
468
+ . map_err ( |e| e. with_key ( key) )
469
469
}
470
470
471
471
/// Returns the value for the given key if one is present and is of type [`ElementType::Int64`].
@@ -474,12 +474,12 @@ impl Document {
474
474
match self . get ( key) {
475
475
Some ( & Bson :: Int64 ( v) ) => Ok ( v) ,
476
476
Some ( bson) => Err ( Error :: value_access_unexpected_type (
477
- key,
478
477
bson. element_type ( ) ,
479
478
ElementType :: Int64 ,
480
479
) ) ,
481
- None => Err ( Error :: value_access_not_present ( key ) ) ,
480
+ None => Err ( Error :: value_access_not_present ( ) ) ,
482
481
}
482
+ . map_err ( |e| e. with_key ( key) )
483
483
}
484
484
485
485
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -489,12 +489,12 @@ impl Document {
489
489
match self . get_mut ( key) {
490
490
Some ( & mut Bson :: Int64 ( ref mut v) ) => Ok ( v) ,
491
491
Some ( bson) => Err ( Error :: value_access_unexpected_type (
492
- key,
493
492
bson. element_type ( ) ,
494
493
ElementType :: Int64 ,
495
494
) ) ,
496
- None => Err ( Error :: value_access_not_present ( key ) ) ,
495
+ None => Err ( Error :: value_access_not_present ( ) ) ,
497
496
}
497
+ . map_err ( |e| e. with_key ( key) )
498
498
}
499
499
500
500
/// Returns the value for the given key if one is present and is of type
@@ -504,12 +504,12 @@ impl Document {
504
504
match self . get ( key) {
505
505
Some ( & Bson :: Timestamp ( timestamp) ) => Ok ( timestamp) ,
506
506
Some ( bson) => Err ( Error :: value_access_unexpected_type (
507
- key,
508
507
bson. element_type ( ) ,
509
508
ElementType :: Timestamp ,
510
509
) ) ,
511
- None => Err ( Error :: value_access_not_present ( key ) ) ,
510
+ None => Err ( Error :: value_access_not_present ( ) ) ,
512
511
}
512
+ . map_err ( |e| e. with_key ( key) )
513
513
}
514
514
515
515
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -519,12 +519,12 @@ impl Document {
519
519
match self . get_mut ( key) {
520
520
Some ( & mut Bson :: Timestamp ( ref mut timestamp) ) => Ok ( timestamp) ,
521
521
Some ( bson) => Err ( Error :: value_access_unexpected_type (
522
- key,
523
522
bson. element_type ( ) ,
524
523
ElementType :: Timestamp ,
525
524
) ) ,
526
- None => Err ( Error :: value_access_not_present ( key ) ) ,
525
+ None => Err ( Error :: value_access_not_present ( ) ) ,
527
526
}
527
+ . map_err ( |e| e. with_key ( key) )
528
528
}
529
529
530
530
/// Returns a reference to the value for the given key if one is present and is of type
@@ -537,12 +537,12 @@ impl Document {
537
537
ref bytes,
538
538
} ) ) => Ok ( bytes) ,
539
539
Some ( bson) => Err ( Error :: value_access_unexpected_type (
540
- key,
541
540
bson. element_type ( ) ,
542
541
ElementType :: Binary ,
543
542
) ) ,
544
- None => Err ( Error :: value_access_not_present ( key ) ) ,
543
+ None => Err ( Error :: value_access_not_present ( ) ) ,
545
544
}
545
+ . map_err ( |e| e. with_key ( key) )
546
546
}
547
547
548
548
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -555,12 +555,12 @@ impl Document {
555
555
ref mut bytes,
556
556
} ) ) => Ok ( bytes) ,
557
557
Some ( bson) => Err ( Error :: value_access_unexpected_type (
558
- key,
559
558
bson. element_type ( ) ,
560
559
ElementType :: Binary ,
561
560
) ) ,
562
- None => Err ( Error :: value_access_not_present ( key ) ) ,
561
+ None => Err ( Error :: value_access_not_present ( ) ) ,
563
562
}
563
+ . map_err ( |e| e. with_key ( key) )
564
564
}
565
565
566
566
/// Returns the value for the given key if one is present and is of type
@@ -570,12 +570,12 @@ impl Document {
570
570
match self . get ( key) {
571
571
Some ( & Bson :: ObjectId ( v) ) => Ok ( v) ,
572
572
Some ( bson) => Err ( Error :: value_access_unexpected_type (
573
- key,
574
573
bson. element_type ( ) ,
575
574
ElementType :: ObjectId ,
576
575
) ) ,
577
- None => Err ( Error :: value_access_not_present ( key ) ) ,
576
+ None => Err ( Error :: value_access_not_present ( ) ) ,
578
577
}
578
+ . map_err ( |e| e. with_key ( key) )
579
579
}
580
580
581
581
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -585,12 +585,12 @@ impl Document {
585
585
match self . get_mut ( key) {
586
586
Some ( & mut Bson :: ObjectId ( ref mut v) ) => Ok ( v) ,
587
587
Some ( bson) => Err ( Error :: value_access_unexpected_type (
588
- key,
589
588
bson. element_type ( ) ,
590
589
ElementType :: ObjectId ,
591
590
) ) ,
592
- None => Err ( Error :: value_access_not_present ( key ) ) ,
591
+ None => Err ( Error :: value_access_not_present ( ) ) ,
593
592
}
593
+ . map_err ( |e| e. with_key ( key) )
594
594
}
595
595
596
596
/// Returns a reference to the value for the given key if one is present and is of type
@@ -600,12 +600,12 @@ impl Document {
600
600
match self . get ( key) {
601
601
Some ( Bson :: DateTime ( v) ) => Ok ( v) ,
602
602
Some ( bson) => Err ( Error :: value_access_unexpected_type (
603
- key,
604
603
bson. element_type ( ) ,
605
604
ElementType :: DateTime ,
606
605
) ) ,
607
- None => Err ( Error :: value_access_not_present ( key ) ) ,
606
+ None => Err ( Error :: value_access_not_present ( ) ) ,
608
607
}
608
+ . map_err ( |e| e. with_key ( key) )
609
609
}
610
610
611
611
/// Returns a mutable reference to the value for the given key if one is present and is of type
@@ -615,12 +615,12 @@ impl Document {
615
615
match self . get_mut ( key) {
616
616
Some ( & mut Bson :: DateTime ( ref mut v) ) => Ok ( v) ,
617
617
Some ( bson) => Err ( Error :: value_access_unexpected_type (
618
- key,
619
618
bson. element_type ( ) ,
620
619
ElementType :: DateTime ,
621
620
) ) ,
622
- None => Err ( Error :: value_access_not_present ( key ) ) ,
621
+ None => Err ( Error :: value_access_not_present ( ) ) ,
623
622
}
623
+ . map_err ( |e| e. with_key ( key) )
624
624
}
625
625
626
626
/// Returns whether the map contains a value for the specified key.
0 commit comments