diff options
author | Teddy Wing | 2019-11-02 00:35:49 +0100 |
---|---|---|
committer | Teddy Wing | 2019-11-02 00:58:29 +0100 |
commit | 92f8f57b76b32c3d3e52d4b61dcdf25969f47ab7 (patch) | |
tree | 52c98e805c59aada905a8d8b3ca25a51fa5f2dae /src/lib.rs | |
parent | adff229c543b765b1bfd7cb6c871d6b89617eff0 (diff) | |
download | pdf-urls-92f8f57b76b32c3d3e52d4b61dcdf25969f47ab7.tar.bz2 |
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.
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 32 |
1 files changed, 20 insertions, 12 deletions
@@ -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<P: AsRef<Path>>(path: P) { - let doc = Document::load(path).unwrap(); +use errors::Result; + +pub fn get_urls_from_pdf<P: AsRef<Path>>(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(()) } |