From 6d5c6ec18d9341975aab25a0477637447a0e1b18 Mon Sep 17 00:00:00 2001 From: Thijs Cadier Date: Thu, 27 Aug 2015 22:32:46 +0200 Subject: [PATCH] Uri new now returns an option --- src/uri.rs | 16 ++++++++++++---- tests/client.rs | 8 ++++---- tests/collection.rs | 6 +++--- tests/uri.rs | 5 +++++ 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/uri.rs b/src/uri.rs index a992dea..ebb8775 100644 --- a/src/uri.rs +++ b/src/uri.rs @@ -12,11 +12,19 @@ pub struct Uri { } impl Uri { - pub fn new>>(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>>(uri_string: T) -> Option { let uri_cstring = CString::new(uri_string).unwrap(); let uri = unsafe { bindings::mongoc_uri_new(uri_cstring.as_ptr()) }; - Uri { - inner: uri + if uri.is_null() { + None + } else { + Some(Uri { inner: uri }) } } @@ -46,7 +54,7 @@ impl fmt::Debug for Uri { impl Clone for Uri { fn clone(&self) -> Uri { - Uri::new(self.as_str().into_owned()) + Uri::new(self.as_str().into_owned()).unwrap() } } diff --git a/tests/client.rs b/tests/client.rs index 3c60755..2f1c860 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -6,7 +6,7 @@ use mongo_driver::client::{ClientPool,SslOptions}; #[test] 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); // Pop a client and get a database and collection @@ -22,7 +22,7 @@ fn test_new_pool_and_pop_client() { #[test] 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 pool1 = pool.clone(); @@ -43,7 +43,7 @@ fn test_new_pool_and_pop_client_in_threads() { #[test] 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 client = pool.pop(); @@ -56,7 +56,7 @@ fn test_get_server_status() { #[test] fn test_new_pool_with_ssl_options() { // 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( Some(PathBuf::from("./README.md")), Some("password".to_string()), diff --git a/tests/collection.rs b/tests/collection.rs index 8493c55..0414f35 100644 --- a/tests/collection.rs +++ b/tests/collection.rs @@ -7,7 +7,7 @@ use mongo_driver::flags; #[test] 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 client = pool.pop(); let collection = client.get_collection("rust_driver_test", "items"); @@ -20,7 +20,7 @@ fn test_command() { #[test] 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 client = pool.pop(); let mut collection = client.get_collection("rust_driver_test", "items"); @@ -110,7 +110,7 @@ fn test_mutation_and_finding() { #[test] 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 client = pool.pop(); let collection = client.get_collection("rust_driver_test", "items"); diff --git a/tests/uri.rs b/tests/uri.rs index 1b4fbf9..1189f2b 100644 --- a/tests/uri.rs +++ b/tests/uri.rs @@ -7,4 +7,9 @@ mod tests { let uri = Uri::new("mongodb://localhost:27017/"); assert_eq!("mongodb://localhost:27017/", uri.as_str()); } + + #[test] + fn test_new_invalid_uri() { + assert!(Uri::new("@:/mongo::").is_none()); + } }