Rewrite ExternalLinker::get_external_links() to return iterator
This commit is contained in:
parent
3aa05c3ecc
commit
db17b4f858
1 changed files with 13 additions and 25 deletions
|
@ -47,6 +47,8 @@ use result::Result;
|
||||||
use internal::InternalLinker;
|
use internal::InternalLinker;
|
||||||
use module_path::ModuleEntryPath;
|
use module_path::ModuleEntryPath;
|
||||||
|
|
||||||
|
use self::iter::*;
|
||||||
|
|
||||||
use toml::Value;
|
use toml::Value;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
use crypto::sha1::Sha1;
|
use crypto::sha1::Sha1;
|
||||||
|
@ -98,7 +100,7 @@ impl<'a> Link<'a> {
|
||||||
pub trait ExternalLinker : InternalLinker {
|
pub trait ExternalLinker : InternalLinker {
|
||||||
|
|
||||||
/// Get the external links from the implementor object
|
/// Get the external links from the implementor object
|
||||||
fn get_external_links(&self, store: &Store) -> Result<Vec<Url>>;
|
fn get_external_links<'a>(&self, store: &'a Store) -> Result<UrlIter<'a>>;
|
||||||
|
|
||||||
/// Set the external links for the implementor object
|
/// Set the external links for the implementor object
|
||||||
fn set_external_links(&mut self, store: &Store, links: Vec<Url>) -> Result<()>;
|
fn set_external_links(&mut self, store: &Store, links: Vec<Url>) -> Result<()>;
|
||||||
|
@ -296,33 +298,16 @@ fn get_external_link_from_file(entry: &FileLockEntry) -> Result<Url> {
|
||||||
impl ExternalLinker for Entry {
|
impl ExternalLinker for Entry {
|
||||||
|
|
||||||
/// Get the external links from the implementor object
|
/// Get the external links from the implementor object
|
||||||
fn get_external_links(&self, store: &Store) -> Result<Vec<Url>> {
|
fn get_external_links<'a>(&self, store: &'a Store) -> Result<UrlIter<'a>> {
|
||||||
// Iterate through all internal links and filter for FileLockEntries which live in
|
// Iterate through all internal links and filter for FileLockEntries which live in
|
||||||
// /link/external/<SHA> -> load these files and get the external link from their headers,
|
// /link/external/<SHA> -> load these files and get the external link from their headers,
|
||||||
// put them into the return vector.
|
// put them into the return vector.
|
||||||
self.get_internal_links()
|
self.get_internal_links()
|
||||||
|
.map_err(|e| LE::new(LEK::StoreReadError, Some(Box::new(e))))
|
||||||
.map(|iter| {
|
.map(|iter| {
|
||||||
debug!("Getting external links");
|
debug!("Getting external links");
|
||||||
iter.filter(|l| is_external_link_storeid(l))
|
iter.only_external_links().urls(store)
|
||||||
.map(|id| {
|
|
||||||
debug!("Retrieving entry for id: '{:?}'", id);
|
|
||||||
match store.retrieve(id.clone()) {
|
|
||||||
Ok(f) => {
|
|
||||||
debug!("Store::retrieve({:?}) succeeded", id);
|
|
||||||
debug!("getting external link from file now");
|
|
||||||
get_external_link_from_file(&f)
|
|
||||||
.map_err(|e| { debug!("URL -> Err = {:?}", e); e })
|
|
||||||
},
|
|
||||||
Err(e) => {
|
|
||||||
debug!("Retrieving entry for id: '{:?}' failed", id);
|
|
||||||
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
|
/// Set the external links for the implementor object
|
||||||
|
@ -401,7 +386,9 @@ impl ExternalLinker for Entry {
|
||||||
// get external links, add this one, save them
|
// get external links, add this one, save them
|
||||||
debug!("Getting links");
|
debug!("Getting links");
|
||||||
self.get_external_links(store)
|
self.get_external_links(store)
|
||||||
.and_then(|mut links| {
|
.and_then(|links| {
|
||||||
|
// TODO: Do not ignore errors here
|
||||||
|
let mut links = links.filter_map(Result::ok).collect::<Vec<_>>();
|
||||||
debug!("Adding link = '{:?}' to links = {:?}", link, links);
|
debug!("Adding link = '{:?}' to links = {:?}", link, links);
|
||||||
links.push(link);
|
links.push(link);
|
||||||
debug!("Setting {} links = {:?}", links.len(), links);
|
debug!("Setting {} links = {:?}", links.len(), links);
|
||||||
|
@ -414,10 +401,11 @@ impl ExternalLinker for Entry {
|
||||||
// get external links, remove this one, save them
|
// get external links, remove this one, save them
|
||||||
self.get_external_links(store)
|
self.get_external_links(store)
|
||||||
.and_then(|links| {
|
.and_then(|links| {
|
||||||
debug!("Removing link = '{:?}' from links = {:?}", link, links);
|
debug!("Removing link = '{:?}'", link);
|
||||||
let links = links.into_iter()
|
let links = links
|
||||||
|
.filter_map(Result::ok)
|
||||||
.filter(|l| l.as_str() != link.as_str())
|
.filter(|l| l.as_str() != link.as_str())
|
||||||
.collect();
|
.collect::<Vec<_>>();
|
||||||
self.set_external_links(store, links)
|
self.set_external_links(store, links)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue