@ -43,6 +43,19 @@ impl Uri {
}
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
@ -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());
fn test_get_database() {
let uri = Uri::new("mongodb://localhost:27017/db").unwrap();
assert_eq!("db", uri.get_database().unwrap());