diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-10-17 15:45:04 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-10-20 14:11:47 +0200 |
commit | 7806cf34c12ccacbc9fd2fd3ac3fa237d9d1acbe (patch) | |
tree | ee37f3f4d232b319fc3272731337ba1e70157902 /libimagentrylink | |
parent | 19b60c3776cda364e3e51ab16c7b32ad3432f806 (diff) |
Refactor FilterCompareLinkCountIter into FilterLinksIter
Diffstat (limited to 'libimagentrylink')
-rw-r--r-- | libimagentrylink/src/internal.rs | 49 |
1 files changed, 36 insertions, 13 deletions
diff --git a/libimagentrylink/src/internal.rs b/libimagentrylink/src/internal.rs index 088034cc..1627db8d 100644 --- a/libimagentrylink/src/internal.rs +++ b/libimagentrylink/src/internal.rs @@ -125,18 +125,36 @@ pub mod iter { DeleteUnlinkedIter(self) } - /// Turn this iterator into a FilterCompareLinkCountIter, which filters out the unlinked - /// entries. - pub fn without_unlinked(self) -> FilterCompareLinkCountIter<'a> { - FilterCompareLinkCountIter(gi, |u: usize| u > n) + /// Turn this iterator into a FilterLinksIter that removes all entries that are not linked + /// to any other entry, by filtering them out the iterator. + /// + /// This does _not_ remove the entries from the store. + pub fn without_unlinked(self) -> FilterLinksIter<'a> { + FilterLinksIter::new(self, Box::new(|links: &[Link]| links.len() > 0)) } - pub fn with_less_than_n_links(self, n: usize) -> FilterCompareLinkCountIter<'a> { - FilterCompareLinkCountIter(gi, |u: usize| u < n) + /// Turn this iterator into a FilterLinksIter that removes all entries that have less than + /// `n` links to any other entries. + /// + /// This does _not_ remove the entries from the store. + pub fn with_less_than_n_links(self, n: usize) -> FilterLinksIter<'a> { + FilterLinksIter::new(self, Box::new(move |links: &[Link]| links.len() < n)) } - pub fn with_more_than_n_links(self, n: usize) -> FilterMoreThanIter<'a> { - FilterMoreThanIter(self, n) + /// Turn this iterator into a FilterLinksIter that removes all entries that have more than + /// `n` links to any other entries. + /// + /// This does _not_ remove the entries from the store. + pub fn with_more_than_n_links(self, n: usize) -> FilterLinksIter<'a> { + FilterLinksIter::new(self, Box::new(move |links: &[Link]| links.len() > n)) + } + + /// Turn this iterator into a FilterLinksIter that removes all entries where the predicate + /// `F` returns false + /// + /// This does _not_ remove the entries from the store. + pub fn filtered_for_links(self, f: Box<Fn(&[Link]) -> bool>) -> FilterLinksIter<'a> { + FilterLinksIter::new(self, f) } pub fn store(&self) -> &Store { @@ -161,10 +179,15 @@ pub mod iter { /// /// If the function F returns `false` for the number of links, the entry is ignored, else it is /// taken. - struct FilterCompareLinkCountIter<'a, F>(GetIter<'a>, F) - where F: FnOnce(usize) -> bool; + pub struct FilterLinksIter<'a>(GetIter<'a>, Box<Fn(&[Link]) -> bool>); + + impl<'a> FilterLinksIter<'a> { + pub fn new(gi: GetIter<'a>, f: Box<Fn(&[Link]) -> bool>) -> FilterLinksIter<'a> { + FilterLinksIter(gi, f) + } + } - impl<'a> Iterator for FilterCompareLinkCountIter<'a> { + impl<'a> Iterator for FilterLinksIter<'a> { type Item = Result<FileLockEntry<'a>>; fn next(&mut self) -> Option<Self::Item> { @@ -176,9 +199,9 @@ pub mod iter { let links = match fle.get_internal_links().map_err_into(LEK::StoreReadError) { Err(e) => return Some(Err(e)), - Ok(links) => links, + Ok(links) => links.collect::<Vec<_>>(), }; - if !self.1(links.count()) { + if !(self.1)(&links) { continue; } else { return Some(Ok(fle)); |