diff --git a/src/lib.rs b/src/lib.rs index fda5319..6f5a65f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,17 +17,7 @@ impl PopplerDocument { p: P, password: Option<&str>, ) -> Result { - let pw = CString::new(if password.is_none() { - "" - } else { - password.expect("password.is_none() is false, but apparently it's lying.") - }) - .map_err(|_| { - glib::error::Error::new( - glib::FileError::Inval, - "Password invalid (possibly contains NUL characters)", - ) - })?; + let pw = Self::get_password(password)?; let path_cstring = util::path_to_glib_url(p)?; let doc = util::call_with_gerror(|err_ptr| unsafe { @@ -46,17 +36,7 @@ impl PopplerDocument { "data is empty", )); } - let pw = CString::new(if password.is_none() { - "" - } else { - password.expect("password.is_none() is false, but apparently it's lying.") - }) - .map_err(|_| { - glib::error::Error::new( - glib::FileError::Inval, - "Password invalid (possibly contains NUL characters)", - ) - })?; + let pw = Self::get_password(password)?; let doc = util::call_with_gerror(|err_ptr| unsafe { ffi::poppler_document_new_from_data( @@ -123,6 +103,21 @@ impl PopplerDocument { doc: self, } } + + /// Converts the provided password into a `CString`. + fn get_password(password: Option<&str>) -> Result { + Ok(CString::new(if password.is_none() { + "" + } else { + password.expect("password.is_none() is false, but apparently it's lying.") + }) + .map_err(|_| { + glib::error::Error::new( + glib::FileError::Inval, + "Password invalid (possibly contains NUL characters)", + ) + })?) + } } impl Drop for PopplerDocument {