diff --git a/lib/core/libimagstore/src/file_abstraction/iter.rs b/lib/core/libimagstore/src/file_abstraction/iter.rs index c0bd8b5f..d7ae82d9 100644 --- a/lib/core/libimagstore/src/file_abstraction/iter.rs +++ b/lib/core/libimagstore/src/file_abstraction/iter.rs @@ -20,6 +20,7 @@ use std::path::PathBuf; use error::Result; +use storeid::StoreId; /// A wrapper for an iterator over `PathBuf`s pub struct PathIterator(Box>>); @@ -30,6 +31,10 @@ impl PathIterator { PathIterator(iter) } + pub fn store_id_constructing(self, storepath: PathBuf) -> StoreIdConstructingIterator { + StoreIdConstructingIterator(self, storepath) + } + } impl Iterator for PathIterator { @@ -41,3 +46,29 @@ impl Iterator for PathIterator { } + +/// Helper type for constructing StoreIds from a PathIterator. +/// +/// Automatically ignores non-files. +pub struct StoreIdConstructingIterator(PathIterator, PathBuf); + +impl Iterator for StoreIdConstructingIterator { + type Item = Result; + + fn next(&mut self) -> Option { + while let Some(next) = self.0.next() { + match next { + Err(e) => return Some(Err(e)), + Ok(next) => if next.is_file() { + return Some(StoreId::from_full_path(&self.1, next)) + } else { + continue + } + } + } + + None + } + +} + diff --git a/lib/core/libimagstore/src/file_abstraction/mod.rs b/lib/core/libimagstore/src/file_abstraction/mod.rs index 893b8ca2..a3172735 100644 --- a/lib/core/libimagstore/src/file_abstraction/mod.rs +++ b/lib/core/libimagstore/src/file_abstraction/mod.rs @@ -27,7 +27,7 @@ use storeid::StoreId; mod fs; mod inmemory; -mod iter; +pub(crate) mod iter; pub use self::fs::FSFileAbstraction; pub use self::fs::FSFileAbstractionInstance; diff --git a/lib/core/libimagstore/src/iter.rs b/lib/core/libimagstore/src/iter.rs index 0171a93b..63100470 100644 --- a/lib/core/libimagstore/src/iter.rs +++ b/lib/core/libimagstore/src/iter.rs @@ -32,10 +32,10 @@ macro_rules! mk_iterator { use store::Store; use error::Result; - pub struct $itername<'a>(Box + 'a>, &'a Store); + pub struct $itername<'a>(Box> + 'a>, &'a Store); impl<'a> $itername<'a> { - pub fn new(inner: Box + 'a>, store: &'a Store) -> Self { + pub fn new(inner: Box> + 'a>, store: &'a Store) -> Self { $itername(inner, store) } } @@ -44,7 +44,7 @@ macro_rules! mk_iterator { type Item = Result<$yield>; fn next(&mut self) -> Option { - self.0.next().map(|id| $fun(id, self.1)) + self.0.next().map(|id| $fun(id?, self.1)) } } @@ -53,7 +53,7 @@ macro_rules! mk_iterator { } impl<'a, I> $extname<'a> for I - where I: Iterator + 'a + where I: Iterator> + 'a { fn $extfnname(self, store: &'a Store) -> $itername<'a> { $itername(Box::new(self), store) diff --git a/lib/core/libimagstore/src/store.rs b/lib/core/libimagstore/src/store.rs index 7edf3f05..c401594a 100644 --- a/lib/core/libimagstore/src/store.rs +++ b/lib/core/libimagstore/src/store.rs @@ -741,25 +741,12 @@ impl Store { } /// Get _all_ entries in the store (by id as iterator) - pub fn entries<'a>(&'a self) -> Result> { + pub fn entries(&self) -> Result { self.backend .pathes_recursively(self.path().clone()) - .and_then(|iter| { - let mut elems = vec![]; - for element in iter { - let is_file = { - let mut base = self.path().clone(); - base.push(element.clone()); - self.backend.is_file(&base)? - }; - - if is_file { - let sid = StoreId::from_full_path(self.path(), element)?; - elems.push(sid); - } - } - Ok(StoreIdIteratorWithStore::new(Box::new(elems.into_iter()), self)) - }) + .map(|i| i.store_id_constructing(self.path().clone())) + .map(Box::new) + .map(|it| StoreIdIteratorWithStore::new(it, self)) } /// Gets the path where this store is on the disk diff --git a/lib/core/libimagstore/src/storeid.rs b/lib/core/libimagstore/src/storeid.rs index 2bcd9ba7..80e1b35d 100644 --- a/lib/core/libimagstore/src/storeid.rs +++ b/lib/core/libimagstore/src/storeid.rs @@ -241,7 +241,7 @@ macro_rules! module_entry_path_mod { } pub struct StoreIdIterator { - iter: Box>, + iter: Box>>, } impl Debug for StoreIdIterator { @@ -254,16 +254,16 @@ impl Debug for StoreIdIterator { impl StoreIdIterator { - pub fn new(iter: Box>) -> StoreIdIterator { + pub fn new(iter: Box>>) -> StoreIdIterator { StoreIdIterator { iter } } } impl Iterator for StoreIdIterator { - type Item = StoreId; + type Item = Result; - fn next(&mut self) -> Option { + fn next(&mut self) -> Option { self.iter.next() } @@ -280,16 +280,16 @@ impl<'a> Deref for StoreIdIteratorWithStore<'a> { } impl<'a> Iterator for StoreIdIteratorWithStore<'a> { - type Item = StoreId; + type Item = Result; - fn next(&mut self) -> Option { + fn next(&mut self) -> Option { self.0.next() } } impl<'a> StoreIdIteratorWithStore<'a> { - pub fn new(iter: Box>, store: &'a Store) -> Self { + pub fn new(iter: Box>>, store: &'a Store) -> Self { StoreIdIteratorWithStore(StoreIdIterator::new(iter), store) }