From 91caa0c6653f3cfd07c5c40e6c203f25c32a30fe Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 9 Apr 2016 15:15:13 +0200 Subject: [PATCH] Implement get_external_links() --- libimaglink/src/external.rs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/libimaglink/src/external.rs b/libimaglink/src/external.rs index dd2888b0..760aff2f 100644 --- a/libimaglink/src/external.rs +++ b/libimaglink/src/external.rs @@ -91,7 +91,7 @@ impl<'a> Link<'a> { pub trait ExternalLinker : InternalLinker { /// Get the external links from the implementor object - fn get_external_links(&self) -> Result>; + fn get_external_links(&self, store: &Store) -> Result>; /// Set the external links for the implementor object fn set_external_links(&mut self, links: Vec) -> Result>; @@ -104,17 +104,40 @@ pub trait ExternalLinker : InternalLinker { } +/// Check whether the StoreId starts with `/link/external/` +fn is_link_store_id(id: &StoreId) -> bool { + id.starts_with("/link/external/") +} + +fn get_external_link_from_file(entry: &FileLockEntry) -> Result { + Link::get_link_uri_from_filelockentry(entry) // TODO: Do not hide error by using this function + .ok_or(LE::new(LEK::StoreReadError, None)) +} + /// Implement ExternalLinker for Entry, hiding the fact that there is no such thing as an external /// link in an entry, but internal links to other entries which serve as external links, as one /// entry in the store can only have one external link. impl ExternalLinker for Entry { /// Get the external links from the implementor object - fn get_external_links(&self) -> Result> { + fn get_external_links(&self, store: &Store) -> Result> { // Iterate through all internal links and filter for FileLockEntries which live in // /link/external/ -> load these files and get the external link from their headers, // put them into the return vector. - unimplemented!() + self.get_internal_links() + .map(|vect| { + vect.into_iter() + .filter(is_link_store_id) + .map(|id| { + match store.retrieve(id) { + Ok(f) => get_external_link_from_file(&f), + Err(e) => Err(LE::new(LEK::StoreReadError, Some(Box::new(e)))), + } + }) + .filter_map(|x| x.ok()) // TODO: Do not ignore error here + .collect() + }) + .map_err(|e| LE::new(LEK::StoreReadError, Some(Box::new(e)))) } /// Set the external links for the implementor object