From 5fe843e5d6064b99e802f5793bc052d8355047a9 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 26 Mar 2016 14:59:03 +0100 Subject: [PATCH] Add simple PathLister type --- libimagentrylist/src/lib.rs | 1 + libimagentrylist/src/listers/mod.rs | 1 + libimagentrylist/src/listers/path.rs | 54 ++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 libimagentrylist/src/listers/mod.rs create mode 100644 libimagentrylist/src/listers/path.rs diff --git a/libimagentrylist/src/lib.rs b/libimagentrylist/src/lib.rs index b47d1f2e..c9c369fa 100644 --- a/libimagentrylist/src/lib.rs +++ b/libimagentrylist/src/lib.rs @@ -5,5 +5,6 @@ extern crate libimagstore; pub mod error; pub mod lister; +pub mod listers; pub mod result; diff --git a/libimagentrylist/src/listers/mod.rs b/libimagentrylist/src/listers/mod.rs new file mode 100644 index 00000000..4da97892 --- /dev/null +++ b/libimagentrylist/src/listers/mod.rs @@ -0,0 +1 @@ +pub mod path; diff --git a/libimagentrylist/src/listers/path.rs b/libimagentrylist/src/listers/path.rs new file mode 100644 index 00000000..285802e2 --- /dev/null +++ b/libimagentrylist/src/listers/path.rs @@ -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>>(&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))) + } + }) + }) + } + +} +