@@ -50,6 +50,7 @@ pub enum Bson {
5050 Binary ( BinarySubtype , Vec < u8 > ) ,
5151 ObjectId ( oid:: ObjectId ) ,
5252 UtcDatetime ( DateTime < UTC > ) ,
53+ Symbol ( String ) ,
5354}
5455
5556/// Alias for `Vec<Bson>`.
@@ -94,7 +95,8 @@ impl Display for Bson {
9495 & Bson :: Binary ( t, ref vec) =>
9596 write ! ( fmt, "BinData({}, 0x{})" , u8 :: from( t) , vec. to_hex( ) ) ,
9697 & Bson :: ObjectId ( ref id) => write ! ( fmt, "ObjectId(\" {}\" )" , id) ,
97- & Bson :: UtcDatetime ( date_time) => write ! ( fmt, "Date(\" {}\" )" , date_time)
98+ & Bson :: UtcDatetime ( date_time) => write ! ( fmt, "Date(\" {}\" )" , date_time) ,
99+ & Bson :: Symbol ( ref sym) => write ! ( fmt, "Symbol(\" {}\" )" , sym) ,
98100 }
99101 }
100102}
@@ -229,6 +231,7 @@ impl Bson {
229231 & Bson :: Binary ( ..) => ElementType :: Binary ,
230232 & Bson :: ObjectId ( ..) => ElementType :: ObjectId ,
231233 & Bson :: UtcDatetime ( ..) => ElementType :: UtcDatetime ,
234+ & Bson :: Symbol ( ..) => ElementType :: Symbol ,
232235 }
233236 }
234237
@@ -295,10 +298,16 @@ impl Bson {
295298 & Bson :: UtcDatetime ( ref v) => {
296299 let mut obj = json:: Object :: new ( ) ;
297300 let mut inner = json:: Object :: new ( ) ;
298- inner. insert ( "$numberLong" . to_owned ( ) , json:: Json :: I64 ( ( v. timestamp ( ) * 1000 ) +
301+ inner. insert ( "$numberLong" . to_owned ( ) , json:: Json :: I64 ( ( v. timestamp ( ) * 1000 ) +
299302 ( v. nanosecond ( ) / 1000000 ) as i64 ) ) ;
300303 obj. insert ( "$date" . to_owned ( ) , json:: Json :: Object ( inner) ) ;
301304 json:: Json :: Object ( obj)
305+ } ,
306+ & Bson :: Symbol ( ref v) => {
307+ // FIXME: Don't know what is the best way to encode Symbol type
308+ let mut obj = json:: Object :: new ( ) ;
309+ obj. insert ( "$symbol" . to_owned ( ) , json:: Json :: String ( v. to_owned ( ) ) ) ;
310+ json:: Json :: Object ( obj)
302311 }
303312 }
304313 }
@@ -365,13 +374,18 @@ impl Bson {
365374 }
366375 }
367376 }
377+ Bson :: Symbol ( ref v) => {
378+ doc ! {
379+ "$symbol" => ( v. to_owned( ) )
380+ }
381+ }
368382 _ => panic ! ( "Attempted conversion of invalid data type: {}" , self ) ,
369383 }
370384 }
371385
372386 pub fn from_extended_document ( values : Document ) -> Bson {
373387 if values. len ( ) == 2 {
374- if let ( Ok ( pat) , Ok ( opt) ) = ( values. get_str ( "$regex" ) ,
388+ if let ( Ok ( pat) , Ok ( opt) ) = ( values. get_str ( "$regex" ) ,
375389 values. get_str ( "$options" ) ) {
376390 return Bson :: RegExp ( pat. to_owned ( ) , opt. to_owned ( ) ) ;
377391
@@ -405,9 +419,11 @@ impl Bson {
405419 } else if let Ok ( long) = values. get_document ( "$date" )
406420 . and_then ( |inner| inner. get_i64 ( "$numberLong" ) ) {
407421 return Bson :: UtcDatetime ( UTC . timestamp ( long / 1000 , ( long % 1000 ) as u32 * 1000000 ) ) ;
422+ } else if let Ok ( sym) = values. get_str ( "$symbol" ) {
423+ return Bson :: Symbol ( sym. to_owned ( ) ) ;
408424 }
409425 }
410-
426+
411427 Bson :: Document ( values)
412428 }
413429}
0 commit comments