diff --git a/src/client.rs b/src/client.rs index 6bf9aaa..14bdd64 100644 --- a/src/client.rs +++ b/src/client.rs @@ -2,8 +2,9 @@ //! //! Get started by creating a `ClientPool` you can use to pop a `Client`. +use std::borrow::Cow; use std::fmt; -use std::ffi::CString; +use std::ffi::{CStr,CString}; use std::path::PathBuf; use std::mem; use std::ptr; @@ -21,7 +22,6 @@ use super::collection; use super::collection::Collection; use super::database; use super::database::Database; -use super::uri::Uri; use super::read_prefs::ReadPrefs; /// Pool that allows usage of clients out of a single pool from multiple threads. @@ -323,3 +323,81 @@ impl<'a> Drop for Client<'a> { } } } + +/// Abstraction on top of MongoDB connection URI format. +pub struct Uri { + inner: *mut bindings::mongoc_uri_t +} + +impl Uri { + /// Parses a string containing a MongoDB style URI connection string. + /// + /// Returns None if the uri is not in the correct format, there is no + /// further information available if this is not the case. + pub fn new>>(uri_string: T) -> Option { + let uri_cstring = CString::new(uri_string).unwrap(); + let uri = unsafe { bindings::mongoc_uri_new(uri_cstring.as_ptr()) }; + if uri.is_null() { + None + } else { + Some(Uri { inner: uri }) + } + } + + unsafe fn inner(&self) -> *const bindings::mongoc_uri_t { + assert!(!self.inner.is_null()); + self.inner + } + + pub fn as_str<'a>(&'a self) -> Cow<'a, str> { + assert!(!self.inner.is_null()); + unsafe { + let cstr = CStr::from_ptr( + bindings::mongoc_uri_get_string(self.inner) + ); + String::from_utf8_lossy(cstr.to_bytes()) + } + } + + pub fn get_database<'a>(&'a self) -> Option> { + assert!(!self.inner.is_null()); + unsafe { + let ptr = bindings::mongoc_uri_get_database(self.inner); + if ptr.is_null() { + None + } else { + let cstr = CStr::from_ptr(ptr); + Some(String::from_utf8_lossy(cstr.to_bytes())) + } + } + } + + // TODO add various methods that are available on uri +} + +impl PartialEq for Uri { + fn eq(&self, other: &Uri) -> bool { + self.as_str() == other.as_str() + } +} + +impl fmt::Debug for Uri { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.as_str()) + } +} + +impl Clone for Uri { + fn clone(&self) -> Uri { + Uri::new(self.as_str().into_owned()).unwrap() + } +} + +impl Drop for Uri { + fn drop(&mut self) { + assert!(!self.inner.is_null()); + unsafe { + bindings::mongoc_uri_destroy(self.inner); + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 1bbdb08..2166724 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,8 +11,7 @@ //! //! ``` //! use std::sync::Arc; -//! use mongo_driver::uri::Uri; -//! use mongo_driver::client::ClientPool; +//! use mongo_driver::client::{ClientPool,Uri}; //! //! let uri = Uri::new("mongodb://localhost:27017/").unwrap(); //! let pool = Arc::new(ClientPool::new(uri.clone(), None)); @@ -45,7 +44,6 @@ pub mod cursor; pub mod database; pub mod flags; pub mod read_prefs; -pub mod uri; pub mod write_concern; mod bsonc; diff --git a/src/uri.rs b/src/uri.rs deleted file mode 100644 index 0525d2f..0000000 --- a/src/uri.rs +++ /dev/null @@ -1,87 +0,0 @@ -use std::borrow::Cow; -use std::ffi::{CStr,CString}; -use std::fmt; - -use mongoc::bindings; - -/// Abstraction on top of MongoDB connection URI format. -/// See: http://api.mongodb.org/c/current/mongoc_uri_t.html - -pub struct Uri { - inner: *mut bindings::mongoc_uri_t -} - -impl Uri { - /// Parses a string containing a MongoDB style URI connection string. - /// - /// Returns None if the uri is not in the correct format, there is no - /// further information available if this is not the case. - /// - /// See: http://api.mongodb.org/c/current/mongoc_uri_new.html - pub fn new>>(uri_string: T) -> Option { - let uri_cstring = CString::new(uri_string).unwrap(); - let uri = unsafe { bindings::mongoc_uri_new(uri_cstring.as_ptr()) }; - if uri.is_null() { - None - } else { - Some(Uri { inner: uri }) - } - } - - pub unsafe fn inner(&self) -> *const bindings::mongoc_uri_t { - assert!(!self.inner.is_null()); - self.inner - } - - pub fn as_str<'a>(&'a self) -> Cow<'a, str> { - assert!(!self.inner.is_null()); - unsafe { - let cstr = CStr::from_ptr( - bindings::mongoc_uri_get_string(self.inner) - ); - String::from_utf8_lossy(cstr.to_bytes()) - } - } - - pub fn get_database<'a>(&'a self) -> Option> { - assert!(!self.inner.is_null()); - unsafe { - let ptr = bindings::mongoc_uri_get_database(self.inner); - if ptr.is_null() { - None - } else { - let cstr = CStr::from_ptr(ptr); - Some(String::from_utf8_lossy(cstr.to_bytes())) - } - } - } - - // TODO add various methods that are available on uri -} - -impl PartialEq for Uri { - fn eq(&self, other: &Uri) -> bool { - self.as_str() == other.as_str() - } -} - -impl fmt::Debug for Uri { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.as_str()) - } -} - -impl Clone for Uri { - fn clone(&self) -> Uri { - Uri::new(self.as_str().into_owned()).unwrap() - } -} - -impl Drop for Uri { - fn drop(&mut self) { - assert!(!self.inner.is_null()); - unsafe { - bindings::mongoc_uri_destroy(self.inner); - } - } -} diff --git a/tests/bson_encode_decode.rs b/tests/bson_encode_decode.rs index 2268769..9227e6e 100644 --- a/tests/bson_encode_decode.rs +++ b/tests/bson_encode_decode.rs @@ -1,7 +1,6 @@ use chrono::*; -use mongo_driver::uri::Uri; -use mongo_driver::client::ClientPool; +use mongo_driver::client::{ClientPool,Uri}; use bson::oid::ObjectId; use bson::spec::BinarySubtype; diff --git a/tests/bulk_operation.rs b/tests/bulk_operation.rs index ed7696c..cc8447c 100644 --- a/tests/bulk_operation.rs +++ b/tests/bulk_operation.rs @@ -2,8 +2,7 @@ use std::env; use bson; -use mongo_driver::uri::Uri; -use mongo_driver::client::ClientPool; +use mongo_driver::client::{ClientPool,Uri}; #[test] fn test_execute_error() { diff --git a/tests/client.rs b/tests/client.rs index 3710b70..8cd90bc 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -3,8 +3,7 @@ use std::path::PathBuf; use std::sync::Arc; use std::thread; -use mongo_driver::uri::Uri; -use mongo_driver::client::{ClientPool,SslOptions}; +use mongo_driver::client::{ClientPool,SslOptions,Uri}; #[test] fn test_new_pool_pop_client_and_borrow_collection() { diff --git a/tests/collection.rs b/tests/collection.rs index 35e6fa8..2e52a0b 100644 --- a/tests/collection.rs +++ b/tests/collection.rs @@ -2,8 +2,7 @@ use bson; use mongo_driver::CommandAndFindOptions; use mongo_driver::collection::{CountOptions,FindAndModifyOperation}; -use mongo_driver::uri::Uri; -use mongo_driver::client::ClientPool; +use mongo_driver::client::{ClientPool,Uri}; use mongo_driver::flags; #[test] diff --git a/tests/cursor.rs b/tests/cursor.rs index 7f4b0e3..8e73723 100644 --- a/tests/cursor.rs +++ b/tests/cursor.rs @@ -4,8 +4,7 @@ use std::time::Duration; use bson; -use mongo_driver::uri::Uri; -use mongo_driver::client::ClientPool; +use mongo_driver::client::{ClientPool,Uri}; use mongo_driver::Result; #[test] diff --git a/tests/database.rs b/tests/database.rs index b255bfa..8769a64 100644 --- a/tests/database.rs +++ b/tests/database.rs @@ -1,5 +1,4 @@ -use mongo_driver::uri::Uri; -use mongo_driver::client::ClientPool; +use mongo_driver::client::{ClientPool,Uri}; #[test] fn test_command() { diff --git a/tests/uri.rs b/tests/uri.rs index a191ea9..ab65a9b 100644 --- a/tests/uri.rs +++ b/tests/uri.rs @@ -1,4 +1,4 @@ -use mongo_driver::uri::Uri; +use mongo_driver::client::Uri; #[test] fn test_new_uri() {