From 92f8f57b76b32c3d3e52d4b61dcdf25969f47ab7 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 2 Nov 2019 00:35:49 +0100 Subject: get_urls_from_pdf: Remove `unwrap`s and replace with an error type Create a custom error type to use instead of the `unwrap`s. --- src/lib.rs | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index cb96c0e..60e2338 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +mod errors; + extern crate lopdf; use std::path::Path; @@ -5,37 +7,43 @@ use std::str; use lopdf::{Document, Object}; -pub fn get_urls_from_pdf>(path: P) { - let doc = Document::load(path).unwrap(); +use errors::Result; + +pub fn get_urls_from_pdf>(path: P) -> Result<()> { + let doc = Document::load(path)?; for (_, obj) in doc.objects { - match obj { + return match obj { Object::Dictionary(d) => { for (k, v) in d.iter() { - let key = str::from_utf8(&k).unwrap(); + let key = str::from_utf8(&k)?; if key == "A" { - for (k, v) in v.as_dict().unwrap() { - let key = str::from_utf8(&k).unwrap(); + let url_objects = v.as_dict()?; + + for (k, v) in url_objects { + let key = str::from_utf8(&k)?; if key == "URI" { - match v { + return match v { Object::String(s, _) => { - println!("{}", str::from_utf8(s).unwrap()); + println!("{}", str::from_utf8(s)?); - () + Ok(()) }, - _ => (), + _ => Ok(()), } } } } } - () + Ok(()) }, - _ => (), + _ => Ok(()), } } + + Ok(()) } -- cgit v1.2.3