Uri new now returns an option

pull/9/head
Thijs Cadier 9 years ago
parent c77cc42024
commit 6d5c6ec18d

@ -12,11 +12,19 @@ pub struct Uri {
} }
impl Uri { impl Uri {
pub fn new<T: Into<Vec<u8>>>(uri_string: T) -> 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_cstring = CString::new(uri_string).unwrap();
let uri = unsafe { bindings::mongoc_uri_new(uri_cstring.as_ptr()) }; let uri = unsafe { bindings::mongoc_uri_new(uri_cstring.as_ptr()) };
Uri { if uri.is_null() {
inner: uri None
} else {
Some(Uri { inner: uri })
} }
} }
@ -46,7 +54,7 @@ impl fmt::Debug for Uri {
impl Clone for Uri { impl Clone for Uri {
fn clone(&self) -> Uri { fn clone(&self) -> Uri {
Uri::new(self.as_str().into_owned()) Uri::new(self.as_str().into_owned()).unwrap()
} }
} }

@ -6,7 +6,7 @@ use mongo_driver::client::{ClientPool,SslOptions};
#[test] #[test]
fn test_new_pool_and_pop_client() { fn test_new_pool_and_pop_client() {
let uri = Uri::new("mongodb://localhost:27017/"); let uri = Uri::new("mongodb://localhost:27017/").unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
// Pop a client and get a database and collection // Pop a client and get a database and collection
@ -22,7 +22,7 @@ fn test_new_pool_and_pop_client() {
#[test] #[test]
fn test_new_pool_and_pop_client_in_threads() { fn test_new_pool_and_pop_client_in_threads() {
let uri = Uri::new("mongodb://localhost:27017/"); let uri = Uri::new("mongodb://localhost:27017/").unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let pool1 = pool.clone(); let pool1 = pool.clone();
@ -43,7 +43,7 @@ fn test_new_pool_and_pop_client_in_threads() {
#[test] #[test]
fn test_get_server_status() { fn test_get_server_status() {
let uri = Uri::new("mongodb://localhost:27017/"); let uri = Uri::new("mongodb://localhost:27017/").unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
@ -56,7 +56,7 @@ fn test_get_server_status() {
#[test] #[test]
fn test_new_pool_with_ssl_options() { fn test_new_pool_with_ssl_options() {
// We currently have no way to test full operations // We currently have no way to test full operations
let uri = Uri::new("mongodb://localhost:27017/"); let uri = Uri::new("mongodb://localhost:27017/").unwrap();
let ssl_options = SslOptions::new( let ssl_options = SslOptions::new(
Some(PathBuf::from("./README.md")), Some(PathBuf::from("./README.md")),
Some("password".to_string()), Some("password".to_string()),

@ -7,7 +7,7 @@ use mongo_driver::flags;
#[test] #[test]
fn test_command() { fn test_command() {
let uri = Uri::new("mongodb://localhost:27017/"); let uri = Uri::new("mongodb://localhost:27017/").unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let collection = client.get_collection("rust_driver_test", "items"); let collection = client.get_collection("rust_driver_test", "items");
@ -20,7 +20,7 @@ fn test_command() {
#[test] #[test]
fn test_mutation_and_finding() { fn test_mutation_and_finding() {
let uri = Uri::new("mongodb://localhost:27017/"); let uri = Uri::new("mongodb://localhost:27017/").unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let mut collection = client.get_collection("rust_driver_test", "items"); let mut collection = client.get_collection("rust_driver_test", "items");
@ -110,7 +110,7 @@ fn test_mutation_and_finding() {
#[test] #[test]
fn test_insert_failure() { fn test_insert_failure() {
let uri = Uri::new("mongodb://localhost:27018/"); // There should be no mongo server here let uri = Uri::new("mongodb://localhost:27018/").unwrap(); // There should be no mongo server here
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let collection = client.get_collection("rust_driver_test", "items"); let collection = client.get_collection("rust_driver_test", "items");

@ -7,4 +7,9 @@ mod tests {
let uri = Uri::new("mongodb://localhost:27017/"); let uri = Uri::new("mongodb://localhost:27017/");
assert_eq!("mongodb://localhost:27017/", uri.as_str()); assert_eq!("mongodb://localhost:27017/", uri.as_str());
} }
#[test]
fn test_new_invalid_uri() {
assert!(Uri::new("@:/mongo::").is_none());
}
} }

Loading…
Cancel
Save