Skip to content

Commit 6f6417c

Browse files
committed
Reformatted with rustfmt
1 parent a16b10d commit 6f6417c

File tree

20 files changed

+354
-423
lines changed

20 files changed

+354
-423
lines changed

examples/encode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
extern crate bson;
22
extern crate chrono;
33

4+
use bson::{decode_document, encode_document, oid, Array, Bson, Document};
45
use std::io::Cursor;
5-
use bson::{Bson, Document, Array, encode_document, decode_document, oid};
66

77
fn main() {
88
let mut doc = Document::new();

src/bson.rs

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@
2121

2222
//! BSON definition
2323
24-
use std::fmt::{self, Display, Debug};
24+
use std::fmt::{self, Debug, Display};
2525
use std::ops::{Deref, DerefMut};
2626

2727
use chrono::{DateTime, Timelike, Utc};
2828
use chrono::offset::TimeZone;
29-
use serde_json::Value;
3029
use hex::{FromHex, ToHex};
30+
use serde_json::Value;
3131

3232
use oid;
3333
use ordered::OrderedDocument;
34-
use spec::{ElementType, BinarySubtype};
34+
use spec::{BinarySubtype, ElementType};
3535

3636
/// Possible BSON value types.
3737
#[derive(Clone, PartialEq)]
@@ -121,15 +121,15 @@ impl Display for Bson {
121121
&Bson::FloatingPoint(f) => write!(fmt, "{}", f),
122122
&Bson::String(ref s) => write!(fmt, "\"{}\"", s),
123123
&Bson::Array(ref vec) => {
124-
try!(write!(fmt, "["));
124+
write!(fmt, "[")?;
125125

126126
let mut first = true;
127127
for bson in vec.iter() {
128128
if !first {
129-
try!(write!(fmt, ", "));
129+
write!(fmt, ", ")?;
130130
}
131131

132-
try!(write!(fmt, "{}", bson));
132+
write!(fmt, "{}", bson)?;
133133
first = false;
134134
}
135135

@@ -139,8 +139,7 @@ impl Display for Bson {
139139
&Bson::Boolean(b) => write!(fmt, "{}", b),
140140
&Bson::Null => write!(fmt, "null"),
141141
&Bson::RegExp(ref pat, ref opt) => write!(fmt, "/{}/{}", pat, opt),
142-
&Bson::JavaScriptCode(ref s) |
143-
&Bson::JavaScriptCodeWithScope(ref s, _) => fmt.write_str(&s),
142+
&Bson::JavaScriptCode(ref s) | &Bson::JavaScriptCodeWithScope(ref s, _) => fmt.write_str(&s),
144143
&Bson::I32(i) => write!(fmt, "{}", i),
145144
&Bson::I64(i) => write!(fmt, "{}", i),
146145
&Bson::TimeStamp(i) => {
@@ -149,9 +148,7 @@ impl Display for Bson {
149148

150149
write!(fmt, "Timestamp({}, {})", time, inc)
151150
}
152-
&Bson::Binary(t, ref vec) => {
153-
write!(fmt, "BinData({}, 0x{})", u8::from(t), vec.to_hex())
154-
}
151+
&Bson::Binary(t, ref vec) => write!(fmt, "BinData({}, 0x{})", u8::from(t), vec.to_hex()),
155152
&Bson::ObjectId(ref id) => write!(fmt, "ObjectId(\"{}\")", id),
156153
&Bson::UtcDatetime(date_time) => write!(fmt, "Date(\"{}\")", date_time),
157154
&Bson::Symbol(ref sym) => write!(fmt, "Symbol(\"{}\")", sym),
@@ -274,19 +271,16 @@ impl From<Value> for Bson {
274271
fn from(a: Value) -> Bson {
275272
match a {
276273
Value::Number(x) => {
277-
x.as_i64()
278-
.map(Bson::from)
279-
.or_else(|| x.as_u64().map(Bson::from))
280-
.or_else(|| x.as_f64().map(Bson::from))
281-
.unwrap_or_else(|| panic!("Invalid number value: {}", x))
274+
x.as_i64().map(Bson::from)
275+
.or_else(|| x.as_u64().map(Bson::from))
276+
.or_else(|| x.as_f64().map(Bson::from))
277+
.unwrap_or_else(|| panic!("Invalid number value: {}", x))
282278
}
283279
Value::String(x) => x.into(),
284280
Value::Bool(x) => x.into(),
285281
Value::Array(x) => Bson::Array(x.into_iter().map(Bson::from).collect()),
286282
Value::Object(x) => {
287-
Bson::from_extended_document(x.into_iter()
288-
.map(|(k, v)| (k.clone(), v.into()))
289-
.collect())
283+
Bson::from_extended_document(x.into_iter().map(|(k, v)| (k.clone(), v.into())).collect())
290284
}
291285
Value::Null => Bson::Null,
292286
}
@@ -308,7 +302,7 @@ impl Into<Value> for Bson {
308302
"$options": opt
309303
})
310304
}
311-
Bson::JavaScriptCode(code) => json!({"$code": code}),
305+
Bson::JavaScriptCode(code) => json!({ "$code": code }),
312306
Bson::JavaScriptCodeWithScope(code, scope) => {
313307
json!({
314308
"$code": code,
@@ -341,7 +335,7 @@ impl Into<Value> for Bson {
341335
})
342336
}
343337
// FIXME: Don't know what is the best way to encode Symbol type
344-
Bson::Symbol(v) => json!({"$symbol": v}),
338+
Bson::Symbol(v) => json!({ "$symbol": v }),
345339
}
346340
}
347341
}
@@ -371,21 +365,21 @@ impl Bson {
371365

372366
/// Clones the bson and returns the representative serde_json Value.
373367
/// The json will be in [extended JSON format](https://docs.mongodb.com/manual/reference/mongodb-extended-json/).
374-
#[deprecated(since="0.5.1", note="use bson.clone().into() instead")]
368+
#[deprecated(since = "0.5.1", note = "use bson.clone().into() instead")]
375369
pub fn to_json(&self) -> Value {
376370
self.clone().into()
377371
}
378372

379373
/// Consumes the bson and returns the representative serde_json Value.
380374
/// The json will be in [extended JSON format](https://docs.mongodb.com/manual/reference/mongodb-extended-json/).
381-
#[deprecated(since="0.5.1", note="use bson.into() instead")]
375+
#[deprecated(since = "0.5.1", note = "use bson.into() instead")]
382376
pub fn into_json(self) -> Value {
383377
self.into()
384378
}
385379

386380
/// Consumes the serde_json Value and returns the representative bson.
387381
/// The json should be in [extended JSON format](https://docs.mongodb.com/manual/reference/mongodb-extended-json/).
388-
#[deprecated(since="0.5.1", note="use json.into() instead")]
382+
#[deprecated(since = "0.5.1", note = "use json.into() instead")]
389383
pub fn from_json(val: Value) -> Bson {
390384
val.into()
391385
}
@@ -456,37 +450,28 @@ impl Bson {
456450
if values.len() == 2 {
457451
if let (Ok(pat), Ok(opt)) = (values.get_str("$regex"), values.get_str("$options")) {
458452
return Bson::RegExp(pat.to_owned(), opt.to_owned());
459-
460-
} else if let (Ok(code), Ok(scope)) =
461-
(values.get_str("$code"), values.get_document("$scope")) {
453+
} else if let (Ok(code), Ok(scope)) = (values.get_str("$code"), values.get_document("$scope")) {
462454
return Bson::JavaScriptCodeWithScope(code.to_owned(), scope.to_owned());
463-
464455
} else if let (Ok(t), Ok(i)) = (values.get_i32("t"), values.get_i32("i")) {
465456
let timestamp = ((t as i64) << 32) + (i as i64);
466457
return Bson::TimeStamp(timestamp);
467-
468458
} else if let (Ok(t), Ok(i)) = (values.get_i64("t"), values.get_i64("i")) {
469459
let timestamp = (t << 32) + i;
470460
return Bson::TimeStamp(timestamp);
471-
472461
} else if let (Ok(hex), Ok(t)) = (values.get_str("$binary"), values.get_i64("type")) {
473462
let ttype = t as u8;
474463
return Bson::Binary(From::from(ttype),
475464
FromHex::from_hex(hex.as_bytes()).unwrap());
476465
}
477-
478466
} else if values.len() == 1 {
479467
if let Ok(code) = values.get_str("$code") {
480468
return Bson::JavaScriptCode(code.to_owned());
481-
482469
} else if let Ok(hex) = values.get_str("$oid") {
483470
return Bson::ObjectId(oid::ObjectId::with_string(hex).unwrap());
484-
485-
} else if let Ok(long) = values
486-
.get_document("$date")
487-
.and_then(|inner| inner.get_i64("$numberLong")) {
488-
return Bson::UtcDatetime(Utc.timestamp(long / 1000,
489-
((long % 1000) * 1000000) as u32));
471+
} else if let Ok(long) = values.get_document("$date")
472+
.and_then(|inner| inner.get_i64("$numberLong"))
473+
{
474+
return Bson::UtcDatetime(Utc.timestamp(long / 1000, ((long % 1000) * 1000000) as u32));
490475
} else if let Ok(sym) = values.get_str("$symbol") {
491476
return Bson::Symbol(sym.to_owned());
492477
}

src/compat/u2f.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Convert unsigned types to/from `Bson::FloatingPoint`
22
3-
use serde::{Serializer, Deserializer, Deserialize};
3+
use serde::{Deserialize, Deserializer, Serializer};
44

55
/// Converts primitive unsigned types to `f64`
66
pub trait ToF64 {

src/decoder/error.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{io, error, fmt, string};
1+
use std::{error, fmt, io, string};
22
use std::fmt::Display;
33

44
use serde::de::{self, Expected, Unexpected};
@@ -48,22 +48,16 @@ impl fmt::Display for DecoderError {
4848
match *self {
4949
DecoderError::IoError(ref inner) => inner.fmt(fmt),
5050
DecoderError::FromUtf8Error(ref inner) => inner.fmt(fmt),
51-
DecoderError::UnrecognizedElementType(tag) => {
52-
write!(fmt, "unrecognized element type `{}`", tag)
53-
}
51+
DecoderError::UnrecognizedElementType(tag) => write!(fmt, "unrecognized element type `{}`", tag),
5452
DecoderError::InvalidArrayKey(ref want, ref got) => {
5553
write!(fmt, "invalid array key: expected `{}`, got `{}`", want, got)
5654
}
57-
DecoderError::ExpectedField(field_type) => {
58-
write!(fmt, "expected a field of type `{}`", field_type)
59-
}
55+
DecoderError::ExpectedField(field_type) => write!(fmt, "expected a field of type `{}`", field_type),
6056
DecoderError::UnknownField(ref field) => write!(fmt, "unknown field `{}`", field),
6157
DecoderError::SyntaxError(ref inner) => inner.fmt(fmt),
6258
DecoderError::EndOfStream => write!(fmt, "end of stream"),
6359
DecoderError::InvalidType(ref desc) => desc.fmt(fmt),
64-
DecoderError::InvalidLength(ref len, ref desc) => {
65-
write!(fmt, "expecting length {}, {}", len, desc)
66-
}
60+
DecoderError::InvalidLength(ref len, ref desc) => write!(fmt, "expecting length {}, {}", len, desc),
6761
DecoderError::DuplicatedField(ref field) => write!(fmt, "duplicated field `{}`", field),
6862
DecoderError::UnknownVariant(ref var) => write!(fmt, "unknown variant `{}`", var),
6963
DecoderError::InvalidValue(ref desc) => desc.fmt(fmt),
@@ -78,7 +72,7 @@ impl error::Error for DecoderError {
7872
DecoderError::IoError(ref inner) => inner.description(),
7973
DecoderError::FromUtf8Error(ref inner) => inner.description(),
8074
DecoderError::UnrecognizedElementType(_) => "unrecognized element type",
81-
DecoderError::InvalidArrayKey(_, _) => "invalid array key",
75+
DecoderError::InvalidArrayKey(..) => "invalid array key",
8276
DecoderError::ExpectedField(_) => "expected a field",
8377
DecoderError::UnknownField(_) => "found an unknown field",
8478
DecoderError::SyntaxError(ref inner) => inner,

0 commit comments

Comments
 (0)