From 18301441252deb8791f25c6ff4e1d58b2c50555c Mon Sep 17 00:00:00 2001 From: Thijs Cadier Date: Tue, 25 Aug 2015 23:26:00 +0200 Subject: [PATCH] Replace all bson document handling with macro --- src/bsonc.rs | 5 +---- src/bulk_operation.rs | 29 ++++++++++++++--------------- src/collection.rs | 35 ++++++++++++++++------------------- src/cursor.rs | 23 ++++++++++------------- src/database.rs | 4 +--- src/lib.rs | 1 + 6 files changed, 43 insertions(+), 54 deletions(-) diff --git a/src/bsonc.rs b/src/bsonc.rs index 591b296..b39c9f2 100644 --- a/src/bsonc.rs +++ b/src/bsonc.rs @@ -122,12 +122,9 @@ impl Drop for Bsonc { #[cfg(test)] mod tests { - use bson; - #[test] fn test_bsonc_from_and_as_document() { - let mut document = bson::Document::new(); - document.insert("key".to_string(), bson::Bson::String("value".to_string())); + let document = doc! { "key" => "value" }; let bsonc = super::Bsonc::from_document(&document).unwrap(); diff --git a/src/bulk_operation.rs b/src/bulk_operation.rs index 142b906..ac47a11 100644 --- a/src/bulk_operation.rs +++ b/src/bulk_operation.rs @@ -232,9 +232,10 @@ mod tests { { let bulk_operation = collection.create_bulk_operation(None); - let mut document = bson::Document::new(); - document.insert("key_1".to_string(), bson::Bson::String("Value 1".to_string())); - document.insert("key_2".to_string(), bson::Bson::String("Value 2".to_string())); + let document = doc! { + "key_1" => "Value 1", + "key_2" => "Value 2" + }; for _ in 0..5 { bulk_operation.insert(&document).unwrap(); } @@ -246,15 +247,14 @@ mod tests { result.ok().unwrap().get("nInserted").unwrap().to_json(), bson::Bson::I32(5).to_json() ); - assert_eq!(5, collection.count(&bson::Document::new(), None).unwrap()); + assert_eq!(5, collection.count(&doc!{}, None).unwrap()); } - let query = bson::Document::new(); + let query = doc!{}; - let mut update_document = bson::Document::new(); - let mut set = bson::Document::new(); - set.insert("key_1".to_string(), bson::Bson::String("Value update".to_string())); - update_document.insert("$set".to_string(), bson::Bson::Document(set)); + let update_document = doc! { + "$set" => {"key_1" => "Value update"} + }; // Update one { @@ -274,7 +274,7 @@ mod tests { bson::Bson::I32(1).to_json() ); - let first_document = collection.find(&bson::Document::new(), None).unwrap().next().unwrap().unwrap(); + let first_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); assert_eq!( first_document.get("key_1").unwrap().to_json(), bson::Bson::String("Value update".to_string()).to_json() @@ -301,8 +301,8 @@ mod tests { bson::Bson::I32(4).to_json() ); - collection.find(&bson::Document::new(), None).unwrap().next().unwrap().unwrap(); - let second_document = collection.find(&bson::Document::new(), None).unwrap().next().unwrap().unwrap(); + collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); + let second_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); assert_eq!( second_document.get("key_1").unwrap().to_json(), bson::Bson::String("Value update".to_string()).to_json() @@ -313,8 +313,7 @@ mod tests { // Replace one { - let mut replace_document = bson::Document::new(); - replace_document.insert("key_1".to_string(), bson::Bson::String("Value replace".to_string())); + let replace_document = doc! { "key_1" => "Value replace" }; let bulk_operation = collection.create_bulk_operation(None); bulk_operation.replace_one( @@ -331,7 +330,7 @@ mod tests { bson::Bson::I32(1).to_json() ); - let first_document = collection.find(&bson::Document::new(), None).unwrap().next().unwrap().unwrap(); + let first_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); assert_eq!( first_document.get("key_1").unwrap().to_json(), bson::Bson::String("Value replace".to_string()).to_json() diff --git a/src/collection.rs b/src/collection.rs index d510029..74a5257 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -4,7 +4,7 @@ use std::borrow::Cow; use mongo_c_driver_wrapper::bindings; -use bson::{Bson,Document}; +use bson::Document; use super::Result; use super::CommandAndFindOptions; @@ -378,12 +378,10 @@ impl<'a> Collection<'a> { find_options: Option, tail_options: Option ) -> TailingCursor<'a> { - let mut query_with_options = Document::new(); - query_with_options.insert( - "$query".to_string(), - Bson::Document(query) - ); - query_with_options.insert("$natural".to_string(), Bson::I32(1)); + let query_with_options = doc! { + "$query" => query, + "$natural" => 1 + }; TailingCursor::new( self, @@ -417,8 +415,7 @@ mod tests { let client = pool.pop(); let collection = client.get_collection("rust_driver_test", "items"); - let mut command = bson::Document::new(); - command.insert("ping".to_string(), bson::Bson::I32(1)); + let command = doc! { "ping" => 1 }; let result = collection.command(command, None).unwrap().next().unwrap().unwrap(); assert!(result.contains_key("ok")); @@ -434,16 +431,18 @@ mod tests { assert_eq!("items", collection.get_name().to_mut()); - let mut document = bson::Document::new(); - document.insert("key_1".to_string(), bson::Bson::String("Value 1".to_string())); - document.insert("key_2".to_string(), bson::Bson::String("Value 2".to_string())); + let document = doc! { + "key_1" => "Value 1", + "key_2" => "Value 2" + }; assert!(collection.insert(&document, None).is_ok()); - let mut second_document = bson::Document::new(); - second_document.insert("key_1".to_string(), bson::Bson::String("Value 3".to_string())); + let second_document = doc! { + "key_1" => "Value 3" + }; assert!(collection.insert(&second_document, None).is_ok()); - let query = bson::Document::new(); + let query = doc!{}; // Count the documents in the collection assert_eq!(2, collection.count(&query, None).unwrap()); @@ -486,14 +485,12 @@ mod tests { // Find the document with fields set { - let mut fields = bson::Document::new(); - fields.insert("key_1".to_string(), bson::Bson::Boolean(true)); let options = super::super::CommandAndFindOptions { query_flags: flags::Flags::new(), skip: 0, limit: 0, batch_size: 0, - fields: Some(fields), + fields: Some(doc! { "key_1" => true }), read_prefs: None }; @@ -520,7 +517,7 @@ mod tests { let pool = ClientPool::new(uri, None); let client = pool.pop(); let collection = client.get_collection("rust_driver_test", "items"); - let document = bson::Document::new(); + let document = doc! {}; let result = collection.insert(&document, None); assert!(result.is_err()); diff --git a/src/cursor.rs b/src/cursor.rs index 25dc22c..aa60b59 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -179,9 +179,7 @@ impl<'a> Iterator for TailingCursor<'a> { // Add the last seen id to the query if it's present. match self.last_seen_id.take() { Some(id) => { - let mut gt_id = Document::new(); - gt_id.insert("$gt".to_string(), Bson::ObjectId(id)); - self.query.insert("_id".to_string(), Bson::Document(gt_id)); + self.query.insert("_id".to_string(), Bson::Document(doc! { "$gt" => id })); }, None => () }; @@ -245,15 +243,14 @@ mod tests { let client = pool.pop(); let mut collection = client.get_collection("rust_driver_test", "cursor_items"); - let mut document = bson::Document::new(); - document.insert("key".to_string(), bson::Bson::String("value".to_string())); + let document = doc! { "key" => "value" }; collection.drop().unwrap_or(()); for _ in 0..10 { assert!(collection.insert(&document, None).is_ok()); } - let query = bson::Document::new(); + let query = doc! {}; let cursor = collection.find(&query, None).unwrap(); assert!(cursor.is_alive()); @@ -275,14 +272,15 @@ mod tests { database.get_collection("capped").drop().unwrap_or(()); database.get_collection("not_capped").drop().unwrap_or(()); - let mut options = bson::Document::new(); - options.insert("capped".to_string(), bson::Bson::Boolean(true)); - options.insert("size".to_string(), bson::Bson::I32(100000)); + let options = doc! { + "capped" => true, + "size" => 100000 + }; let capped_collection = database.create_collection("capped", Some(&options)).unwrap(); let normal_collection = database.create_collection("not_capped", None).unwrap(); // Try to tail on a normal collection - let failing_cursor = normal_collection.tail(bson::Document::new(), None, None); + let failing_cursor = normal_collection.tail(doc!{}, None, None); let failing_result = failing_cursor.into_iter().next().unwrap(); assert!(failing_result.is_err()); assert_eq!( @@ -290,8 +288,7 @@ mod tests { format!("{:?}", failing_result.err().unwrap()) ); - let mut document = bson::Document::new(); - document.insert("key_1".to_string(), bson::Bson::String("Value 1".to_string())); + let document = doc! { "key_1" => "Value 1" }; // Insert a first document into the collection capped_collection.insert(&document, None).unwrap(); @@ -300,7 +297,7 @@ mod tests { let guard = thread::spawn(move || { let client = cloned_pool.pop(); let collection = client.get_collection("rust_test", "capped"); - let cursor = collection.tail(bson::Document::new(), None, None); + let cursor = collection.tail(doc!{}, None, None); let mut counter = 0usize; for result in cursor.into_iter() { assert!(result.is_ok()); diff --git a/src/database.rs b/src/database.rs index 7d8b4b1..7814561 100644 --- a/src/database.rs +++ b/src/database.rs @@ -133,7 +133,6 @@ impl<'a> Drop for Database<'a> { #[cfg(test)] mod tests { - use bson; use super::super::uri::Uri; use super::super::client::ClientPool; @@ -144,8 +143,7 @@ mod tests { let client = pool.pop(); let database = client.get_database("rust_test"); - let mut command = bson::Document::new(); - command.insert("ping".to_string(), bson::Bson::I32(1)); + let command = doc! { "ping" => 1 }; let result = database.command(command, None).unwrap().next().unwrap().unwrap(); assert!(result.contains_key("ok")); diff --git a/src/lib.rs b/src/lib.rs index 1d2be72..5e8bf15 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ extern crate libc; extern crate mongo_c_driver_wrapper; +#[macro_use] extern crate bson; use std::result;