Skip to content

Commit f9451e6

Browse files
committed
[#66] Fixed bug, add missing convertion for f64 in Value
1 parent fc58a01 commit f9451e6

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bson"
3-
version = "0.6.0"
3+
version = "0.6.1"
44
authors = [
55
"Y. T. Chung <[email protected]>",
66
"Kevin Yeh <[email protected]>"

src/bson.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,20 @@ impl From<DateTime<UTC>> for Bson {
266266
impl From<Value> for Bson {
267267
fn from(a: Value) -> Bson {
268268
match a {
269-
Value::Number(x) =>
270-
x.as_i64().map(Bson::from)
271-
.or(x.as_u64().map(Bson::from))
272-
.expect(&format!("Invalid number value: {}", x)),
269+
Value::Number(x) => {
270+
x.as_i64()
271+
.map(Bson::from)
272+
.or_else(|| x.as_u64().map(Bson::from))
273+
.or_else(|| x.as_f64().map(Bson::from))
274+
.unwrap_or_else(|| panic!("Invalid number value: {}", x))
275+
}
273276
Value::String(x) => x.into(),
274277
Value::Bool(x) => x.into(),
275278
Value::Array(x) => Bson::Array(x.into_iter().map(Bson::from).collect()),
276279
Value::Object(x) => {
277280
Bson::from_extended_document(x.into_iter()
278-
.map(|(k, v)| (k.clone(), v.into()))
279-
.collect())
281+
.map(|(k, v)| (k.clone(), v.into()))
282+
.collect())
280283
}
281284
Value::Null => Bson::Null,
282285
}
@@ -292,10 +295,12 @@ impl Into<Value> for Bson {
292295
Bson::Document(v) => json!(v),
293296
Bson::Boolean(v) => json!(v),
294297
Bson::Null => Value::Null,
295-
Bson::RegExp(pat, opt) => json!({
296-
"$regex": pat,
297-
"$options": opt
298-
}),
298+
Bson::RegExp(pat, opt) => {
299+
json!({
300+
"$regex": pat,
301+
"$options": opt
302+
})
303+
}
299304
Bson::JavaScriptCode(code) => json!({"$code": code}),
300305
Bson::JavaScriptCodeWithScope(code, scope) => {
301306
json!({
@@ -321,13 +326,15 @@ impl Into<Value> for Bson {
321326
})
322327
}
323328
Bson::ObjectId(v) => json!({"$oid": v.to_string()}),
324-
Bson::UtcDatetime(v) => json!({
325-
"$date": {
326-
"$numberLong": (v.timestamp() * 1000) + ((v.nanosecond() / 1000000) as i64)
327-
}
328-
}),
329+
Bson::UtcDatetime(v) => {
330+
json!({
331+
"$date": {
332+
"$numberLong": (v.timestamp() * 1000) + ((v.nanosecond() / 1000000) as i64)
333+
}
334+
})
335+
}
329336
// FIXME: Don't know what is the best way to encode Symbol type
330-
Bson::Symbol(v) => json!({"$symbol": v})
337+
Bson::Symbol(v) => json!({"$symbol": v}),
331338
}
332339
}
333340
}
@@ -468,9 +475,11 @@ impl Bson {
468475
} else if let Ok(hex) = values.get_str("$oid") {
469476
return Bson::ObjectId(oid::ObjectId::with_string(hex).unwrap());
470477

471-
} else if let Ok(long) = values.get_document("$date")
472-
.and_then(|inner| inner.get_i64("$numberLong")) {
473-
return Bson::UtcDatetime(UTC.timestamp(long / 1000, (long % 1000) as u32 * 1000000));
478+
} else if let Ok(long) = values
479+
.get_document("$date")
480+
.and_then(|inner| inner.get_i64("$numberLong")) {
481+
return Bson::UtcDatetime(UTC.timestamp(long / 1000,
482+
(long % 1000) as u32 * 1000000));
474483
} else if let Ok(sym) = values.get_str("$symbol") {
475484
return Bson::Symbol(sym.to_owned());
476485
}

0 commit comments

Comments
 (0)