Add simple PathLister type

This commit is contained in:
Matthias Beyer 2016-03-26 14:59:03 +01:00
parent 52eb81a6f7
commit 5fe843e5d6
3 changed files with 56 additions and 0 deletions

View file

@ -5,5 +5,6 @@ extern crate libimagstore;
pub mod error;
pub mod lister;
pub mod listers;
pub mod result;

View file

@ -0,0 +1 @@
pub mod path;

View file

@ -0,0 +1,54 @@
use std::io::stdout;
use std::io::Write;
use std::ops::Deref;
use lister::Lister;
use result::Result;
use libimagstore::store::FileLockEntry;
pub struct PathLister {
absolute: bool,
}
impl PathLister {
pub fn new(absolute: bool) -> PathLister {
PathLister {
absolute: absolute,
}
}
}
impl Lister for PathLister {
fn list<'a, I: Iterator<Item = FileLockEntry<'a>>>(&self, entries: I) -> Result<()> {
use error::ListError as LE;
use error::ListErrorKind as LEK;
entries.fold(Ok(()), |accu, entry| {
accu.and_then(|_| Ok(entry.deref().get_location().clone()))
.and_then(|pb| {
if self.absolute {
pb.canonicalize().map_err(|e| LE::new(LEK::FormatError, Some(Box::new(e))))
} else {
Ok(pb)
}
})
.and_then(|pb| {
write!(stdout(), "{:?}\n", pb)
.map_err(|e| LE::new(LEK::FormatError, Some(Box::new(e))))
})
.map_err(|e| {
if e.err_type() == LEK::FormatError {
e
} else {
LE::new(LEK::FormatError, Some(Box::new(e)))
}
})
})
}
}