Skip to content

Commit aebe617

Browse files
committed
Add additional From traits, update macro tests to check validity of document
1 parent eb133f0 commit aebe617

File tree

2 files changed

+75
-52
lines changed

2 files changed

+75
-52
lines changed

src/bson.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ pub type Array = Vec<Bson>;
5454
/// Alias for `OrderedDocument`.
5555
pub type Document = OrderedDocument;
5656

57+
impl From<f32> for Bson {
58+
fn from(a: f32) -> Bson {
59+
Bson::FloatingPoint(a as f64)
60+
}
61+
}
62+
5763
impl From<f64> for Bson {
5864
fn from(a: f64) -> Bson {
5965
Bson::FloatingPoint(a)
@@ -123,6 +129,18 @@ impl From<i64> for Bson {
123129
}
124130
}
125131

132+
impl From<u32> for Bson {
133+
fn from(a: u32) -> Bson {
134+
Bson::I32(a as i32)
135+
}
136+
}
137+
138+
impl From<u64> for Bson {
139+
fn from(a: u64) -> Bson {
140+
Bson::I64(a as i64)
141+
}
142+
}
143+
126144
impl From<[u8; 12]> for Bson {
127145
fn from(a: [u8; 12]) -> Bson {
128146
Bson::ObjectId(oid::ObjectId::with_bytes(a))

tests/modules/macros.rs

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,4 @@
1-
use bson::{self, Bson, Document};
2-
3-
fn print_doc_with_indent_level(doc: &Document, n: i32) {
4-
macro_rules! print_spaces {
5-
( $n:expr ) => {
6-
for _ in 0..$n {
7-
print!(" ");
8-
}
9-
};
10-
}
11-
12-
println!("{{");
13-
14-
for (key, value) in doc.iter() {
15-
print_spaces!(n + 4);
16-
print!("{}: ", key);
17-
18-
print_bson_with_indent_level(value, n);
19-
println!("");
20-
}
21-
22-
print_spaces!(n);
23-
println!("}}");
24-
}
25-
26-
#[macro_export]
27-
macro_rules! print_doc {
28-
( $doc:expr ) => {
29-
print_doc_with_indent_level($doc, 0)
30-
};
31-
}
32-
33-
fn print_bson_with_indent_level(bson: &Bson, n: i32) {
34-
match bson {
35-
&Bson::Document(ref d) => print_doc_with_indent_level(&d, n + 4),
36-
&Bson::String(ref s) => print!("\"{}\",", s),
37-
&Bson::Boolean(b) => print!("{},", b),
38-
&Bson::Array(ref v) => {
39-
print!("[ ");
40-
41-
for b in v {
42-
print_bson_with_indent_level(b, n);
43-
print!(" ");
44-
}
45-
46-
print!("]");
47-
},
48-
ref bson => print!("{:?},", bson)
49-
};
50-
}
51-
1+
use bson::Bson;
522

533
#[test]
544
fn recursive_macro() {
@@ -64,5 +14,60 @@ fn recursive_macro() {
6414
"c" => [-7]
6515
};
6616

67-
print_doc!(&doc);
17+
match doc.get("a") {
18+
Some(&Bson::String(ref s)) => assert_eq!("foo", s),
19+
_ => panic!("String 'foo' was not inserted correctly."),
20+
}
21+
22+
// Inner Doc 1
23+
match doc.get("b") {
24+
Some(&Bson::Document(ref doc)) => {
25+
26+
// Inner doc 2
27+
match doc.get("bar") {
28+
Some(&Bson::Document(ref inner_doc)) => {
29+
30+
// Inner array
31+
match inner_doc.get("harbor") {
32+
Some(&Bson::Array(ref arr)) => {
33+
assert_eq!(2, arr.len());
34+
35+
// Match array items
36+
match arr[0] {
37+
Bson::String(ref s) => assert_eq!("seal", s),
38+
_ => panic!("String 'seal' was not inserted into inner array correctly."),
39+
}
40+
match arr[1] {
41+
Bson::Boolean(ref b) => assert!(!b),
42+
_ => panic!("Boolean 'false' was not inserted into inner array correctly."),
43+
}
44+
},
45+
_ => panic!("Inner array was not inserted correctly."),
46+
}
47+
48+
// Inner floating point
49+
match inner_doc.get("jelly") {
50+
Some(&Bson::FloatingPoint(ref fp)) => assert_eq!(42.0, *fp),
51+
_ => panic!("Floating point 42.0 was not inserted correctly."),
52+
}
53+
},
54+
_ => panic!("Second inner document was not inserted correctly."),
55+
}
56+
},
57+
_ => panic!("Inner document was not inserted correctly."),
58+
}
59+
60+
// Outer array
61+
match doc.get("c") {
62+
Some(&Bson::Array(ref arr)) => {
63+
assert_eq!(1, arr.len());
64+
65+
// Integer type
66+
match arr[0] {
67+
Bson::I32(ref i) => assert_eq!(-7, *i),
68+
_ => panic!("I32 '-7' was not inserted correctly."),
69+
}
70+
},
71+
_ => panic!("Array was not inserted correctly."),
72+
}
6873
}

0 commit comments

Comments
 (0)