Skip to content

Commit

Permalink
Replace unwrap with returning error
Browse files Browse the repository at this point in the history
  • Loading branch information
Heinenen committed Aug 14, 2024
1 parent c25cef3 commit 4aeec7c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ impl Document {
}

/// Get resources used by a page.
pub fn get_page_resources(&self, page_id: ObjectId) -> (Option<&Dictionary>, Vec<ObjectId>) {
pub fn get_page_resources(&self, page_id: ObjectId) -> Result<(Option<&Dictionary>, Vec<ObjectId>)> {
fn collect_resources(
page_node: &Dictionary, resource_ids: &mut Vec<ObjectId>, doc: &Document,
already_seen: &mut HashSet<ObjectId>,
Expand All @@ -443,13 +443,13 @@ impl Document {
let mut resource_ids = Vec::new();
if let Ok(page) = self.get_dictionary(page_id) {
resource_dict = page.get(b"Resources").and_then(Object::as_dict).ok();
collect_resources(page, &mut resource_ids, self, &mut HashSet::new()).unwrap();
collect_resources(page, &mut resource_ids, self, &mut HashSet::new())?;
}
(resource_dict, resource_ids)
Ok((resource_dict, resource_ids))
}

/// Get fonts used by a page.
pub fn get_page_fonts(&self, page_id: ObjectId) -> BTreeMap<Vec<u8>, &Dictionary> {
pub fn get_page_fonts(&self, page_id: ObjectId) -> Result<BTreeMap<Vec<u8>, &Dictionary>> {
fn collect_fonts_from_resources<'a>(
resources: &'a Dictionary, fonts: &mut BTreeMap<Vec<u8>, &'a Dictionary>, doc: &'a Document,
) {
Expand All @@ -475,7 +475,7 @@ impl Document {
}

let mut fonts = BTreeMap::new();
let (resource_dict, resource_ids) = self.get_page_resources(page_id);
let (resource_dict, resource_ids) = self.get_page_resources(page_id)?;
if let Some(resources) = resource_dict {
collect_fonts_from_resources(resources, &mut fonts, self);
}
Expand All @@ -484,7 +484,7 @@ impl Document {
collect_fonts_from_resources(resources, &mut fonts, self);
}
}
fonts
Ok(fonts)
}

/// Get the PDF annotations of a page. The /Subtype of each annotation dictionary defines the
Expand Down
4 changes: 2 additions & 2 deletions src/parser_aux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Document {
let pages = self.get_pages();
for page_number in page_numbers {
let page_id = *pages.get(page_number).ok_or(Error::PageNumberNotFound(*page_number))?;
let fonts = self.get_page_fonts(page_id);
let fonts = self.get_page_fonts(page_id)?;
let encodings = fonts
.into_iter()
.map(|(name, font)| (name, font.get_font_encoding()))
Expand Down Expand Up @@ -108,7 +108,7 @@ impl Document {
.nth(page)
.ok_or(Error::PageNumberNotFound(page_number))?;
let encodings = self
.get_page_fonts(page_id)
.get_page_fonts(page_id)?
.into_iter()
.map(|(name, font)| (name, font.get_font_encoding().to_owned()))
.collect::<BTreeMap<Vec<u8>, String>>();
Expand Down

0 comments on commit 4aeec7c

Please sign in to comment.