Merge pull request #57 from thijsc/modern_rust

Modern rust
pull/60/head
Thijs Cadier 4 years ago committed by GitHub
commit e5f89793f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,6 +9,7 @@ repository = "https://github.com/thijsc/mongo-rust-driver"
keywords = ["mongodb", "database"]
categories = ["database", "api-bindings"]
license = "MIT/Apache-2.0"
edition = "2018"
[badges]
travis-ci = { repository = "thijsc/mongo-rust-driver" }

@ -102,7 +102,7 @@ pub mod bindings {
pub fn mongoc_database_get_collection(database: *mut mongoc_database_t, name: *const ::libc::c_char) -> *mut mongoc_collection_t;
pub fn mongoc_database_get_name(database: *mut mongoc_database_t) -> *const ::libc::c_char;
pub fn mongoc_database_destroy(database: *mut mongoc_database_t) -> ();
pub fn mongoc_database_has_collection(database: *mut mongoc_database_t, name: *const ::libc::c_char, error: *mut bson_error_t) -> ::libc::int32_t;
pub fn mongoc_database_has_collection(database: *mut mongoc_database_t, name: *const ::libc::c_char, error: *mut bson_error_t) -> i32;
}
// Client

@ -4,7 +4,7 @@ use std::fmt;
use std::slice;
use libc::c_void;
use mongoc::bindings;
use crate::mongoc::bindings;
use bson;
use super::Result;
@ -25,7 +25,7 @@ impl Bsonc {
pub fn from_document(document: &bson::Document) -> Result<Bsonc> {
let mut buffer = Vec::new();
try!(bson::encode_document(&mut buffer, document));
bson::encode_document(&mut buffer, document)?;
let inner = unsafe {
bindings::bson_new_from_data(
@ -60,8 +60,7 @@ impl Bsonc {
slice::from_raw_parts(data_ptr, data_len)
};
let document = try!(bson::decode_document(&mut slice));
Ok(document)
Ok(bson::decode_document(&mut slice)?)
}
/// Decode a bson from the C side to a document with lossy UTF-8 decoding
@ -82,8 +81,7 @@ impl Bsonc {
slice::from_raw_parts(data_ptr, data_len)
};
let document = try!(bson::decode_document_utf8_lossy(&mut slice));
Ok(document)
Ok(bson::decode_document_utf8_lossy(&mut slice)?)
}
pub fn as_json(&self) -> String {

@ -11,7 +11,7 @@ use std::ptr;
use std::io;
use std::fs::File;
use mongoc::bindings;
use crate::mongoc::bindings;
use bson::Document;
@ -142,11 +142,11 @@ impl SslOptions {
crl_file: Option<PathBuf>,
weak_cert_validation: bool
) -> io::Result<SslOptions> {
let pem_file_cstring = try!(Self::cstring_from_path(&pem_file));
let pem_file_cstring = Self::cstring_from_path(&pem_file)?;
let pem_password_cstring = Self::cstring_from_string(&pem_password);
let ca_file_cstring = try!(Self::cstring_from_path(&ca_file));
let ca_dir_cstring = try!(Self::cstring_from_path(&ca_dir));
let crl_file_cstring = try!(Self::cstring_from_path(&crl_file));
let ca_file_cstring = Self::cstring_from_path(&ca_file)?;
let ca_dir_cstring = Self::cstring_from_path(&ca_dir)?;
let crl_file_cstring = Self::cstring_from_path(&crl_file)?;
let ssl_options = bindings::mongoc_ssl_opt_t {
pem_file: match pem_file_cstring {
@ -192,7 +192,7 @@ impl SslOptions {
fn cstring_from_path(path: &Option<PathBuf>) -> io::Result<Option<CString>> {
match path {
&Some(ref p) => {
try!(File::open(p.as_path()));
File::open(p.as_path())?;
Ok(Some(CString::new(p.to_string_lossy().into_owned()).unwrap()))
},
&None => Ok(None)

@ -7,8 +7,8 @@ use std::ffi::CStr;
use std::borrow::Cow;
use std::time::Duration;
use mongoc::bindings;
use bsonc;
use crate::mongoc::bindings;
use crate::bsonc;
use bson::Document;
@ -247,10 +247,10 @@ impl<'a> Collection<'a> {
bindings::mongoc_collection_aggregate(
self.inner,
options.query_flags.flags(),
try!(Bsonc::from_document(pipeline)).inner(),
Bsonc::from_document(pipeline)?.inner(),
match options.options {
Some(ref o) => {
try!(Bsonc::from_document(o)).inner()
Bsonc::from_document(o)?.inner()
},
None => ptr::null()
},
@ -292,7 +292,7 @@ impl<'a> Collection<'a> {
options.skip,
options.limit,
options.batch_size,
try!(Bsonc::from_document(&command)).inner(),
Bsonc::from_document(&command)?.inner(),
match fields_bsonc {
Some(ref f) => f.inner(),
None => ptr::null()
@ -331,7 +331,7 @@ impl<'a> Collection<'a> {
let success = unsafe {
bindings::mongoc_collection_command_simple(
self.inner,
try!(Bsonc::from_document(&command)).inner(),
Bsonc::from_document(&command)?.inner(),
match read_prefs {
Some(ref prefs) => prefs.inner(),
None => ptr::null()
@ -365,7 +365,7 @@ impl<'a> Collection<'a> {
let default_options = CountOptions::default();
let options = options.unwrap_or(&default_options);
let opts_bsonc = match options.opts {
Some(ref o) => Some(try!(Bsonc::from_document(o))),
Some(ref o) => Some(Bsonc::from_document(o)?),
None => None
};
@ -374,7 +374,7 @@ impl<'a> Collection<'a> {
bindings::mongoc_collection_count_with_opts(
self.inner,
options.query_flags.flags(),
try!(Bsonc::from_document(query)).inner(),
Bsonc::from_document(query)?.inner(),
options.skip as i64,
options.limit as i64,
match opts_bsonc {
@ -458,7 +458,7 @@ impl<'a> Collection<'a> {
options.skip,
options.limit,
options.batch_size,
try!(Bsonc::from_document(query)).inner(),
Bsonc::from_document(query)?.inner(),
match fields_bsonc {
Some(ref f) => f.inner(),
None => ptr::null()
@ -505,13 +505,13 @@ impl<'a> Collection<'a> {
// them around long enough.
let sort_bsonc = match options.sort {
Some(ref doc) => {
Some(try!(Bsonc::from_document(doc)))
Some(Bsonc::from_document(doc)?)
},
None => None
};
let update_bsonc = match operation {
FindAndModifyOperation::Update(ref doc) | FindAndModifyOperation::Upsert(ref doc) => {
Some(try!(Bsonc::from_document(doc)))
Some(Bsonc::from_document(doc)?)
},
FindAndModifyOperation::Remove => None
};
@ -519,7 +519,7 @@ impl<'a> Collection<'a> {
let success = unsafe {
bindings::mongoc_collection_find_and_modify(
self.inner,
try!(Bsonc::from_document(&query)).inner(),
Bsonc::from_document(&query)?.inner(),
match sort_bsonc {
Some(ref s) => s.inner(),
None => ptr::null()
@ -582,7 +582,7 @@ impl<'a> Collection<'a> {
bindings::mongoc_collection_insert(
self.inner,
options.insert_flags.flags(),
try!(Bsonc::from_document(&document)).inner(),
Bsonc::from_document(&document)?.inner(),
options.write_concern.inner(),
error.mut_inner()
)
@ -613,7 +613,7 @@ impl<'a> Collection<'a> {
bindings::mongoc_collection_remove(
self.inner,
options.remove_flags.flags(),
try!(Bsonc::from_document(&selector)).inner(),
Bsonc::from_document(&selector)?.inner(),
options.write_concern.inner(),
error.mut_inner()
)
@ -642,7 +642,7 @@ impl<'a> Collection<'a> {
let success = unsafe {
bindings::mongoc_collection_save(
self.inner,
try!(Bsonc::from_document(&document)).inner(),
Bsonc::from_document(&document)?.inner(),
write_concern.inner(),
error.mut_inner()
)
@ -673,8 +673,8 @@ impl<'a> Collection<'a> {
bindings::mongoc_collection_update(
self.inner,
options.update_flags.flags(),
try!(Bsonc::from_document(&selector)).inner(),
try!(Bsonc::from_document(&update)).inner(),
Bsonc::from_document(&selector)?.inner(),
Bsonc::from_document(&update)?.inner(),
options.write_concern.inner(),
error.mut_inner()
)
@ -757,7 +757,7 @@ impl<'a>BulkOperation<'a> {
unsafe {
bindings::mongoc_bulk_operation_insert(
self.inner,
try!(Bsonc::from_document(&document)).inner()
Bsonc::from_document(&document)?.inner()
)
}
Ok(())
@ -773,7 +773,7 @@ impl<'a>BulkOperation<'a> {
unsafe {
bindings::mongoc_bulk_operation_remove(
self.inner,
try!(Bsonc::from_document(&selector)).inner()
Bsonc::from_document(&selector)?.inner()
)
}
Ok(())
@ -789,7 +789,7 @@ impl<'a>BulkOperation<'a> {
unsafe {
bindings::mongoc_bulk_operation_remove_one(
self.inner,
try!(Bsonc::from_document(&selector)).inner()
Bsonc::from_document(&selector)?.inner()
)
}
Ok(())
@ -807,8 +807,8 @@ impl<'a>BulkOperation<'a> {
unsafe {
bindings::mongoc_bulk_operation_replace_one(
self.inner,
try!(Bsonc::from_document(&selector)).inner(),
try!(Bsonc::from_document(&document)).inner(),
Bsonc::from_document(&selector)?.inner(),
Bsonc::from_document(&document)?.inner(),
upsert as u8
)
}
@ -830,8 +830,8 @@ impl<'a>BulkOperation<'a> {
unsafe {
bindings::mongoc_bulk_operation_update_one(
self.inner,
try!(Bsonc::from_document(&selector)).inner(),
try!(Bsonc::from_document(&document)).inner(),
Bsonc::from_document(&selector)?.inner(),
Bsonc::from_document(&document)?.inner(),
upsert as u8
)
}
@ -853,8 +853,8 @@ impl<'a>BulkOperation<'a> {
unsafe {
bindings::mongoc_bulk_operation_update(
self.inner,
try!(Bsonc::from_document(&selector)).inner(),
try!(Bsonc::from_document(&document)).inner(),
Bsonc::from_document(&selector)?.inner(),
Bsonc::from_document(&document)?.inner(),
upsert as u8
)
}

@ -6,7 +6,7 @@ use std::thread;
use std::time::Duration;
use std::collections::VecDeque;
use mongoc::bindings;
use crate::mongoc::bindings;
use bson::{self,Bson,Document,oid};
use super::BsoncError;

@ -4,7 +4,7 @@ use std::ffi::{CString,CStr};
use std::borrow::Cow;
use std::ptr;
use mongoc::bindings;
use crate::mongoc::bindings;
use bson::Document;
use super::Result;
@ -18,7 +18,7 @@ use super::cursor;
use super::cursor::Cursor;
use super::cursor::BatchCursor;
use super::read_prefs::ReadPrefs;
use flags::FlagsValue;
use crate::flags::FlagsValue;
#[doc(hidden)]
pub enum CreatedBy<'a> {
@ -79,7 +79,7 @@ impl<'a> Database<'a> {
options.skip,
options.limit,
options.batch_size,
try!(Bsonc::from_document(&command)).inner(),
Bsonc::from_document(&command)?.inner(),
match fields_bsonc {
Some(ref f) => f.inner(),
None => ptr::null()
@ -135,7 +135,7 @@ impl<'a> Database<'a> {
let success = unsafe {
bindings::mongoc_database_command_simple(
self.inner,
try!(Bsonc::from_document(&command)).inner(),
Bsonc::from_document(&command)?.inner(),
match read_prefs {
Some(ref prefs) => prefs.inner(),
None => ptr::null()
@ -166,7 +166,7 @@ impl<'a> Database<'a> {
let mut error = BsoncError::empty();
let name_cstring = CString::new(name).unwrap();
let options_bsonc = match options {
Some(o) => Some(try!(Bsonc::from_document(o))),
Some(o) => Some(Bsonc::from_document(o)?),
None => None
};

@ -6,7 +6,7 @@ use std::ffi::CStr;
use bson::{DecoderError,EncoderError,ValueAccessError,Document};
use std::ffi::NulError;
use mongoc::bindings;
use crate::mongoc::bindings;
/// Wrapper for all errors that can occur in the driver.
pub enum MongoError {
@ -51,18 +51,7 @@ impl fmt::Debug for MongoError {
}
impl error::Error for MongoError {
fn description(&self) -> &str {
match *self {
MongoError::Bsonc(ref err) => err.description(),
MongoError::Decoder(ref err) => err.description(),
MongoError::Encoder(ref err) => err.description(),
MongoError::ValueAccessError(ref err) => err.description(),
MongoError::InvalidParams(ref err) => err.description(),
MongoError::Nul(ref err) => err.description()
}
}
fn cause(&self) -> Option<&error::Error> {
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
MongoError::Bsonc(ref err) => Some(err),
MongoError::Decoder(ref err) => Some(err),

@ -1,6 +1,6 @@
//! Flags to configure various MongoDB operations.
use mongoc::bindings;
use crate::mongoc::bindings;
use std::collections::BTreeSet;

@ -38,9 +38,9 @@ extern crate serde;
use std::ffi::CStr;
use std::ptr;
use std::result;
use std::sync::{Once,ONCE_INIT};
use std::sync::Once;
use mongoc::bindings;
use crate::mongoc::bindings;
pub mod client;
pub mod collection;
@ -53,7 +53,7 @@ pub mod write_concern;
mod bsonc;
mod error;
pub use error::{MongoError,BsoncError,MongoErrorDomain,MongoErrorCode,InvalidParamsError,BulkOperationError};
pub use crate::error::{MongoError,BsoncError,MongoErrorDomain,MongoErrorCode,InvalidParamsError,BulkOperationError};
/// Result that's used in all functions that perform operations on the database.
pub type Result<T> = result::Result<T, MongoError>;
@ -61,7 +61,7 @@ pub type Result<T> = result::Result<T, MongoError>;
/// Result that's used in bulk operations.
pub type BulkOperationResult<T> = result::Result<T, BulkOperationError>;
static MONGOC_INIT: Once = ONCE_INIT;
static MONGOC_INIT: Once = Once::new();
/// Init mongo driver, needs to be called once before doing
/// anything else.

@ -1,6 +1,6 @@
//! Abstraction on top of the MongoDB connection read prefences.
use mongoc::bindings;
use crate::mongoc::bindings;
/// Describes how reads should be dispatched.
pub enum ReadMode {

@ -1,6 +1,6 @@
//! Abstraction on top of the MongoDB connection write concern.
use mongoc::bindings;
use crate::mongoc::bindings;
/// Possible write concern levels, only default is supported at the moment.
pub enum WriteConcernLevel {

@ -1,7 +1,12 @@
extern crate bson;
extern crate chrono;
extern crate mongo_driver;
use chrono::prelude::*;
use mongo_driver::client::{ClientPool,Uri};
use bson::{bson,doc};
use bson::oid::ObjectId;
use bson::spec::BinarySubtype;

@ -1,7 +1,9 @@
use std::env;
extern crate bson;
extern crate mongo_driver;
use bson;
use std::env;
use bson::{bson,doc};
use mongo_driver::client::{ClientPool,Uri};
#[test]

@ -1,8 +1,12 @@
extern crate bson;
extern crate mongo_driver;
use std::env;
use std::path::PathBuf;
use std::sync::Arc;
use std::thread;
use bson::{bson,doc};
use mongo_driver::client::{ClientPool,SslOptions,Uri};
#[test]

@ -1,4 +1,7 @@
use bson;
extern crate bson;
extern crate mongo_driver;
use bson::{bson,doc};
use mongo_driver::CommandAndFindOptions;
use mongo_driver::collection::{CountOptions,FindAndModifyOperation};

@ -1,8 +1,11 @@
extern crate bson;
extern crate mongo_driver;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use bson;
use bson::{bson,doc};
use mongo_driver::client::{ClientPool,Uri};
use mongo_driver::Result;

@ -1,3 +1,8 @@
extern crate bson;
extern crate mongo_driver;
use bson::{bson,doc};
use mongo_driver::client::{ClientPool,Uri};
#[cfg_attr(target_os = "windows", ignore)]

@ -1,3 +1,5 @@
extern crate mongo_driver;
use mongo_driver::flags::*;
#[test]

@ -1,3 +1,5 @@
extern crate mongo_driver;
use mongo_driver::read_prefs::ReadPrefs;
#[test]

@ -1,15 +0,0 @@
extern crate mongo_driver;
extern crate chrono;
#[macro_use]
extern crate bson;
mod bson_encode_decode;
mod bulk_operation;
mod client;
mod collection;
mod cursor;
mod database;
mod flags;
mod read_prefs;
mod uri;
mod write_concern;

@ -1,3 +1,4 @@
extern crate mongo_driver;
use mongo_driver::client::Uri;
#[test]

@ -1,3 +1,5 @@
extern crate mongo_driver;
use mongo_driver::write_concern::WriteConcern;
#[test]

Loading…
Cancel
Save