Skip to content

Commit 216b906

Browse files
committed
Add From traits for Bson
1 parent 5f9f2e5 commit 216b906

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

src/bson.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,87 @@ pub type Array = Vec<Bson>;
5353
/// Alias for `OrderedDocument`.
5454
pub type Document = OrderedDocument;
5555

56+
impl From<f64> for Bson {
57+
fn from(a: f64) -> Bson {
58+
Bson::FloatingPoint(a)
59+
}
60+
}
61+
62+
impl<'a> From<&'a str> for Bson {
63+
fn from(s: &str) -> Bson {
64+
Bson::String(s.to_owned())
65+
}
66+
}
67+
68+
impl From<String> for Bson {
69+
fn from(a: String) -> Bson {
70+
Bson::String(a.to_owned())
71+
}
72+
}
73+
74+
impl From<Array> for Bson {
75+
fn from(a: Array) -> Bson {
76+
Bson::Array(a)
77+
}
78+
}
79+
80+
impl From<Document> for Bson {
81+
fn from(a: Document) -> Bson {
82+
Bson::Document(a)
83+
}
84+
}
85+
86+
impl From<bool> for Bson {
87+
fn from(a: bool) -> Bson {
88+
Bson::Boolean(a)
89+
}
90+
}
91+
92+
impl From<(String, String)> for Bson {
93+
fn from(a: (String, String)) -> Bson {
94+
let (a1, a2) = a;
95+
Bson::RegExp(a1.to_owned(), a2.to_owned())
96+
}
97+
}
98+
99+
impl From<(String, Document)> for Bson {
100+
fn from(a: (String, Document)) -> Bson {
101+
let (a1, a2) = a;
102+
Bson::JavaScriptCodeWithScope(a1, a2)
103+
}
104+
}
105+
106+
impl From<(BinarySubtype, Vec<u8>)> for Bson {
107+
fn from(a: (BinarySubtype, Vec<u8>)) -> Bson {
108+
let (a1, a2) = a;
109+
Bson::Binary(a1, a2)
110+
}
111+
}
112+
113+
impl From<i32> for Bson {
114+
fn from(a: i32) -> Bson {
115+
Bson::I32(a)
116+
}
117+
}
118+
119+
impl From<i64> for Bson {
120+
fn from(a: i64) -> Bson {
121+
Bson::I64(a)
122+
}
123+
}
124+
125+
impl From<[u8; 12]> for Bson {
126+
fn from(a: [u8; 12]) -> Bson {
127+
Bson::ObjectId(a)
128+
}
129+
}
130+
131+
impl From<DateTime<UTC>> for Bson {
132+
fn from(a: DateTime<UTC>) -> Bson {
133+
Bson::UtcDatetime(a)
134+
}
135+
}
136+
56137
impl Bson {
57138
/// Get the `ElementType` of this value.
58139
pub fn element_type(&self) -> ElementType {

0 commit comments

Comments
 (0)