Merge pull request #803 from matthiasbeyer/libimagentrylink/iter-interface

libimagentrylink: iter interface
This commit is contained in:
Matthias Beyer 2016-10-14 17:46:41 +02:00 committed by GitHub
commit 924692d7cb
4 changed files with 83 additions and 50 deletions

View File

@ -97,7 +97,6 @@ fn handle_internal_linking(rt: &Runtime) {
e.get_internal_links() e.get_internal_links()
.map(|links| { .map(|links| {
let i = links let i = links
.iter()
.filter_map(|l| { .filter_map(|l| {
l.to_str() l.to_str()
.map_warn_err(|e| format!("Failed to convert StoreId to string: {:?}", e)) .map_warn_err(|e| format!("Failed to convert StoreId to string: {:?}", e))

View File

@ -114,7 +114,7 @@ impl<'a> BookmarkCollection<'a> {
self.fle self.fle
.get_internal_links() .get_internal_links()
.map(|v| v.into_iter().filter(|id| is_external_link_storeid(id)).collect()) .map(|v| v.filter(|id| is_external_link_storeid(id)).collect())
.map_err_into(BEK::StoreReadError) .map_err_into(BEK::StoreReadError)
} }

View File

@ -133,10 +133,9 @@ impl ExternalLinker for Entry {
// /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(|vect| { .map(|iter| {
debug!("Getting external links"); debug!("Getting external links");
vect.into_iter() iter.filter(|l| is_external_link_storeid(l))
.filter(is_external_link_storeid)
.map(|id| { .map(|id| {
debug!("Retrieving entry for id: '{:?}'", id); debug!("Retrieving entry for id: '{:?}'", id);
match store.retrieve(id.clone()) { match store.retrieve(id.clone()) {

View File

@ -17,8 +17,6 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use std::cmp::Ordering;
use libimagstore::storeid::StoreId; use libimagstore::storeid::StoreId;
use libimagstore::store::Entry; use libimagstore::store::Entry;
use libimagstore::store::EntryHeader; use libimagstore::store::EntryHeader;
@ -28,19 +26,20 @@ use libimagerror::into::IntoError;
use error::LinkErrorKind as LEK; use error::LinkErrorKind as LEK;
use error::MapErrInto; use error::MapErrInto;
use result::Result; use result::Result;
use self::iter::LinkIter;
use self::iter::IntoValues;
use toml::Value; use toml::Value;
use itertools::Itertools;
pub type Link = StoreId; pub type Link = StoreId;
pub trait InternalLinker { pub trait InternalLinker {
/// Get the internal links from the implementor object /// Get the internal links from the implementor object
fn get_internal_links(&self) -> Result<Vec<Link>>; fn get_internal_links(&self) -> Result<LinkIter>;
/// Set the internal links for the implementor object /// Set the internal links for the implementor object
fn set_internal_links(&mut self, links: Vec<&mut Entry>) -> Result<Vec<Link>>; fn set_internal_links(&mut self, links: Vec<&mut Entry>) -> Result<LinkIter>;
/// Add an internal link to the implementor object /// Add an internal link to the implementor object
fn add_internal_link(&mut self, link: &mut Entry) -> Result<()>; fn add_internal_link(&mut self, link: &mut Entry) -> Result<()>;
@ -50,14 +49,73 @@ pub trait InternalLinker {
} }
pub mod iter {
use std::vec::IntoIter;
use std::cmp::Ordering;
use super::Link;
use error::LinkErrorKind as LEK;
use error::MapErrInto;
use result::Result;
use toml::Value;
use itertools::Itertools;
pub struct LinkIter(IntoIter<Link>);
impl LinkIter {
pub fn new(v: Vec<Link>) -> LinkIter {
LinkIter(v.into_iter())
}
}
impl Iterator for LinkIter {
type Item = Link;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
pub trait IntoValues {
fn into_values(self) -> IntoIter<Result<Value>>;
}
impl<I: Iterator<Item = Link>> IntoValues for I {
fn into_values(self) -> IntoIter<Result<Value>> {
self.map(|s| s.without_base().to_str().map_err_into(LEK::InternalConversionError))
.unique_by(|entry| {
match entry {
&Ok(ref e) => Some(e.clone()),
&Err(_) => None,
}
})
.map(|elem| elem.map(Value::String))
.sorted_by(|a, b| {
match (a, b) {
(&Ok(Value::String(ref a)), &Ok(Value::String(ref b))) => Ord::cmp(a, b),
(&Err(_), _) | (_, &Err(_)) => Ordering::Equal,
_ => unreachable!()
}
})
.into_iter()
}
}
}
impl InternalLinker for Entry { impl InternalLinker for Entry {
fn get_internal_links(&self) -> Result<Vec<Link>> { fn get_internal_links(&self) -> Result<LinkIter> {
process_rw_result(self.get_header().read("imag.links")) process_rw_result(self.get_header().read("imag.links"))
} }
/// Set the links in a header and return the old links, if any. /// Set the links in a header and return the old links, if any.
fn set_internal_links(&mut self, links: Vec<&mut Entry>) -> Result<Vec<Link>> { fn set_internal_links(&mut self, links: Vec<&mut Entry>) -> Result<LinkIter> {
use internal::iter::IntoValues;
let self_location = self.get_location().clone(); let self_location = self.get_location().clone();
let mut new_links = vec![]; let mut new_links = vec![];
@ -69,8 +127,8 @@ impl InternalLinker for Entry {
new_links.push(link); new_links.push(link);
} }
let new_links = try!(links_into_values(new_links) let new_links = try!(LinkIter::new(new_links)
.into_iter() .into_values()
.fold(Ok(vec![]), |acc, elem| { .fold(Ok(vec![]), |acc, elem| {
acc.and_then(move |mut v| { acc.and_then(move |mut v| {
elem.map_err_into(LEK::InternalConversionError) elem.map_err_into(LEK::InternalConversionError)
@ -89,8 +147,8 @@ impl InternalLinker for Entry {
add_foreign_link(link, self.get_location().clone()) add_foreign_link(link, self.get_location().clone())
.and_then(|_| { .and_then(|_| {
self.get_internal_links() self.get_internal_links()
.and_then(|mut links| { .and_then(|links| {
links.push(new_link); let links = links.chain(LinkIter::new(vec![new_link]));
rewrite_links(self.get_header_mut(), links) rewrite_links(self.get_header_mut(), links)
}) })
}) })
@ -102,43 +160,20 @@ impl InternalLinker for Entry {
link.get_internal_links() link.get_internal_links()
.and_then(|links| { .and_then(|links| {
let links = links.into_iter().filter(|l| l.clone() != own_loc).collect(); rewrite_links(self.get_header_mut(), links.filter(|l| *l != own_loc))
rewrite_links(self.get_header_mut(), links)
}) })
.and_then(|_| { .and_then(|_| {
self.get_internal_links() self.get_internal_links()
.and_then(|links| { .and_then(|links| {
let links = links.into_iter().filter(|l| l.clone() != other_loc).collect(); rewrite_links(link.get_header_mut(), links.filter(|l| *l != other_loc))
rewrite_links(link.get_header_mut(), links)
}) })
}) })
} }
} }
fn links_into_values(links: Vec<StoreId>) -> Vec<Result<Value>> { fn rewrite_links<I: Iterator<Item = Link>>(header: &mut EntryHeader, links: I) -> Result<()> {
links let links = try!(links.into_values()
.into_iter()
.map(|s| s.without_base().to_str().map_err_into(LEK::InternalConversionError))
.unique_by(|entry| {
match entry {
&Ok(ref e) => Some(e.clone()),
&Err(_) => None,
}
})
.map(|elem| elem.map(Value::String))
.sorted_by(|a, b| {
match (a, b) {
(&Ok(Value::String(ref a)), &Ok(Value::String(ref b))) => Ord::cmp(a, b),
(&Err(_), _) | (_, &Err(_)) => Ordering::Equal,
_ => unreachable!()
}
})
}
fn rewrite_links(header: &mut EntryHeader, links: Vec<StoreId>) -> Result<()> {
let links = try!(links_into_values(links)
.into_iter()
.fold(Ok(vec![]), |acc, elem| { .fold(Ok(vec![]), |acc, elem| {
acc.and_then(move |mut v| { acc.and_then(move |mut v| {
elem.map_err_into(LEK::InternalConversionError) elem.map_err_into(LEK::InternalConversionError)
@ -157,10 +192,10 @@ fn rewrite_links(header: &mut EntryHeader, links: Vec<StoreId>) -> Result<()> {
/// This is a helper function which does this. /// This is a helper function which does this.
fn add_foreign_link(target: &mut Entry, from: StoreId) -> Result<()> { fn add_foreign_link(target: &mut Entry, from: StoreId) -> Result<()> {
target.get_internal_links() target.get_internal_links()
.and_then(|mut links| { .and_then(|links| {
links.push(from); let links = try!(links
let links = try!(links_into_values(links) .chain(LinkIter::new(vec![from]))
.into_iter() .into_values()
.fold(Ok(vec![]), |acc, elem| { .fold(Ok(vec![]), |acc, elem| {
acc.and_then(move |mut v| { acc.and_then(move |mut v| {
elem.map_err_into(LEK::InternalConversionError) elem.map_err_into(LEK::InternalConversionError)
@ -175,7 +210,7 @@ fn add_foreign_link(target: &mut Entry, from: StoreId) -> Result<()> {
}) })
} }
fn process_rw_result(links: StoreResult<Option<Value>>) -> Result<Vec<Link>> { fn process_rw_result(links: StoreResult<Option<Value>>) -> Result<LinkIter> {
use std::path::PathBuf; use std::path::PathBuf;
let links = match links { let links = match links {
@ -185,7 +220,7 @@ fn process_rw_result(links: StoreResult<Option<Value>>) -> Result<Vec<Link>> {
}, },
Ok(None) => { Ok(None) => {
debug!("We got no value from the header!"); debug!("We got no value from the header!");
return Ok(vec![]) return Ok(LinkIter::new(vec![]))
}, },
Ok(Some(Value::Array(l))) => l, Ok(Some(Value::Array(l))) => l,
Ok(Some(_)) => { Ok(Some(_)) => {
@ -211,6 +246,6 @@ fn process_rw_result(links: StoreResult<Option<Value>>) -> Result<Vec<Link>> {
.collect()); .collect());
debug!("Ok, the RW action was successful, returning link vector now!"); debug!("Ok, the RW action was successful, returning link vector now!");
Ok(links) Ok(LinkIter::new(links))
} }