@@ -29,12 +29,11 @@ use std::{
2929use chrono:: { DateTime , Datelike , SecondsFormat , TimeZone , Utc } ;
3030use serde_json:: { json, Value } ;
3131
32- #[ cfg( feature = "decimal128" ) ]
33- use crate :: decimal128:: Decimal128 ;
3432pub use crate :: document:: Document ;
3533use crate :: {
3634 oid:: { self , ObjectId } ,
3735 spec:: { BinarySubtype , ElementType } ,
36+ Decimal128 ,
3837} ;
3938
4039/// Possible BSON value types.
@@ -73,7 +72,6 @@ pub enum Bson {
7372 /// Symbol (Deprecated)
7473 Symbol ( String ) ,
7574 /// [128-bit decimal floating point](https://github.com/mongodb/specifications/blob/master/source/bson-decimal128/decimal128.rst)
76- #[ cfg( feature = "decimal128" ) ]
7775 Decimal128 ( Decimal128 ) ,
7876 /// Undefined value (Deprecated)
7977 Undefined ,
@@ -139,8 +137,7 @@ impl Display for Bson {
139137 Bson :: ObjectId ( ref id) => write ! ( fmt, "ObjectId(\" {}\" )" , id) ,
140138 Bson :: UtcDatetime ( date_time) => write ! ( fmt, "Date(\" {}\" )" , date_time) ,
141139 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) ,
144141 Bson :: Undefined => write ! ( fmt, "undefined" ) ,
145142 Bson :: MinKey => write ! ( fmt, "MinKey" ) ,
146143 Bson :: MaxKey => write ! ( fmt, "MaxKey" ) ,
@@ -331,6 +328,10 @@ impl From<Bson> for Value {
331328
332329impl Bson {
333330 /// 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.
334335 pub fn into_relaxed_extjson ( self ) -> Value {
335336 match self {
336337 Bson :: FloatingPoint ( v) if v. is_nan ( ) => {
@@ -408,6 +409,11 @@ impl Bson {
408409 Bson :: Symbol ( v) => json ! ( { "$symbol" : v } ) ,
409410 #[ cfg( feature = "decimal128" ) ]
410411 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+ ) ,
411417 Bson :: Undefined => json ! ( { "$undefined" : true } ) ,
412418 Bson :: MinKey => json ! ( { "$minKey" : 1 } ) ,
413419 Bson :: MaxKey => json ! ( { "$maxKey" : 1 } ) ,
@@ -426,6 +432,10 @@ impl Bson {
426432 }
427433
428434 /// 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.
429439 pub fn into_canonical_extjson ( self ) -> Value {
430440 match self {
431441 Bson :: I32 ( i) => json ! ( { "$numberInt" : i. to_string( ) } ) ,
@@ -482,7 +492,6 @@ impl Bson {
482492 Bson :: ObjectId ( ..) => ElementType :: ObjectId ,
483493 Bson :: UtcDatetime ( ..) => ElementType :: UtcDatetime ,
484494 Bson :: Symbol ( ..) => ElementType :: Symbol ,
485- #[ cfg( feature = "decimal128" ) ]
486495 Bson :: Decimal128 ( ..) => ElementType :: Decimal128Bit ,
487496 Bson :: Undefined => ElementType :: Undefined ,
488497 Bson :: MaxKey => ElementType :: MaxKey ,
0 commit comments