aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/errors.rs43
-rw-r--r--src/lib.rs32
-rw-r--r--src/main.rs2
3 files changed, 64 insertions, 13 deletions
diff --git a/src/errors.rs b/src/errors.rs
new file mode 100644
index 0000000..6ec2ee1
--- /dev/null
+++ b/src/errors.rs
@@ -0,0 +1,43 @@
+use std::fmt;
+use std::error;
+use std::str;
+
+use lopdf;
+
+pub type Result<T> = ::std::result::Result<T, Error>;
+
+#[derive(Debug)]
+pub enum Error {
+ Lopdf(lopdf::Error),
+ Utf8(str::Utf8Error),
+}
+
+impl fmt::Display for Error {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match *self {
+ Error::Lopdf(ref err) => write!(f, "pdf error: {}", err),
+ Error::Utf8(ref err) => write!(f, "utf8 error: {}", err),
+ }
+ }
+}
+
+impl error::Error for Error {
+ fn source(&self) -> Option<&(dyn error::Error + 'static)> {
+ match *self {
+ Error::Lopdf(ref err) => Some(err),
+ Error::Utf8(ref err) => Some(err),
+ }
+ }
+}
+
+impl From<lopdf::Error> for Error {
+ fn from(err: lopdf::Error) -> Error {
+ Error::Lopdf(err)
+ }
+}
+
+impl From<str::Utf8Error> for Error {
+ fn from(err: str::Utf8Error) -> Error {
+ Error::Utf8(err)
+ }
+}
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<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(())
}
diff --git a/src/main.rs b/src/main.rs
index 14eab24..d8bb0d5 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,5 @@
use pdf_urls::get_urls_from_pdf;
fn main() {
- get_urls_from_pdf("example.pdf");
+ get_urls_from_pdf("example.pdf").unwrap();
}