22
22
//! BSON definition
23
23
24
24
use std:: collections:: BTreeMap ;
25
- use std:: string;
26
25
27
26
use chrono:: { DateTime , UTC } ;
28
27
use rustc_serialize:: json;
29
28
use rustc_serialize:: hex:: ToHex ;
30
29
31
- use spec:: BinarySubtype ;
30
+ use spec:: { ElementType , BinarySubtype } ;
32
31
32
+ /// Possible BSON value types.
33
33
#[ derive( Debug , Clone ) ]
34
34
pub enum Bson {
35
35
FloatingPoint ( f64 ) ,
36
36
String ( String ) ,
37
- Array ( self :: Array ) ,
38
- Document ( self :: Document ) ,
37
+ Array ( Array ) ,
38
+ Document ( Document ) ,
39
39
Boolean ( bool ) ,
40
40
Null ,
41
- RegExp ( string:: String , string:: String ) ,
42
- JavaScriptCode ( string:: String ) ,
43
- JavaScriptCodeWithScope ( string:: String , self :: Document ) ,
44
- Deprecated ,
41
+ RegExp ( String , String ) ,
42
+ JavaScriptCode ( String ) ,
43
+ JavaScriptCodeWithScope ( String , Document ) ,
45
44
I32 ( i32 ) ,
46
45
I64 ( i64 ) ,
47
46
TimeStamp ( i64 ) ,
@@ -50,10 +49,34 @@ pub enum Bson {
50
49
UtcDatetime ( DateTime < UTC > ) ,
51
50
}
52
51
52
+ /// Alias for `Vec<Bson>`.
53
53
pub type Array = Vec < Bson > ;
54
+ /// Alias for `BTreeMap<String, Bson>`.
54
55
pub type Document = BTreeMap < String , Bson > ;
55
56
56
57
impl Bson {
58
+ /// Get the `ElementType` of this value.
59
+ pub fn element_type ( & self ) -> ElementType {
60
+ match self {
61
+ & Bson :: FloatingPoint ( ..) => ElementType :: FloatingPoint ,
62
+ & Bson :: String ( ..) => ElementType :: Utf8String ,
63
+ & Bson :: Array ( ..) => ElementType :: Array ,
64
+ & Bson :: Document ( ..) => ElementType :: EmbeddedDocument ,
65
+ & Bson :: Boolean ( ..) => ElementType :: Boolean ,
66
+ & Bson :: Null => ElementType :: NullValue ,
67
+ & Bson :: RegExp ( ..) => ElementType :: RegularExpression ,
68
+ & Bson :: JavaScriptCode ( ..) => ElementType :: JavaScriptCode ,
69
+ & Bson :: JavaScriptCodeWithScope ( ..) => ElementType :: JavaScriptCodeWithScope ,
70
+ & Bson :: I32 ( ..) => ElementType :: Integer32Bit ,
71
+ & Bson :: I64 ( ..) => ElementType :: Integer64Bit ,
72
+ & Bson :: TimeStamp ( ..) => ElementType :: TimeStamp ,
73
+ & Bson :: Binary ( ..) => ElementType :: Binary ,
74
+ & Bson :: ObjectId ( ..) => ElementType :: ObjectId ,
75
+ & Bson :: UtcDatetime ( ..) => ElementType :: UtcDatetime ,
76
+ }
77
+ }
78
+
79
+ /// Convert this value to the best approximate `Json`.
57
80
pub fn to_json ( & self ) -> json:: Json {
58
81
match self {
59
82
& Bson :: FloatingPoint ( v) => json:: Json :: F64 ( v) ,
@@ -66,32 +89,31 @@ impl Bson {
66
89
& Bson :: Null => json:: Json :: Null ,
67
90
& Bson :: RegExp ( ref pat, ref opt) => {
68
91
let mut re = json:: Object :: new ( ) ;
69
- re. insert ( "pattern" . to_string ( ) , json:: Json :: String ( pat. clone ( ) ) ) ;
70
- re. insert ( "options" . to_string ( ) , json:: Json :: String ( opt. clone ( ) ) ) ;
92
+ re. insert ( "pattern" . to_owned ( ) , json:: Json :: String ( pat. clone ( ) ) ) ;
93
+ re. insert ( "options" . to_owned ( ) , json:: Json :: String ( opt. clone ( ) ) ) ;
71
94
72
95
json:: Json :: Object ( re)
73
96
} ,
74
97
& Bson :: JavaScriptCode ( ref code) => json:: Json :: String ( code. clone ( ) ) ,
75
98
& Bson :: JavaScriptCodeWithScope ( ref code, ref scope) => {
76
99
let mut obj = json:: Object :: new ( ) ;
77
- obj. insert ( "code" . to_string ( ) , json:: Json :: String ( code. clone ( ) ) ) ;
100
+ obj. insert ( "code" . to_owned ( ) , json:: Json :: String ( code. clone ( ) ) ) ;
78
101
79
102
let scope_obj =
80
103
scope. iter ( ) . map ( |( k, v) | ( k. clone ( ) , v. to_json ( ) ) ) . collect ( ) ;
81
104
82
- obj. insert ( "scope" . to_string ( ) , json:: Json :: Object ( scope_obj) ) ;
105
+ obj. insert ( "scope" . to_owned ( ) , json:: Json :: Object ( scope_obj) ) ;
83
106
84
107
json:: Json :: Object ( obj)
85
108
} ,
86
- & Bson :: Deprecated => json:: Json :: String ( "deprecated" . to_string ( ) ) ,
87
109
& Bson :: I32 ( v) => json:: Json :: I64 ( v as i64 ) ,
88
110
& Bson :: I64 ( v) => json:: Json :: I64 ( v) ,
89
111
& Bson :: TimeStamp ( v) => json:: Json :: I64 ( v) ,
90
112
& Bson :: Binary ( t, ref v) => {
91
113
let mut obj = json:: Object :: new ( ) ;
92
114
let tval: u8 = From :: from ( t) ;
93
- obj. insert ( "type" . to_string ( ) , json:: Json :: I64 ( tval as i64 ) ) ;
94
- obj. insert ( "data" . to_string ( ) , json:: Json :: String ( v[ .. ] . to_hex ( ) ) ) ;
115
+ obj. insert ( "type" . to_owned ( ) , json:: Json :: I64 ( tval as i64 ) ) ;
116
+ obj. insert ( "data" . to_owned ( ) , json:: Json :: String ( v. to_hex ( ) ) ) ;
95
117
96
118
json:: Json :: Object ( obj)
97
119
} ,
@@ -100,38 +122,17 @@ impl Bson {
100
122
}
101
123
}
102
124
125
+ /// Create a `Bson` from a `Json`.
103
126
pub fn from_json ( j : & json:: Json ) -> Bson {
104
127
match j {
105
128
& json:: Json :: I64 ( x) => Bson :: I64 ( x) ,
106
129
& json:: Json :: U64 ( x) => Bson :: I64 ( x as i64 ) ,
107
130
& json:: Json :: F64 ( x) => Bson :: FloatingPoint ( x) ,
108
131
& json:: Json :: String ( ref x) => Bson :: String ( x. clone ( ) ) ,
109
132
& json:: Json :: Boolean ( x) => Bson :: Boolean ( x) ,
110
- & json:: Json :: Array ( ref x) => Bson :: Array ( x. iter ( ) . map ( |x| Bson :: from_json ( x ) ) . collect ( ) ) ,
133
+ & json:: Json :: Array ( ref x) => Bson :: Array ( x. iter ( ) . map ( Bson :: from_json) . collect ( ) ) ,
111
134
& json:: Json :: Object ( ref x) => Bson :: Document ( x. iter ( ) . map ( |( k, v) | ( k. clone ( ) , Bson :: from_json ( v) ) ) . collect ( ) ) ,
112
135
& json:: Json :: Null => Bson :: Null ,
113
136
}
114
137
}
115
138
}
116
-
117
- pub trait ToBson {
118
- fn to_bson ( & self ) -> Bson ;
119
- }
120
-
121
- impl ToBson for str {
122
- fn to_bson ( & self ) -> Bson {
123
- Bson :: String ( self . to_string ( ) )
124
- }
125
- }
126
-
127
- impl < T : ToBson > ToBson for [ T ] {
128
- fn to_bson ( & self ) -> Bson {
129
- Bson :: Array ( self . iter ( ) . map ( |x| x. to_bson ( ) ) . collect ( ) )
130
- }
131
- }
132
-
133
- impl < T : ToBson > ToBson for BTreeMap < String , T > {
134
- fn to_bson ( & self ) -> Bson {
135
- Bson :: Document ( self . iter ( ) . map ( |( k, v) | ( k. clone ( ) , v. to_bson ( ) ) ) . collect ( ) )
136
- }
137
- }
0 commit comments