diff options
| -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),      }  } | 
