Skip to content

Commit 80ec808

Browse files
committed
update rustfmt to same style as the driver
1 parent 4ade1d6 commit 80ec808

File tree

22 files changed

+773
-390
lines changed

22 files changed

+773
-390
lines changed

examples/encode.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
use std::io::Cursor;
32

43
use bson::{decode_document, encode_document, oid, Array, Bson, Document};
@@ -11,7 +10,9 @@ fn main() {
1110
let mut arr = Array::new();
1211
arr.push(Bson::String("blah".to_string()));
1312
arr.push(Bson::UtcDatetime(chrono::Utc::now()));
14-
arr.push(Bson::ObjectId(oid::ObjectId::with_bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])));
13+
arr.push(Bson::ObjectId(oid::ObjectId::with_bytes([
14+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
15+
])));
1516

1617
doc.insert("array".to_string(), Bson::Array(arr));
1718

src/bson.rs

Lines changed: 70 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,22 @@
2121

2222
//! BSON definition
2323
24-
use std::fmt::{self, Debug, Display};
25-
use std::ops::{Deref, DerefMut};
24+
use std::{
25+
fmt::{self, Debug, Display},
26+
ops::{Deref, DerefMut},
27+
};
2628

27-
use chrono::offset::TimeZone;
28-
use chrono::{DateTime, Timelike, Utc};
29+
use chrono::{offset::TimeZone, DateTime, Timelike, Utc};
2930
use hex;
30-
use serde_json::{Value, json};
31+
use serde_json::{json, Value};
3132

3233
#[cfg(feature = "decimal128")]
3334
use crate::decimal128::Decimal128;
34-
use crate::oid;
35-
use crate::ordered::OrderedDocument;
36-
use crate::spec::{BinarySubtype, ElementType};
35+
use crate::{
36+
oid,
37+
ordered::OrderedDocument,
38+
spec::{BinarySubtype, ElementType},
39+
};
3740

