@@ -29,12 +29,11 @@ use std::{
29
29
use chrono:: { DateTime , Datelike , SecondsFormat , TimeZone , Utc } ;
30
30
use serde_json:: { json, Value } ;
31
31
32
- #[ cfg( feature = "decimal128" ) ]
33
- use crate :: decimal128:: Decimal128 ;
34
32
pub use crate :: document:: Document ;
35
33
use crate :: {
36
34
oid:: { self , ObjectId } ,
37
35
spec:: { BinarySubtype , ElementType } ,
36
+ Decimal128 ,
38
37
} ;
39
38
40
39
/// Possible BSON value types.
@@ -73,7 +72,6 @@ pub enum Bson {
73
72
/// Symbol (Deprecated)
74
73
Symbol ( String ) ,
75
74
/// [128-bit decimal floating point](https://github.com/mongodb/specifications/blob/master/source/bson-decimal128/decimal128.rst)
76
- #[ cfg( feature = "decimal128" ) ]
77
75
Decimal128 ( Decimal128 ) ,
78
76
/// Undefined value (Deprecated)
79
77
Undefined ,
@@ -139,8 +137,7 @@ impl Display for Bson {
139
137
Bson :: ObjectId ( ref id) => write ! ( fmt, "ObjectId(\" {}\" )" , id) ,
140
138
Bson :: UtcDatetime ( date_time) => write ! ( fmt, "Date(\" {}\" )" , date_time) ,
141
139
Bson :: Symbol ( ref sym) => write ! ( fmt, "Symbol(\" {}\" )" , sym) ,
142
- #[ cfg( feature = "decimal128" ) ]
143
- Bson :: Decimal128 ( ref d) => write ! ( fmt, "Decimal128({})" , d) ,
140
+ Bson :: Decimal128 ( ref d) => write ! ( fmt, "{}" , d) ,
144
141
Bson :: Undefined => write ! ( fmt, "undefined" ) ,
145
142
Bson :: MinKey => write ! ( fmt, "MinKey" ) ,
146
143
Bson :: MaxKey => write ! ( fmt, "MaxKey" ) ,
@@ -331,6 +328,10 @@ impl From<Bson> for Value {
331
328
332
329
impl Bson {
333
330
/// Converts the Bson value into its [relaxed extended JSON representation](https://docs.mongodb.com/manual/reference/mongodb-extended-json/).
331
+ ///
332
+ /// Note: extended json encoding for `Decimal128` values is not supported without the
333
+ /// "decimal128" feature flag. If this method is called on a case which contains a
334
+ /// `Decimal128` value, it will panic.
334
335
pub fn into_relaxed_extjson ( self ) -> Value {
335
336
match self {
336
337
Bson :: FloatingPoint ( v) if v. is_nan ( ) => {
@@ -408,6 +409,11 @@ impl Bson {
408
409
Bson :: Symbol ( v) => json ! ( { "$symbol" : v } ) ,
409
410
#[ cfg( feature = "decimal128" ) ]
410
411
Bson :: Decimal128 ( ref v) => json ! ( { "$numberDecimal" : v. to_string( ) } ) ,
412
+ #[ cfg( not( feature = "decimal128" ) ) ]
413
+ Bson :: Decimal128 ( _) => panic ! (
414
+ "Decimal128 extended JSON not implemented yet. Use the decimal128 feature to \
415
+ enable experimental support for it."
416
+ ) ,
411
417
Bson :: Undefined => json ! ( { "$undefined" : true } ) ,
412
418
Bson :: MinKey => json ! ( { "$minKey" : 1 } ) ,
413
419
Bson :: MaxKey => json ! ( { "$maxKey" : 1 } ) ,
@@ -426,6 +432,10 @@ impl Bson {
426
432
}
427
433
428
434
/// Converts the Bson value into its [canonical extended JSON representation](https://docs.mongodb.com/manual/reference/mongodb-extended-json/).
435
+ ///
436
+ /// Note: extended json encoding for `Decimal128` values is not supported without the
437
+ /// "decimal128" feature flag. If this method is called on a case which contains a
438
+ /// `Decimal128` value, it will panic.
429
439
pub fn into_canonical_extjson ( self ) -> Value {
430
440
match self {
431
441
Bson :: I32 ( i) => json ! ( { "$numberInt" : i. to_string( ) } ) ,
@@ -482,7 +492,6 @@ impl Bson {
482
492
Bson :: ObjectId ( ..) => ElementType :: ObjectId ,
483
493
Bson :: UtcDatetime ( ..) => ElementType :: UtcDatetime ,
484
494
Bson :: Symbol ( ..) => ElementType :: Symbol ,
485
- #[ cfg( feature = "decimal128" ) ]
486
495
Bson :: Decimal128 ( ..) => ElementType :: Decimal128Bit ,
487
496
Bson :: Undefined => ElementType :: Undefined ,
488
497
Bson :: MaxKey => ElementType :: MaxKey ,
0 commit comments