Move Uri to client module

pull/13/head
Thijs Cadier 9 years ago
parent 646da592a7
commit 58a45bd49d

@ -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<T: Into<Vec<u8>>>(uri_string: T) -> Option<Uri> {
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<Cow<'a, str>> {
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);
}
}
}

@ -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;

@ -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<T: Into<Vec<u8>>>(uri_string: T) -> Option<Uri> {
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<Cow<'a, str>> {
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);
}
}
}

@ -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;

@ -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() {

@ -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() {

@ -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]

@ -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]

@ -1,5 +1,4 @@
use mongo_driver::uri::Uri;
use mongo_driver::client::ClientPool;
use mongo_driver::client::{ClientPool,Uri};
#[test]
fn test_command() {

@ -1,4 +1,4 @@
use mongo_driver::uri::Uri;
use mongo_driver::client::Uri;
#[test]
fn test_new_uri() {

Loading…
Cancel
Save