Skip to content

Commit

Permalink
Extract password CString code to function
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsided committed Nov 30, 2024
1 parent 08d9a6e commit 93f3978
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,7 @@ impl PopplerDocument {
p: P,
password: Option<&str>,
) -> Result<PopplerDocument, glib::error::Error> {
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 {
Expand All @@ -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(
Expand Down Expand Up @@ -123,6 +103,21 @@ impl PopplerDocument {
doc: self,
}
}

/// Converts the provided password into a `CString`.
fn get_password(password: Option<&str>) -> Result<CString, glib::error::Error> {
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 {
Expand Down

0 comments on commit 93f3978

Please sign in to comment.