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 /src/lib.rs | |
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.
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 10 |
1 files changed, 7 insertions, 3 deletions
@@ -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) } |