3841
/// Possible BSON value types.
3942
#[derive(Clone, PartialEq)]
@@ -50,11 +53,12 @@ pub enum Bson {
5053
Boolean(bool),
5154
/// Null value
5255
Null,
53-
/// Regular expression - The first cstring is the regex pattern, the second is the regex options string.
54-
/// Options are identified by characters, which must be stored in alphabetical order.
55-
/// Valid options are 'i' for case insensitive matching, 'm' for multiline matching, 'x' for verbose mode,
56-
/// 'l' to make \w, \W, etc. locale dependent, 's' for dotall mode ('.' matches everything), and 'u' to
57-
/// make \w, \W, etc. match unicode.
56+
/// Regular expression - The first cstring is the regex pattern, the second is the regex
57+
/// options string. Options are identified by characters, which must be stored in
58+
/// alphabetical order. Valid options are 'i' for case insensitive matching, 'm' for
59+
/// multiline matching, 'x' for verbose mode, 'l' to make \w, \W, etc. locale dependent,
60+
/// 's' for dotall mode ('.' matches everything), and 'u' to make \w, \W, etc. match
61+
/// unicode.
5862
RegExp(String, String),
5963
/// JavaScript code
6064
JavaScriptCode(String),
@@ -112,7 +116,9 @@ impl Debug for Bson {
112116

113117
write!(f, "TimeStamp({}, {})", time, inc)
114118
}
115-
Bson::Binary(t, ref vec) => write!(f, "BinData({}, 0x{})", u8::from(t), hex::encode(vec)),
119+
Bson::Binary(t, ref vec) => {
120+
write!(f, "BinData({}, 0x{})", u8::from(t), hex::encode(vec))
121+
}
116122
Bson::ObjectId(ref id) => write!(f, "ObjectId({:?})", id),
117123
Bson::UtcDatetime(date_time) => write!(f, "UtcDatetime({:?})", date_time),
118124
Bson::Symbol(ref sym) => write!(f, "Symbol({:?})", sym),
@@ -146,7 +152,9 @@ impl Display for Bson {
146152
Bson::Boolean(b) => write!(fmt, "{}", b),
147153
Bson::Null => write!(fmt, "null"),
148154
Bson::RegExp(ref pat, ref opt) => write!(fmt, "/{}/{}", pat, opt),
149-
Bson::JavaScriptCode(ref s) | Bson::JavaScriptCodeWithScope(ref s, _) => fmt.write_str(&s),
155+
Bson::JavaScriptCode(ref s) | Bson::JavaScriptCodeWithScope(ref s, _) => {
156+
fmt.write_str(&s)
157+
}
150158
Bson::I32(i) => write!(fmt, "{}", i),
151159
Bson::I64(i) => write!(fmt, "{}", i),
152160
Bson::TimeStamp(i) => {
@@ -155,7 +163,9 @@ impl Display for Bson {
155163

156164
write!(fmt, "Timestamp({}, {})", time, inc)
157165
}
158-
Bson::Binary(t, ref vec) => write!(fmt, "BinData({}, 0x{})", u8::from(t), hex::encode(vec)),
166+
Bson::Binary(t, ref vec) => {
167+
write!(fmt, "BinData({}, 0x{})", u8::from(t), hex::encode(vec))
168+
}
159169
Bson::ObjectId(ref id) => write!(fmt, "ObjectId(\"{}\")", id),
160170
Bson::UtcDatetime(date_time) => write!(fmt, "Date(\"{}\")", date_time),
161171
Bson::Symbol(ref sym) => write!(fmt, "Symbol(\"{}\")", sym),
@@ -221,7 +231,7 @@ impl From<(BinarySubtype, Vec<u8>)> for Bson {
221231

222232
impl<T> From<&T> for Bson
223233
where
224-
T: Clone + Into<Bson>
234+
T: Clone + Into<Bson>,
225235
{
226236
fn from(t: &T) -> Bson {
227237
t.clone().into()
@@ -230,7 +240,7 @@ where
230240

231241
impl<T> From<Vec<T>> for Bson
232242
where
233-
T: Into<Bson>
243+
T: Into<Bson>,
234244
{
235245
fn from(v: Vec<T>) -> Bson {
236246
Bson::Array(v.into_iter().map(|val| val.into()).collect())
@@ -239,7 +249,7 @@ where
239249

240250
impl<T> From<&[T]> for Bson
241251
where
242-
T: Clone + Into<Bson>
252+
T: Clone + Into<Bson>,
243253
{
244254
fn from(s: &[T]) -> Bson {
245255
Bson::Array(s.into_iter().cloned().map(|val| val.into()).collect())
@@ -307,15 +317,18 @@ impl From<DateTime<Utc>> for Bson {
307317
impl From<Value> for Bson {
308318
fn from(a: Value) -> Bson {
309319
match a {
310-
Value::Number(x) => x.as_i64()
311-
.map(Bson::from)
312-
.or_else(|| x.as_u64().map(Bson::from))
313-
.or_else(|| x.as_f64().map(Bson::from))
314-
.unwrap_or_else(|| panic!("Invalid number value: {}", x)),
320+
Value::Number(x) => x
321+
.as_i64()
322+
.map(Bson::from)
323+
.or_else(|| x.as_u64().map(Bson::from))
324+
.or_else(|| x.as_f64().map(Bson::from))
325+
.unwrap_or_else(|| panic!("Invalid number value: {}", x)),
315326
Value::String(x) => x.into(),
316327
Value::Bool(x) => x.into(),
317328
Value::Array(x) => Bson::Array(x.into_iter().map(Bson::from).collect()),
318-
Value::Object(x) => Bson::from_extended_document(x.into_iter().map(|(k, v)| (k, v.into())).collect()),
329+
Value::Object(x) => {
330+
Bson::from_extended_document(x.into_iter().map(|(k, v)| (k, v.into())).collect())
331+
}
319332
Value::Null => Bson::Null,
320333
}
321334
}
@@ -489,7 +502,9 @@ impl Bson {
489502
if values.len() == 2 {
490503
if let (Ok(pat), Ok(opt)) = (values.get_str("$regex"), values.get_str("$options")) {
491504
return Bson::RegExp(pat.to_owned(), opt.to_owned());
492-
} else if let (Ok(code), Ok(scope)) = (values.get_str("$code"), values.get_document("$scope")) {
505+
} else if let (Ok(code), Ok(scope)) =
506+
(values.get_str("$code"), values.get_document("$scope"))
507+
{
493508
return Bson::JavaScriptCodeWithScope(code.to_owned(), scope.to_owned());
494509
} else if let (Ok(t), Ok(i)) = (values.get_i32("t"), values.get_i32("i")) {
495510
let timestamp = ((t as i64) << 32) + (i as i64);
@@ -499,17 +514,24 @@ impl Bson {
499514
return Bson::TimeStamp(timestamp);
500515
} else if let (Ok(hex), Ok(t)) = (values.get_str("$binary"), values.get_i64("type")) {
501516
let ttype = t as u8;
502-
return Bson::Binary(From::from(ttype), hex::decode(hex.as_bytes()).expect("$binary value is not a valid Hex encoded bytes"));
517+
return Bson::Binary(
518+
From::from(ttype),
519+
hex::decode(hex.as_bytes())
520+
.expect("$binary value is not a valid Hex encoded bytes"),
521+
);
503522
}
504523
} else if values.len() == 1 {
505524
if let Ok(code) = values.get_str("$code") {
506525
return Bson::JavaScriptCode(code.to_owned());
507526
} else if let Ok(hex) = values.get_str("$oid") {
508527
return Bson::ObjectId(oid::ObjectId::with_string(hex).unwrap());
509-
} else if let Ok(long) = values.get_document("$date")
510-
.and_then(|inner| inner.get_i64("$numberLong"))
528+
} else if let Ok(long) = values
529+
.get_document("$date")
530+
.and_then(|inner| inner.get_i64("$numberLong"))
511531
{
512-
return Bson::UtcDatetime(Utc.timestamp(long / 1000, ((long % 1000) * 1_000_000) as u32));
532+
return Bson::UtcDatetime(
533+
Utc.timestamp(long / 1000, ((long % 1000) * 1_000_000) as u32),
534+
);
513535
} else if let Ok(sym) = values.get_str("$symbol") {
514536
return Bson::Symbol(sym.to_owned());
515537
} else if let Ok(dec) = values.get_str("$numberDecimal") {
@@ -528,7 +550,9 @@ impl Bson {
528550
if values.len() == 2 {
529551
if let (Ok(pat), Ok(opt)) = (values.get_str("$regex"), values.get_str("$options")) {
530552
return Bson::RegExp(pat.to_owned(), opt.to_owned());
531-
} else if let (Ok(code), Ok(scope)) = (values.get_str("$code"), values.get_document("$scope")) {
553+
} else if let (Ok(code), Ok(scope)) =
554+
(values.get_str("$code"), values.get_document("$scope"))
555+
{
532556
return Bson::JavaScriptCodeWithScope(code.to_owned(), scope.to_owned());
533557
} else if let (Ok(t), Ok(i)) = (values.get_i32("t"), values.get_i32("i")) {
534558
let timestamp = ((t as i64) << 32) + (i as i64);
@@ -538,17 +562,24 @@ impl Bson {
538562
return Bson::TimeStamp(timestamp);
539563
} else if let (Ok(hex), Ok(t)) = (values.get_str("$binary"), values.get_i64("type")) {
540564
let ttype = t as u8;
541-
return Bson::Binary(From::from(ttype), hex::decode(hex.as_bytes()).expect("$binary value is not a valid Hex encoded bytes"));
565+
return Bson::Binary(
566+
From::from(ttype),
567+
hex::decode(hex.as_bytes())
568+
.expect("$binary value is not a valid Hex encoded bytes"),
569+
);
542570
}
543571
} else if values.len() == 1 {
544572
if let Ok(code) = values.get_str("$code") {
545573
return Bson::JavaScriptCode(code.to_owned());
546574
} else if let Ok(hex) = values.get_str("$oid") {
547575
return Bson::ObjectId(oid::ObjectId::with_string(hex).unwrap());
548-
} else if let Ok(long) = values.get_document("$date")
549-
.and_then(|inner| inner.get_i64("$numberLong"))
576+
} else if let Ok(long) = values
577+
.get_document("$date")
578+
.and_then(|inner| inner.get_i64("$numberLong"))
550579
{
551-
return Bson::UtcDatetime(Utc.timestamp(long / 1000, ((long % 1000) * 1_000_000) as u32));
580+
return Bson::UtcDatetime(
581+
Utc.timestamp(long / 1000, ((long % 1000) * 1_000_000) as u32),
582+
);
552583
} else if let Ok(sym) = values.get_str("$symbol") {
553584
return Bson::Symbol(sym.to_owned());
554585
}
@@ -568,7 +599,8 @@ impl Bson {
568599
}
569600
}
570601

571-
/// If `Bson` is `FloatingPoint`, return a mutable reference to its value. Returns `None` otherwise
602+
/// If `Bson` is `FloatingPoint`, return a mutable reference to its value. Returns `None`
603+
/// otherwise
572604
pub fn as_f64_mut(&mut self) -> Option<&mut f64> {
573605
match *self {
574606
Bson::FloatingPoint(ref mut v) => Some(v),
@@ -696,7 +728,8 @@ impl Bson {
696728
}
697729
}
698730

699-
/// If `Bson` is `UtcDateTime`, return a mutable reference to its value. Returns `None` otherwise
731+
/// If `Bson` is `UtcDateTime`, return a mutable reference to its value. Returns `None`
732+
/// otherwise
700733
pub fn as_utc_date_time_mut(&mut self) -> Option<&mut DateTime<Utc>> {
701734
match *self {
702735
Bson::UtcDatetime(ref mut v) => Some(v),

src/compat/u2f.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ impl ToF64 for u64 {
3434

3535
/// Serialize unsigned types to `Bson::FloatingPoint`
3636
pub fn serialize<T, S>(v: &T, s: S) -> Result<S::Ok, S::Error>
37-
where T: ToF64,
38-
S: Serializer
37+
where
38+
T: ToF64,
39+
S: Serializer,
3940
{
4041
s.serialize_f64(v.to_f64())
4142
}
@@ -72,8 +73,9 @@ impl FromF64 for u64 {
7273

7374
/// Deserialize unsigned types to `Bson::FloatingPoint`
7475
pub fn deserialize<'de, T, D>(d: D) -> Result<T, D::Error>
75-
where D: Deserializer<'de>,
76-
T: FromF64
76+
where
77+
D: Deserializer<'de>,
78+
T: FromF64,
7779
{
7880
f64::deserialize(d).map(T::from_f64)
7981
}

src/decimal128.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! [BSON Decimal128](https://github.com/mongodb/specifications/blob/master/source/bson-decimal128/decimal128.rst) data type representation
22
3-
use std::fmt;
4-
use std::str::FromStr;
3+
use std::{fmt, str::FromStr};
54

65
use decimal::d128;
76

@@ -26,7 +25,9 @@ impl Decimal128 {
2625
/// let dec128 = Decimal128::from_str("1.05E+3");
2726
/// ```
2827
pub fn from_str(s: &str) -> Decimal128 {
29-
Decimal128 { inner: s.parse::<d128>().expect("Invalid Decimal128 string"), }
28+
Decimal128 {
29+
inner: s.parse::<d128>().expect("Invalid Decimal128 string"),
30+
}
3031
}
3132

3233
/// Construct a `Decimal128` from a `i32` number.
@@ -38,7 +39,9 @@ impl Decimal128 {
3839
/// let dec128 = Decimal128::from_i32(num);
3940
/// ```
4041
pub fn from_i32(d: i32) -> Decimal128 {
41-
Decimal128 { inner: From::from(d) }
42+
Decimal128 {
43+
inner: From::from(d),
44+
}
4245
}
4346

4447
/// Construct a `Decimal128` from a `u32` number.
@@ -50,7 +53,9 @@ impl Decimal128 {
5053
/// let dec128 = Decimal128::from_u32(num);
5154
/// ```
5255
pub fn from_u32(d: u32) -> Decimal128 {
53-
Decimal128 { inner: From::from(d) }
56+
Decimal128 {
57+
inner: From::from(d),
58+
}
5459
}
5560

5661
/// Construct a `Decimal128` from a `i32` number.
@@ -89,7 +94,9 @@ impl Decimal128 {
8994
/// let dec128 = Decimal128::zero();
9095
/// ```
9196
pub fn zero() -> Decimal128 {
92-
Decimal128 { inner: d128::zero() }
97+
Decimal128 {
98+
inner: d128::zero(),
99+
}
93100
}
94101

95102
#[doc(hidden)]
@@ -98,7 +105,9 @@ impl Decimal128 {
98105
raw.reverse();
99106
}
100107

101-
Decimal128 { inner: d128::from_raw_bytes(raw), }
108+
Decimal128 {
109+
inner: d128::from_raw_bytes(raw),
110+
}
102111
}
103112

104113
#[doc(hidden)]

src/decoder/error.rs

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

43
use serde::de::{self, Expected, Unexpected};
54

@@ -53,16 +52,22 @@ impl fmt::Display for DecoderError {
5352
match *self {
5453
DecoderError::IoError(ref inner) => inner.fmt(fmt),
5554
DecoderError::FromUtf8Error(ref inner) => inner.fmt(fmt),
56-
DecoderError::UnrecognizedElementType(tag) => write!(fmt, "unrecognized element type `{}`", tag),
55+
DecoderError::UnrecognizedElementType(tag) => {
56+
write!(fmt, "unrecognized element type `{}`", tag)
57+
}
5758
DecoderError::InvalidArrayKey(ref want, ref got) => {
5859
write!(fmt, "invalid array key: expected `{}`, got `{}`", want, got)
5960
}
60-
DecoderError::ExpectedField(field_type) => write!(fmt, "expected a field of type `{}`", field_type),
61+
DecoderError::ExpectedField(field_type) => {
62+
write!(fmt, "expected a field of type `{}`", field_type)
63+
}
6164
DecoderError::UnknownField(ref field) => write!(fmt, "unknown field `{}`", field),
6265
DecoderError::SyntaxError(ref inner) => inner.fmt(fmt),
6366
DecoderError::EndOfStream => fmt.write_str("end of stream"),
6467
DecoderError::InvalidType(ref desc) => desc.fmt(fmt),
65-
DecoderError::InvalidLength(ref len, ref desc) => write!(fmt, "expecting length {}, {}", len, desc),
68+
DecoderError::InvalidLength(ref len, ref desc) => {
69+
write!(fmt, "expecting length {}, {}", len, desc)
70+
}
6671
DecoderError::DuplicatedField(ref field) => write!(fmt, "duplicated field `{}`", field),
6772
DecoderError::UnknownVariant(ref var) => write!(fmt, "unknown variant `{}`", var),
6873
DecoderError::InvalidValue(ref desc) => desc.fmt(fmt),

0 commit comments

Comments
 (0)