diff options
author | Teddy Wing | 2019-11-02 01:53:52 +0100 |
---|---|---|
committer | Teddy Wing | 2019-11-02 01:53:52 +0100 |
commit | 950c7d1a93ae44da2584345e4a2624d64ef84816 (patch) | |
tree | 4b1fdcdd98a7fc49a8bd93bfb7b27397c306de9f | |
parent | 65305566946555ec78596e57e48c551a3dbf9dc8 (diff) | |
download | pdf-urls-950c7d1a93ae44da2584345e4a2624d64ef84816.tar.bz2 |
get_urls_from_pdf: Return a `Vec<String>` instead of printing
Facilitate testing by returning a vec of URLs instead of printing them
directly to STDOUT.
-rw-r--r-- | src/errors.rs | 52 | ||||
-rw-r--r-- | src/lib.rs | 10 | ||||
-rw-r--r-- | src/main.rs | 6 |
3 files changed, 60 insertions, 8 deletions
diff --git a/src/errors.rs b/src/errors.rs index 6ec2ee1..641c81b 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,6 +1,7 @@ use std::fmt; use std::error; use std::str; +use std::string; use lopdf; @@ -9,14 +10,14 @@ pub type Result<T> = ::std::result::Result<T, Error>; #[derive(Debug)] pub enum Error { Lopdf(lopdf::Error), - Utf8(str::Utf8Error), + String(StringError), } 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), + Error::String(ref err) => write!(f, "{}", err), } } } @@ -25,7 +26,7 @@ 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), + Error::String(ref err) => Some(err), } } } @@ -38,6 +39,49 @@ impl From<lopdf::Error> for Error { impl From<str::Utf8Error> for Error { fn from(err: str::Utf8Error) -> Error { - Error::Utf8(err) + Error::String(StringError::from(err)) + } +} + +impl From<string::FromUtf8Error> for Error { + fn from(err: string::FromUtf8Error) -> Error { + Error::String(StringError::from(err)) + } +} + + +#[derive(Debug)] +pub enum StringError { + StrUtf8(str::Utf8Error), + StringUtf8(string::FromUtf8Error), +} + +impl fmt::Display for StringError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + StringError::StrUtf8(ref err) => write!(f, "utf8 error: {}", err), + StringError::StringUtf8(ref err) => write!(f, "utf8 error: {}", err), + } + } +} + +impl error::Error for StringError { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + match *self { + StringError::StrUtf8(ref err) => Some(err), + StringError::StringUtf8(ref err) => Some(err), + } + } +} + +impl From<str::Utf8Error> for StringError { + fn from(err: str::Utf8Error) -> StringError { + StringError::StrUtf8(err) + } +} + +impl From<string::FromUtf8Error> for StringError { + fn from(err: string::FromUtf8Error) -> StringError { + StringError::StringUtf8(err) } } @@ -4,14 +4,18 @@ extern crate lopdf; use std::path::Path; use std::str; +use std::string::String; +use std::vec::Vec; use lopdf::{Document, Object}; use errors::Result; -pub fn get_urls_from_pdf<P: AsRef<Path>>(path: P) -> Result<()> { +pub fn get_urls_from_pdf<P: AsRef<Path>>(path: P) -> Result<Vec<String>> { let doc = Document::load(path)?; + let mut urls = Vec::new(); + for (_, obj) in doc.objects { match obj { Object::Dictionary(d) => { @@ -27,7 +31,7 @@ pub fn get_urls_from_pdf<P: AsRef<Path>>(path: P) -> Result<()> { if key == "URI" { match v { Object::String(s, _) => { - println!("{}", str::from_utf8(s)?); + urls.push(String::from_utf8(s.to_vec())?); }, _ => (), } @@ -40,5 +44,5 @@ pub fn get_urls_from_pdf<P: AsRef<Path>>(path: P) -> Result<()> { } } - Ok(()) + Ok(urls) } diff --git a/src/main.rs b/src/main.rs index 9bd5c37..049fcd2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,11 @@ use pdf_urls::get_urls_from_pdf; fn main() { match get_urls_from_pdf("example.pdf") { - Ok(_) => (), + Ok(urls) => { + for url in urls { + println!("{}", url); + } + }, Err(err) => eprintln!("error: {}", err), } } |