Skip to content

Commit 55dad37

Browse files
committed
[#43] Support Symbol, rustfmt format
1 parent 7f097e5 commit 55dad37

File tree

11 files changed

+312
-274
lines changed

11 files changed

+312
-274
lines changed

src/bson.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub enum Bson {
5050
Binary(BinarySubtype, Vec<u8>),
5151
ObjectId(oid::ObjectId),
5252
UtcDatetime(DateTime<UTC>),
53+
Symbol(String),
5354
}
5455

5556
/// Alias for `Vec<Bson>`.
@@ -94,7 +95,8 @@ impl Display for Bson {
9495
&Bson::Binary(t, ref vec) =>
9596
write!(fmt, "BinData({}, 0x{})", u8::from(t), vec.to_hex()),
9697
&Bson::ObjectId(ref id) => write!(fmt, "ObjectId(\"{}\")", id),
97-
&Bson::UtcDatetime(date_time) => write!(fmt, "Date(\"{}\")", date_time)
98+
&Bson::UtcDatetime(date_time) => write!(fmt, "Date(\"{}\")", date_time),
99+
&Bson::Symbol(ref sym) => write!(fmt, "Symbol(\"{}\")", sym),
98100
}
99101
}
100102
}
@@ -229,6 +231,7 @@ impl Bson {
229231
&Bson::Binary(..) => ElementType::Binary,
230232
&Bson::ObjectId(..) => ElementType::ObjectId,
231233
&Bson::UtcDatetime(..) => ElementType::UtcDatetime,
234+
&Bson::Symbol(..) => ElementType::Symbol,
232235
}
233236
}
234237

@@ -295,10 +298,16 @@ impl Bson {
295298
&Bson::UtcDatetime(ref v) => {
296299
let mut obj = json::Object::new();
297300
let mut inner = json::Object::new();
298-
inner.insert("$numberLong".to_owned(), json::Json::I64((v.timestamp() * 1000) +
301+
inner.insert("$numberLong".to_owned(), json::Json::I64((v.timestamp() * 1000) +
299302
(v.nanosecond() / 1000000) as i64));
300303
obj.insert("$date".to_owned(), json::Json::Object(inner));
301304
json::Json::Object(obj)
305+
},
306+
&Bson::Symbol(ref v) => {
307+
// FIXME: Don't know what is the best way to encode Symbol type
308+
let mut obj = json::Object::new();
309+
obj.insert("$symbol".to_owned(), json::Json::String(v.to_owned()));
310+
json::Json::Object(obj)
302311
}
303312
}
304313
}
@@ -365,13 +374,18 @@ impl Bson {
365374
}
366375
}
367376
}
377+
Bson::Symbol(ref v) => {
378+
doc! {
379+
"$symbol" => (v.to_owned())
380+
}
381+
}
368382
_ => panic!("Attempted conversion of invalid data type: {}", self),
369383
}
370384
}
371385

372386
pub fn from_extended_document(values: Document) -> Bson {
373387
if values.len() == 2 {
374-
if let (Ok(pat), Ok(opt)) = (values.get_str("$regex"),
388+
if let (Ok(pat), Ok(opt)) = (values.get_str("$regex"),
375389
values.get_str("$options")) {
376390
return Bson::RegExp(pat.to_owned(), opt.to_owned());
377391

@@ -405,9 +419,11 @@ impl Bson {
405419
} else if let Ok(long) = values.get_document("$date")
406420
.and_then(|inner| inner.get_i64("$numberLong")) {
407421
return Bson::UtcDatetime(UTC.timestamp(long / 1000, (long % 1000) as u32 * 1000000));
422+
} else if let Ok(sym) = values.get_str("$symbol") {
423+
return Bson::Symbol(sym.to_owned());
408424
}
409425
}
410-
426+
411427
Bson::Document(values)
412428
}
413429
}

src/decoder/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl error::Error for DecoderError {
7878
match *self {
7979
DecoderError::IoError(ref inner) => Some(inner),
8080
DecoderError::Utf8Error(ref inner) => Some(inner),
81-
_ => None
81+
_ => None,
8282
}
8383
}
8484
}

src/decoder/mod.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use std::io::Read;
3131
use std::str;
3232

3333
use byteorder::{LittleEndian, ReadBytesExt};
34-
use chrono::{UTC};
34+
use chrono::UTC;
3535
use chrono::offset::TimeZone;
3636

3737
use spec::{self, BinarySubtype};
@@ -55,7 +55,9 @@ fn read_cstring<R: Read + ?Sized>(reader: &mut R) -> DecoderResult<String> {
5555

5656
loop {
5757
let c = try!(reader.read_u8());
58-
if c == 0 { break; }
58+
if c == 0 {
59+
break;
60+
}
5961
v.push(c);
6062
}
6163

@@ -123,9 +125,7 @@ fn decode_array<R: Read + ?Sized>(reader: &mut R) -> DecoderResult<Array> {
123125
fn decode_bson<R: Read + ?Sized>(reader: &mut R, tag: u8) -> DecoderResult<Bson> {
124126
use spec::ElementType::*;
125127
match spec::ElementType::from(tag) {
126-
Some(FloatingPoint) => {
127-
Ok(Bson::FloatingPoint(try!(reader.read_f64::<LittleEndian>())))
128-
},
128+
Some(FloatingPoint) => Ok(Bson::FloatingPoint(try!(reader.read_f64::<LittleEndian>()))),
129129
Some(Utf8String) => read_string(reader).map(Bson::String),
130130
Some(EmbeddedDocument) => decode_document(reader).map(Bson::Document),
131131
Some(Array) => decode_array(reader).map(Bson::Array),
@@ -149,7 +149,7 @@ fn decode_bson<R: Read + ?Sized>(reader: &mut R, tag: u8) -> DecoderResult<Bson>
149149
let pat = try!(read_cstring(reader));
150150
let opt = try!(read_cstring(reader));
151151
Ok(Bson::RegExp(pat, opt))
152-
},
152+
}
153153
Some(JavaScriptCode) => read_string(reader).map(Bson::JavaScriptCode),
154154
Some(JavaScriptCodeWithScope) => {
155155
// disregard the length:
@@ -159,20 +159,18 @@ fn decode_bson<R: Read + ?Sized>(reader: &mut R, tag: u8) -> DecoderResult<Bson>
159159
let code = try!(read_string(reader));
160160
let scope = try!(decode_document(reader));
161161
Ok(Bson::JavaScriptCodeWithScope(code, scope))
162-
},
162+
}
163163
Some(Integer32Bit) => read_i32(reader).map(Bson::I32),
164164
Some(Integer64Bit) => read_i64(reader).map(Bson::I64),
165165
Some(TimeStamp) => read_i64(reader).map(Bson::TimeStamp),
166166
Some(UtcDatetime) => {
167167
let time = try!(read_i64(reader));
168168
Ok(Bson::UtcDatetime(UTC.timestamp(time / 1000, (time % 1000) as u32 * 1000000)))
169-
},
170-
Some(Deprecated) |
171-
Some(Undefined) |
172-
Some(DbPointer) |
173-
Some(MaxKey) |
174-
Some(MinKey) |
175-
None => Err(DecoderError::UnrecognizedElementType(tag))
169+
}
170+
Some(Symbol) => read_string(reader).map(Bson::Symbol),
171+
Some(Undefined) | Some(DbPointer) | Some(MaxKey) | Some(MinKey) | None => {
172+
Err(DecoderError::UnrecognizedElementType(tag))
173+
}
176174
}
177175
}
178176

0 commit comments

Comments
 (0)