Make StoreIdIterator generic

This patch makes the StoreIdIterator type generic over all iterators
with Item = StoreId.

As the StoreIdIterator type was previousely used for iterating over a
glob() result, I had to wrap this result type in another iterator type
which is then wrapped in StoreIdIterator.

With this patch applied, other libraries may use the StoreIdIterator
type to abstract things away in their implementation.
This commit is contained in:
Matthias Beyer 2016-05-12 17:21:04 +02:00
parent 9f528fb929
commit 5972ce7f37
2 changed files with 37 additions and 6 deletions

View file

@ -25,7 +25,7 @@ use walkdir::Iter as WalkDirIter;
use error::{ParserErrorKind, ParserError};
use error::{StoreError, StoreErrorKind};
use storeid::{IntoStoreId, StoreId, StoreIdIterator};
use storeid::{IntoStoreId, StoreId, GlobStoreIdIterator, StoreIdIterator};
use lazyfile::LazyFile;
use hook::aspect::Aspect;
@ -361,7 +361,7 @@ impl Store {
let path = [ path, "/*" ].join("");
debug!("glob()ing with '{}'", path);
glob(&path[..])
.map(StoreIdIterator::new)
.map(|paths| StoreIdIterator::new(Box::new(GlobStoreIdIterator::new(paths))))
.map_err(|e| StoreError::new(StoreErrorKind::GlobError, Some(Box::new(e))))
} else {
Err(StoreError::new(StoreErrorKind::EncodingError, None))

View file

@ -155,10 +155,41 @@ macro_rules! module_entry_path_mod {
)
}
pub struct StoreIdIterator {
pub struct GlobStoreIdIterator {
paths: Paths,
}
impl Debug for GlobStoreIdIterator {
fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FmtError> {
write!(fmt, "GlobStoreIdIterator")
}
}
impl GlobStoreIdIterator {
pub fn new(paths: Paths) -> GlobStoreIdIterator {
GlobStoreIdIterator {
paths: paths,
}
}
}
impl Iterator for GlobStoreIdIterator {
type Item = StoreId;
fn next(&mut self) -> Option<StoreId> {
self.paths.next().and_then(|o| o.ok()).map(|p| StoreId::from(p))
}
}
pub struct StoreIdIterator {
iter: Box<Iterator<Item = StoreId>>,
}
impl Debug for StoreIdIterator {
fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FmtError> {
@ -169,9 +200,9 @@ impl Debug for StoreIdIterator {
impl StoreIdIterator {
pub fn new(paths: Paths) -> StoreIdIterator {
pub fn new(iter: Box<Iterator<Item = StoreId>>) -> StoreIdIterator {
StoreIdIterator {
paths: paths,
iter: iter,
}
}
@ -181,7 +212,7 @@ impl Iterator for StoreIdIterator {
type Item = StoreId;
fn next(&mut self) -> Option<StoreId> {
self.paths.next().and_then(|o| o.ok()).map(|p| StoreId::from(p))
self.iter.next()
}
}