Merge pull request #395 from matthiasbeyer/libimagstore/store-id-iterator-generic
Libimagstore/store id iterator generic
This commit is contained in:
commit
0c89fafe20
2 changed files with 54 additions and 6 deletions
|
@ -35,6 +35,8 @@ use hook::accessor::{ MutableHookDataAccessor,
|
||||||
use hook::position::HookPosition;
|
use hook::position::HookPosition;
|
||||||
use hook::Hook;
|
use hook::Hook;
|
||||||
|
|
||||||
|
use self::glob_store_iter::*;
|
||||||
|
|
||||||
/// The Result Type returned by any interaction with the store that could fail
|
/// The Result Type returned by any interaction with the store that could fail
|
||||||
pub type Result<T> = RResult<T, StoreError>;
|
pub type Result<T> = RResult<T, StoreError>;
|
||||||
|
|
||||||
|
@ -361,7 +363,7 @@ impl Store {
|
||||||
let path = [ path, "/*" ].join("");
|
let path = [ path, "/*" ].join("");
|
||||||
debug!("glob()ing with '{}'", path);
|
debug!("glob()ing with '{}'", path);
|
||||||
glob(&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))))
|
.map_err(|e| StoreError::new(StoreErrorKind::GlobError, Some(Box::new(e))))
|
||||||
} else {
|
} else {
|
||||||
Err(StoreError::new(StoreErrorKind::EncodingError, None))
|
Err(StoreError::new(StoreErrorKind::EncodingError, None))
|
||||||
|
@ -1253,6 +1255,53 @@ impl Entry {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod glob_store_iter {
|
||||||
|
use std::fmt::{Debug, Formatter};
|
||||||
|
use std::fmt::Error as FmtError;
|
||||||
|
use glob::Paths;
|
||||||
|
use storeid::StoreId;
|
||||||
|
|
||||||
|
pub struct GlobStoreIdIterator {
|
||||||
|
paths: Paths,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Debug for GlobStoreIdIterator {
|
||||||
|
|
||||||
|
fn fmt(&self, fmt: &mut Formatter) -> Result<(), 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| {
|
||||||
|
match o {
|
||||||
|
Ok(o) => Some(o),
|
||||||
|
Err(e) => {
|
||||||
|
debug!("GlobStoreIdIterator error: {:?}", e);
|
||||||
|
None
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}).map(|p| StoreId::from(p))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
|
|
@ -3,7 +3,6 @@ use std::path::Path;
|
||||||
use std::borrow::Borrow;
|
use std::borrow::Borrow;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
use glob::Paths;
|
|
||||||
use semver::Version;
|
use semver::Version;
|
||||||
use std::fmt::{Debug, Formatter};
|
use std::fmt::{Debug, Formatter};
|
||||||
use std::fmt::Error as FmtError;
|
use std::fmt::Error as FmtError;
|
||||||
|
@ -156,7 +155,7 @@ macro_rules! module_entry_path_mod {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct StoreIdIterator {
|
pub struct StoreIdIterator {
|
||||||
paths: Paths,
|
iter: Box<Iterator<Item = StoreId>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Debug for StoreIdIterator {
|
impl Debug for StoreIdIterator {
|
||||||
|
@ -169,9 +168,9 @@ impl Debug for StoreIdIterator {
|
||||||
|
|
||||||
impl StoreIdIterator {
|
impl StoreIdIterator {
|
||||||
|
|
||||||
pub fn new(paths: Paths) -> StoreIdIterator {
|
pub fn new(iter: Box<Iterator<Item = StoreId>>) -> StoreIdIterator {
|
||||||
StoreIdIterator {
|
StoreIdIterator {
|
||||||
paths: paths,
|
iter: iter,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -181,7 +180,7 @@ impl Iterator for StoreIdIterator {
|
||||||
type Item = StoreId;
|
type Item = StoreId;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<StoreId> {
|
fn next(&mut self) -> Option<StoreId> {
|
||||||
self.paths.next().and_then(|o| o.ok()).map(|p| StoreId::from(p))
|
self.iter.next()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue