Skip to content

Commit 9e51763

Browse files
NathanFlurryzonyitoo
authored andcommitted
Implemented Default for Bson, Document, and ObjectId (#82)
Added `Default` implementations * `Bson`, `Document`
1 parent 8805a57 commit 9e51763

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
target
22
Cargo.lock
33
.vscode
4+
.idea
45
*~
56
*.swp

src/bson.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ pub type Array = Vec<Bson>;
7979
/// Alias for `OrderedDocument`.
8080
pub type Document = OrderedDocument;
8181

82+
impl Default for Bson {
83+
fn default() -> Self {
84+
Bson::Null
85+
}
86+
}
87+
8288
impl Debug for Bson {
8389
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8490
match self {

src/ordered.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ pub struct OrderedDocument {
6060
inner: LinkedHashMap<String, Bson>,
6161
}
6262

63+
impl Default for OrderedDocument {
64+
fn default() -> Self {
65+
Document::new()
66+
}
67+
}
68+
6369
impl Display for OrderedDocument {
6470
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
6571
try!(write!(fmt, "{{"));

tests/modules/bson.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
extern crate serde_json;
2+
13
use bson::{Bson, Document};
4+
use self::serde_json::Value;
25

36
#[test]
47
fn to_json() {
58
let mut doc = Document::new();
69
doc.insert("first", Bson::I32(1));
710
doc.insert("second", Bson::String("foo".to_owned()));
811
doc.insert("alphanumeric", Bson::String("bar".to_owned()));
9-
let data = Bson::Document(doc).to_json();
12+
let data: Value = Bson::Document(doc).clone().into();
1013

1114
assert!(data.is_object());
1215
let obj = data.as_object().unwrap();
@@ -23,3 +26,16 @@ fn to_json() {
2326
assert!(alphanumeric.is_string());
2427
assert_eq!(alphanumeric.as_str().unwrap(), "bar");
2528
}
29+
30+
#[test]
31+
fn bson_default() {
32+
let bson1 = Bson::default();
33+
assert_eq!(bson1, Bson::Null);
34+
}
35+
36+
#[test]
37+
fn document_default() {
38+
let doc1 = Document::default();
39+
assert_eq!(doc1.keys().count(), 0);
40+
assert_eq!(doc1, Document::new());
41+
}

tests/modules/encoder_decoder.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
extern crate bson;
2-
extern crate chrono;
3-
41
use std::io::Cursor;
52
use bson::{Bson, decode_document, encode_document};
63
use bson::oid::ObjectId;

0 commit comments

Comments
 (0)