Skip to content

Commit d6702e7

Browse files
committed
Merge pull request #26 from thijsc/get_binary_generic
Add get binary generic accessor
2 parents 2ab37ad + b8e99a6 commit d6702e7

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/ordered.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use chrono::{DateTime, UTC};
22
use bson::{Array,Bson,Document};
3+
use super::spec::BinarySubtype;
34
use super::oid::ObjectId;
45
use std::collections::BTreeMap;
56
use std::error;
@@ -270,6 +271,15 @@ impl OrderedDocument {
270271
}
271272
}
272273

274+
/// Get a generic binary value for this key if it exists and has the correct type.
275+
pub fn get_binary_generic(&self, key: &str) -> ValueAccessResult<&Vec<u8>> {
276+
match self.get(key) {
277+
Some(&Bson::Binary(BinarySubtype::Generic, ref v)) => Ok(v),
278+
Some(_) => Err(ValueAccessError::UnexpectedType),
279+
None => Err(ValueAccessError::NotPresent)
280+
}
281+
}
282+
273283
/// Get an object id value for this key if it exists and has the correct type.
274284
pub fn get_object_id(&self, key: &str) -> ValueAccessResult<&ObjectId> {
275285
match self.get(key) {

tests/modules/ordered.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use bson::{Bson, Document};
22
use bson::ValueAccessError;
3+
use bson::spec::BinarySubtype;
34
use bson::oid::ObjectId;
45
use chrono::UTC;
56

@@ -41,6 +42,7 @@ fn ordered_insert_shorthand() {
4142
fn test_getters() {
4243
let datetime = UTC::now();
4344
let cloned_dt = datetime.clone();
45+
let binary = vec![0, 1, 2, 3, 4];
4446
let mut doc = doc! {
4547
"floating_point" => 10.0,
4648
"string" => "a value",
@@ -49,7 +51,8 @@ fn test_getters() {
4951
"bool" => true,
5052
"i32" => 1i32,
5153
"i64" => 1i64,
52-
"datetime" => cloned_dt
54+
"datetime" => cloned_dt,
55+
"binary" => (BinarySubtype::Generic, binary.clone())
5356
};
5457

5558
assert_eq!(None, doc.get("nonsense"));
@@ -88,13 +91,16 @@ fn test_getters() {
8891
assert_eq!(Some(&Bson::TimeStamp(100)), doc.get("timestamp"));
8992
assert_eq!(Ok(100i64), doc.get_time_stamp("timestamp"));
9093

94+
assert_eq!(Some(&Bson::UtcDatetime(datetime.clone())), doc.get("datetime"));
95+
assert_eq!(Ok(&datetime), doc.get_utc_datetime("datetime"));
96+
9197
let object_id = ObjectId::new().unwrap();
9298
doc.insert("_id".to_string(), Bson::ObjectId(object_id.clone()));
9399
assert_eq!(Some(&Bson::ObjectId(object_id.clone())), doc.get("_id"));
94100
assert_eq!(Ok(&object_id), doc.get_object_id("_id"));
95101

96-
assert_eq!(Some(&Bson::UtcDatetime(datetime.clone())), doc.get("datetime"));
97-
assert_eq!(Ok(&datetime), doc.get_utc_datetime("datetime"));
102+
assert_eq!(Some(&Bson::Binary(BinarySubtype::Generic, binary.clone())), doc.get("binary"));
103+
assert_eq!(Ok(&binary), doc.get_binary_generic("binary"));
98104
}
99105

100106
#[test]

0 commit comments

Comments
 (0)