From 950c7d1a93ae44da2584345e4a2624d64ef84816 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 2 Nov 2019 01:53:52 +0100 Subject: get_urls_from_pdf: Return a `Vec` instead of printing Facilitate testing by returning a vec of URLs instead of printing them directly to STDOUT. --- src/lib.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index c09fe3e..2a59906 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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>(path: P) -> Result<()> { +pub fn get_urls_from_pdf>(path: P) -> Result> { 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>(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>(path: P) -> Result<()> { } } - Ok(()) + Ok(urls) } -- cgit v1.2.3