From 2123095be003aea3fbf2502e7caf51da6b67cd20 Mon Sep 17 00:00:00 2001 From: Thijs Cadier Date: Sun, 24 Jan 2021 21:55:59 +0100 Subject: [PATCH] Bump bson to 1.1 --- Cargo.toml | 2 +- src/bsonc.rs | 33 ++++++--------------------------- src/client.rs | 4 ++-- src/collection.rs | 6 +++--- src/cursor.rs | 4 ++-- src/database.rs | 2 +- src/error.rs | 16 +++++++++------- tests/bson_encode_decode.rs | 25 ++++++++++++++----------- tests/bulk_operation.rs | 26 +++++++++++++------------- tests/client.rs | 8 ++++---- tests/collection.rs | 34 +++++++++++++++++----------------- tests/cursor.rs | 12 ++++++------ tests/database.rs | 6 +++--- 13 files changed, 81 insertions(+), 97 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index de60dfc..6875011 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ is-it-maintained-open-issues = { repository = "thijsc/mongo-rust-driver" } [dependencies] libc = "^0.2" log = "^0.4" -bson = "^0.12" +bson = "^1.1" serde = "1.0" serde_derive = "1.0" diff --git a/src/bsonc.rs b/src/bsonc.rs index 7fdf221..cc644de 100644 --- a/src/bsonc.rs +++ b/src/bsonc.rs @@ -25,7 +25,7 @@ impl Bsonc { pub fn from_document(document: &bson::Document) -> Result { let mut buffer = Vec::new(); - bson::encode_document(&mut buffer, document)?; + document.to_writer(&mut buffer)?; let inner = unsafe { bindings::bson_new_from_data( @@ -60,28 +60,7 @@ impl Bsonc { slice::from_raw_parts(data_ptr, data_len) }; - Ok(bson::decode_document(&mut slice)?) - } - - /// Decode a bson from the C side to a document with lossy UTF-8 decoding - pub fn as_document_utf8_lossy(&self) -> Result { - assert!(!self.inner.is_null()); - - // This pointer should not be modified or freed - // See: http://mongoc.org/libbson/current/bson_get_data.html - let data_ptr = unsafe { bindings::bson_get_data(self.inner) }; - assert!(!data_ptr.is_null()); - - let data_len = unsafe { - let bson = *self.inner; - bson.len - } as usize; - - let mut slice = unsafe { - slice::from_raw_parts(data_ptr, data_len) - }; - - Ok(bson::decode_document_utf8_lossy(&mut slice)?) + Ok(bson::Document::from_reader(&mut slice)?) } pub fn as_json(&self) -> String { @@ -123,7 +102,7 @@ impl Drop for Bsonc { mod tests { #[test] fn test_bsonc_from_and_as_document() { - let document = doc! { "key" => "value" }; + let document = doc! { "key": "value" }; let bsonc = super::Bsonc::from_document(&document).unwrap(); let decoded = bsonc.as_document().unwrap(); @@ -134,16 +113,16 @@ mod tests { fn test_bsonc_from_and_as_document_invalid_utf8() { let bytes = b"\x80\xae".to_vec(); let value = unsafe { String::from_utf8_unchecked(bytes) }; - let document = doc! { "key" => value }; + let document = doc! { "key": value }; let bsonc = super::Bsonc::from_document(&document).unwrap(); - let decoded = bsonc.as_document_utf8_lossy().unwrap(); + let decoded = bsonc.as_document().unwrap(); assert_eq!(decoded.get_str("key").unwrap(), "��"); } #[test] fn test_bsonc_as_json() { - let document = doc! { "key" => "value" }; + let document = doc! { "key": "value" }; let bsonc = super::Bsonc::from_document(&document).unwrap(); assert_eq!("{ \"key\" : \"value\" }".to_owned(), bsonc.as_json()); } diff --git a/src/client.rs b/src/client.rs index 9412cf4..f847448 100644 --- a/src/client.rs +++ b/src/client.rs @@ -305,7 +305,7 @@ impl<'a> Client<'a> { }; if success == 1 { - match reply.as_document_utf8_lossy() { + match reply.as_document() { Ok(document) => return Ok(document), Err(error) => return Err(error.into()) } @@ -347,7 +347,7 @@ impl<'a> Client<'a> { }; if success == 1 { - match reply.as_document_utf8_lossy() { + match reply.as_document() { Ok(document) => return Ok(document), Err(error) => return Err(error.into()) } diff --git a/src/collection.rs b/src/collection.rs index 9c4ad9c..1690391 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -342,7 +342,7 @@ impl<'a> Collection<'a> { }; if success == 1 { - match reply.as_document_utf8_lossy() { + match reply.as_document() { Ok(document) => return Ok(document), Err(error) => return Err(error.into()) } @@ -547,7 +547,7 @@ impl<'a> Collection<'a> { }; if success == 1 { - match reply.as_document_utf8_lossy() { + match reply.as_document() { Ok(document) => return Ok(document), Err(error) => return Err(error.into()) } @@ -884,7 +884,7 @@ impl<'a>BulkOperation<'a> { ) }; - let document = match reply.as_document_utf8_lossy() { + let document = match reply.as_document() { Ok(document) => document, Err(error) => return Err(BulkOperationError{error: error.into(), reply: doc!{}}) }; diff --git a/src/cursor.rs b/src/cursor.rs index 0238f9f..5526df1 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -205,7 +205,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) => { - self.query.insert_bson("_id".to_string(), Bson::Document(doc!{ "$gt" => id })); + self.query.insert("_id".to_string(), Bson::Document(doc!{ "$gt": id })); }, None => () }; @@ -339,7 +339,7 @@ fn batch_to_array(doc: Document) -> Result<(Option,Option)> bson::from_bson(Bson::Document(doc.clone())) .map_err(|err| { error!("cannot read batch from db: {}", err); - ValueAccessError(bson::ValueAccessError::NotPresent) + ValueAccessError(bson::document::ValueAccessError::NotPresent) }); doc_result.map(|v| { diff --git a/src/database.rs b/src/database.rs index c4334f9..2206eca 100644 --- a/src/database.rs +++ b/src/database.rs @@ -146,7 +146,7 @@ impl<'a> Database<'a> { }; if success == 1 { - match reply.as_document_utf8_lossy() { + match reply.as_document() { Ok(document) => return Ok(document), Err(error) => return Err(error.into()) } diff --git a/src/error.rs b/src/error.rs index 60a8019..9d03bda 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,9 +3,11 @@ use std::fmt; use std::borrow::Cow; use std::ffi::CStr; -use bson::{DecoderError,EncoderError,ValueAccessError,Document}; use std::ffi::NulError; +use bson::{de,ser,Document}; +use bson::document::ValueAccessError; + use crate::mongoc::bindings; /// Wrapper for all errors that can occur in the driver. @@ -13,9 +15,9 @@ pub enum MongoError { /// Error in the underlying C driver. Bsonc(BsoncError), /// Error decoding Bson. - Decoder(DecoderError), + Decoder(de::Error), /// Error encoding Bson. - Encoder(EncoderError), + Encoder(ser::Error), /// Error accessing a value on a Bson document. ValueAccessError(ValueAccessError), /// Invalid params error that can be reported by the underlying C driver. @@ -63,14 +65,14 @@ impl error::Error for MongoError { } } -impl From for MongoError { - fn from(error: DecoderError) -> MongoError { +impl From for MongoError { + fn from(error: de::Error) -> MongoError { MongoError::Decoder(error) } } -impl From for MongoError { - fn from(error: EncoderError) -> MongoError { +impl From for MongoError { + fn from(error: ser::Error) -> MongoError { MongoError::Encoder(error) } } diff --git a/tests/bson_encode_decode.rs b/tests/bson_encode_decode.rs index 9c5ecf0..b15cbfd 100644 --- a/tests/bson_encode_decode.rs +++ b/tests/bson_encode_decode.rs @@ -6,7 +6,7 @@ use chrono::prelude::*; use mongo_driver::client::{ClientPool,Uri}; -use bson::{bson,doc}; +use bson::{doc,Binary}; use bson::oid::ObjectId; use bson::spec::BinarySubtype; @@ -24,16 +24,19 @@ fn test_bson_encode_decode() { let datetime = Utc.ymd(2014, 7, 8).and_hms(9, 10, 11); let document = doc! { - "_id" => (ObjectId::new().unwrap()), - "floating_point" => 10.0, - "string" => "a value", - "array" => [10, 20, 30], - "doc" => {"key" => 1}, - "bool" => true, - "i32" => 1i32, - "i64" => 1i64, - "datetime" => datetime, - "binary_generic" => (BinarySubtype::Generic, vec![0, 1, 2, 3, 4]) + "_id": ObjectId::new(), + "floating_point": 10.0, + "string": "a value", + "array": [10, 20, 30], + "doc": {"key": 1}, + "bool": true, + "i32": 1i32, + "i64": 1i64, + "datetime": datetime, + "binary": (Binary { + subtype: BinarySubtype::Generic, + bytes: vec![0, 1, 2, 3, 4] + }) }; assert!(collection.insert(&document, None).is_ok()); diff --git a/tests/bulk_operation.rs b/tests/bulk_operation.rs index f9316b6..5b6f9dd 100644 --- a/tests/bulk_operation.rs +++ b/tests/bulk_operation.rs @@ -3,7 +3,7 @@ extern crate mongo_driver; use std::env; -use bson::{bson,doc}; +use bson::doc; use mongo_driver::client::{ClientPool,Uri}; #[test] @@ -33,7 +33,7 @@ fn test_basics() { let bulk_operation = collection.create_bulk_operation(None); - let document = doc! {"key_1" => "Value 1"}; + let document = doc! {"key_1": "Value 1"}; bulk_operation.insert(&document).expect("Could not insert"); assert!(bulk_operation.execute().is_ok()); @@ -54,7 +54,7 @@ fn test_utf8() { let bulk_operation = collection.create_bulk_operation(None); - let document = doc! {"key_1" => "kācaṃ śaknomyattum; nopahinasti mām."}; + let document = doc! {"key_1": "kācaṃ śaknomyattum; nopahinasti mām."}; bulk_operation.insert(&document).expect("Could not insert"); assert!(bulk_operation.execute().is_ok()); @@ -82,8 +82,8 @@ fn test_insert_remove_replace_update_extended() { let bulk_operation = collection.create_bulk_operation(None); let document = doc! { - "key_1" => "Value 1", - "key_2" => "Value 2" + "key_1": "Value 1", + "key_2": "Value 2" }; for _ in 0..5 { bulk_operation.insert(&document).unwrap(); @@ -94,7 +94,7 @@ fn test_insert_remove_replace_update_extended() { assert_eq!( result.ok().unwrap().get("nInserted").unwrap(), - &bson::Bson::I32(5) + &bson::Bson::Int32(5) ); assert_eq!(5, collection.count(&doc!{}, None).unwrap()); } @@ -102,7 +102,7 @@ fn test_insert_remove_replace_update_extended() { let query = doc!{}; let update_document = doc! { - "$set" => {"key_1" => "Value update"} + "$set": {"key_1": "Value update"} }; // Update one @@ -120,7 +120,7 @@ fn test_insert_remove_replace_update_extended() { assert_eq!( result.ok().unwrap().get("nModified").unwrap(), - &bson::Bson::I32(1) + &bson::Bson::Int32(1) ); let first_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); @@ -147,7 +147,7 @@ fn test_insert_remove_replace_update_extended() { assert_eq!( result.ok().unwrap().get("nModified").unwrap(), - &bson::Bson::I32(4) + &bson::Bson::Int32(4) ); collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); @@ -162,7 +162,7 @@ fn test_insert_remove_replace_update_extended() { // Replace one { - let replace_document = doc! { "key_1" => "Value replace" }; + let replace_document = doc! { "key_1": "Value replace" }; let bulk_operation = collection.create_bulk_operation(None); bulk_operation.replace_one( @@ -176,7 +176,7 @@ fn test_insert_remove_replace_update_extended() { assert_eq!( result.ok().unwrap().get("nModified").unwrap(), - &bson::Bson::I32(1) + &bson::Bson::Int32(1) ); let first_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); @@ -198,7 +198,7 @@ fn test_insert_remove_replace_update_extended() { assert_eq!( result.ok().unwrap().get("nRemoved").unwrap(), - &bson::Bson::I32(1) + &bson::Bson::Int32(1) ); assert_eq!(4, collection.count(&query, None).unwrap()); } @@ -213,7 +213,7 @@ fn test_insert_remove_replace_update_extended() { assert_eq!( result.ok().unwrap().get("nRemoved").unwrap(), - &bson::Bson::I32(4) + &bson::Bson::Int32(4) ); assert_eq!(0, collection.count(&query, None).unwrap()); } diff --git a/tests/client.rs b/tests/client.rs index 0eac0c7..4bd4681 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -6,7 +6,7 @@ use std::path::PathBuf; use std::sync::Arc; use std::thread; -use bson::{bson,doc}; +use bson::doc; use mongo_driver::client::{ClientPool,SslOptions,Uri}; #[test] @@ -95,8 +95,8 @@ fn test_read_command_with_opts() { let collection = client.get_collection(db_name, coll_name); let document = doc! { - "key_1" => "Value 1", - "key_2" => "kācaṃ śaknomyattum; nopahinasti mām. \u{0}" + "key_1": "Value 1", + "key_2": "kācaṃ śaknomyattum; nopahinasti mām. \u{0}" }; collection.insert(&document, None).expect("Could not insert document"); @@ -161,6 +161,6 @@ fn test_ssl_connection_success() { let client = pool.pop(); let database = client.get_database("admin"); - let result = database.command_simple(doc!{"ping" => 1}, None).unwrap(); + let result = database.command_simple(doc!{"ping": 1}, None).unwrap(); assert!(result.contains_key("ok")); } diff --git a/tests/collection.rs b/tests/collection.rs index aa9f28b..f3c6167 100644 --- a/tests/collection.rs +++ b/tests/collection.rs @@ -1,7 +1,7 @@ extern crate bson; extern crate mongo_driver; -use bson::{bson,doc}; +use bson::doc; use mongo_driver::CommandAndFindOptions; use mongo_driver::collection::{CountOptions,FindAndModifyOperation}; @@ -17,15 +17,15 @@ fn test_aggregate() { collection.drop().unwrap_or(()); for _ in 0..5 { - assert!(collection.insert(&doc!{"key" => 1}, None).is_ok()); + assert!(collection.insert(&doc!{"key": 1}, None).is_ok()); } let pipeline = doc!{ - "pipeline" => [ + "pipeline": [ { - "$group" => { - "_id" => "$key", - "total" => {"$sum" => "$key"} + "$group": { + "_id": "$key", + "total": {"$sum": "$key"} } } ] @@ -44,7 +44,7 @@ fn test_command() { let client = pool.pop(); let collection = client.get_collection("rust_driver_test", "items"); - let command = doc! { "ping" => 1 }; + let command = doc! { "ping": 1 }; let result = collection.command(command, None).unwrap().next().unwrap().unwrap(); assert!(result.contains_key("ok")); @@ -57,7 +57,7 @@ fn test_command_simple() { let client = pool.pop(); let collection = client.get_collection("rust_driver_test", "items"); - let command = doc! { "ping" => 1 }; + let command = doc! { "ping": 1 }; let result = collection.command_simple(command, None).unwrap(); assert!(result.contains_key("ok")); @@ -75,8 +75,8 @@ fn test_mutation_and_finding() { assert_eq!("items", collection.get_name().to_mut()); let document = doc! { - "key_1" => "Value 1", - "key_2" => "kācaṃ śaknomyattum; nopahinasti mām. \u{0}" + "key_1": "Value 1", + "key_2": "kācaṃ śaknomyattum; nopahinasti mām. \u{0}" }; collection.insert(&document, None).expect("Could not insert document"); { @@ -92,7 +92,7 @@ fn test_mutation_and_finding() { } let second_document = doc! { - "key_1" => "Value 3" + "key_1": "Value 3" }; assert!(collection.insert(&second_document, None).is_ok()); @@ -118,12 +118,12 @@ fn test_mutation_and_finding() { ); // Update the second document - let update = doc!{"$set" => {"key_1" => "Value 4"}}; + let update = doc!{"$set": {"key_1": "Value 4"}}; assert!(collection.update(&second_document, &update, None).is_ok()); // Reload and check value let query_after_update = doc! { - "key_1" => "Value 4" + "key_1": "Value 4" }; let mut found_document = collection.find(&query_after_update, None).unwrap().next().unwrap().unwrap(); assert_eq!( @@ -163,7 +163,7 @@ fn test_mutation_and_finding() { skip: 0, limit: 0, batch_size: 0, - fields: Some(doc! { "key_1" => true }), + fields: Some(doc! { "key_1": true }), read_prefs: None }; @@ -194,10 +194,10 @@ fn test_find_and_modify() { // Upsert something, it should now exist let query = doc! { - "key_1" => "Value 1" + "key_1": "Value 1" }; let update = doc! { - "$set" => {"content" => 1i32} + "$set": {"content": 1i32} }; let result = collection.find_and_modify( &query, @@ -211,7 +211,7 @@ fn test_find_and_modify() { // Update this record let update2 = doc! { - "$set" => {"content" => 2i32} + "$set": {"content": 2i32} }; let result = collection.find_and_modify( &query, diff --git a/tests/cursor.rs b/tests/cursor.rs index a633df4..81f8afe 100644 --- a/tests/cursor.rs +++ b/tests/cursor.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use std::thread; use std::time::Duration; -use bson::{bson,doc}; +use bson::doc; use mongo_driver::client::{ClientPool,Uri}; use mongo_driver::Result; @@ -17,7 +17,7 @@ fn test_cursor() { let client = pool.pop(); let mut collection = client.get_collection("rust_driver_test", "cursor_items"); - let document = doc! { "key" => "value" }; + let document = doc! { "key": "value" }; collection.drop().unwrap_or(()); for _ in 0..10 { @@ -45,8 +45,8 @@ fn test_tailing_cursor() { database.get_collection("not_capped").drop().unwrap_or(()); let options = doc! { - "capped" => true, - "size" => 100000 + "capped": true, + "size": 100000 }; let capped_collection = database.create_collection("capped", Some(&options)).unwrap(); let normal_collection = database.create_collection("not_capped", None).unwrap(); @@ -56,7 +56,7 @@ fn test_tailing_cursor() { let failing_result = failing_cursor.into_iter().next().expect("Nothing in iterator"); assert!(failing_result.is_err()); - let document = doc! { "key_1" => "Value 1" }; + let document = doc! { "key_1": "Value 1" }; // Insert a first document into the collection capped_collection.insert(&document, None).unwrap(); @@ -120,7 +120,7 @@ fn test_batch_cursor() { assert_eq!( result.ok().unwrap().get("nInserted").unwrap(), // why is this an i32? - &bson::Bson::I32(NUM_TO_TEST) + &bson::Bson::Int32(NUM_TO_TEST) ); assert_eq!(NUM_TO_TEST as i64, collection.count(&doc!{}, None).unwrap()); } diff --git a/tests/database.rs b/tests/database.rs index fb70c9e..6b1487c 100644 --- a/tests/database.rs +++ b/tests/database.rs @@ -1,7 +1,7 @@ extern crate bson; extern crate mongo_driver; -use bson::{bson,doc}; +use bson::doc; use mongo_driver::client::{ClientPool,Uri}; @@ -13,7 +13,7 @@ fn test_command() { let client = pool.pop(); let database = client.get_database("rust_test"); - let command = doc! { "ping" => 1 }; + let command = doc! { "ping": 1 }; let result = database.command(command, None).unwrap().next().unwrap().unwrap(); assert!(result.contains_key("ok")); @@ -26,7 +26,7 @@ fn test_command_simple() { let client = pool.pop(); let database = client.get_database("rust_test"); - let command = doc! { "ping" => 1 }; + let command = doc! { "ping": 1 }; let result = database.command_simple(command, None).unwrap(); assert!(result.contains_key("ok"));