diff --git a/src/uri.rs b/src/uri.rs index ebb8775..d378025 100644 --- a/src/uri.rs +++ b/src/uri.rs @@ -43,6 +43,19 @@ impl Uri { } } + 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 } diff --git a/tests/uri.rs b/tests/uri.rs index 63de950..800fb4a 100644 --- a/tests/uri.rs +++ b/tests/uri.rs @@ -10,3 +10,15 @@ fn test_new_uri() { fn test_new_invalid_uri() { assert!(Uri::new("@:/mongo::").is_none()); } + +#[test] +fn test_get_database_empty() { + let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + assert!(uri.get_database().is_none()); +} + +#[test] +fn test_get_database() { + let uri = Uri::new("mongodb://localhost:27017/db").unwrap(); + assert_eq!("db", uri.get_database().unwrap()); +}