Skip to content

Commit

Permalink
Extract password CString code to function (#19)
Browse files Browse the repository at this point in the history
* Extract password CString code to function

* fix: compilation error

---------

Co-authored-by: Denys Vitali <[email protected]>
  • Loading branch information
sunsided and denysvitali authored Dec 3, 2024
1 parent 79d7fe9 commit 66b1dcc
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,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 @@ -55,17 +45,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 @@ -143,6 +123,22 @@ impl PopplerDocument {
}
}


/// 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)",
)
})?)
}

/// Returns the number of pages.
///
/// This function is a convenience wrapper around [`get_n_pages`](Self::get_n_pages).
Expand Down

0 comments on commit 66b1dcc

Please sign in to comment.