diff --git a/libimagentrylist/Cargo.toml b/libimagentrylist/Cargo.toml new file mode 100644 index 00000000..659a81f5 --- /dev/null +++ b/libimagentrylist/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "libimagentrylist" +version = "0.1.0" +authors = ["Matthias Beyer "] + +[dependencies] +log = "0.3.5" +toml = "0.1.25" + +[dependencies.libimagstore] +path = "../libimagstore" + diff --git a/libimagentrylist/README.md b/libimagentrylist/README.md new file mode 100644 index 00000000..bab5dc62 --- /dev/null +++ b/libimagentrylist/README.md @@ -0,0 +1,12 @@ +# libimagentrylist + +Library for listing entries in different manner. + +This includes: + +* Plain one-line-one-entry-path listing +* Tree listing by submodule +* Listing with metadata + * One-line-one-entry + * ASCII-Table + diff --git a/libimagentrylist/src/error.rs b/libimagentrylist/src/error.rs new file mode 100644 index 00000000..82f6b387 --- /dev/null +++ b/libimagentrylist/src/error.rs @@ -0,0 +1,83 @@ +use std::error::Error; +use std::fmt::Error as FmtError; +use std::clone::Clone; +use std::fmt::{Display, Formatter}; + +/** + * Kind of error + */ +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum ListErrorKind { + FormatError, + EntryError, + IterationError +} + +fn counter_error_type_as_str(err: &ListErrorKind) -> &'static str{ + match err { + &ListErrorKind::FormatError => "FormatError", + &ListErrorKind::EntryError => "EntryError", + &ListErrorKind::IterationError => "IterationError", + } +} + +impl Display for ListErrorKind { + + fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { + try!(write!(fmt, "{}", counter_error_type_as_str(self))); + Ok(()) + } + +} + +/** + * Store error type + */ +#[derive(Debug)] +pub struct ListError { + err_type: ListErrorKind, + cause: Option>, +} + +impl ListError { + + /** + * Build a new ListError from an ListErrorKind, optionally with cause + */ + pub fn new(errtype: ListErrorKind, cause: Option>) -> ListError { + ListError { + err_type: errtype, + cause: cause, + } + } + + /** + * Get the error type of this ListError + */ + pub fn err_type(&self) -> ListErrorKind { + self.err_type.clone() + } + +} + +impl Display for ListError { + + fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { + try!(write!(fmt, "[{}]", counter_error_type_as_str(&self.err_type.clone()))); + Ok(()) + } + +} + +impl Error for ListError { + + fn description(&self) -> &str { + counter_error_type_as_str(&self.err_type.clone()) + } + + fn cause(&self) -> Option<&Error> { + self.cause.as_ref().map(|e| &**e) + } + +} + diff --git a/libimagentrylist/src/lib.rs b/libimagentrylist/src/lib.rs new file mode 100644 index 00000000..c9c369fa --- /dev/null +++ b/libimagentrylist/src/lib.rs @@ -0,0 +1,10 @@ +#[macro_use] extern crate log; +extern crate toml; + +extern crate libimagstore; + +pub mod error; +pub mod lister; +pub mod listers; +pub mod result; + diff --git a/libimagentrylist/src/lister.rs b/libimagentrylist/src/lister.rs new file mode 100644 index 00000000..b54328e6 --- /dev/null +++ b/libimagentrylist/src/lister.rs @@ -0,0 +1,10 @@ +use libimagstore::store::FileLockEntry; + +use result::Result; + +pub trait Lister { + + fn list<'a, I: Iterator>>(&self, entries: I) -> Result<()>; + +} + diff --git a/libimagentrylist/src/listers/line.rs b/libimagentrylist/src/listers/line.rs new file mode 100644 index 00000000..214f4a05 --- /dev/null +++ b/libimagentrylist/src/listers/line.rs @@ -0,0 +1,40 @@ +use std::io::stdout; +use std::io::Write; +use std::ops::Deref; + +use lister::Lister; +use result::Result; + +use libimagstore::store::FileLockEntry; +use libimagstore::store::Entry; + +pub struct LineLister<'a> { + lister: &'a Fn(&Entry) -> String, +} + +impl<'a> LineLister<'a> { + + pub fn new(lister: &'a Fn(&Entry) -> String) -> LineLister<'a> { + LineLister { + lister: lister, + } + } + +} + +impl<'a> Lister for LineLister<'a> { + + fn list<'b, I: Iterator>>(&self, entries: I) -> Result<()> { + use error::ListError as LE; + use error::ListErrorKind as LEK; + + entries.fold(Ok(()), |accu, entry| { + accu.and_then(|_| { + write!(stdout(), "{:?}\n", (self.lister)(entry.deref())) + .map_err(|e| LE::new(LEK::FormatError, Some(Box::new(e)))) + }) + }) + } + +} + diff --git a/libimagentrylist/src/listers/mod.rs b/libimagentrylist/src/listers/mod.rs new file mode 100644 index 00000000..4f0ca92e --- /dev/null +++ b/libimagentrylist/src/listers/mod.rs @@ -0,0 +1,2 @@ +pub mod line; +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))) + } + }) + }) + } + +} + diff --git a/libimagentrylist/src/result.rs b/libimagentrylist/src/result.rs new file mode 100644 index 00000000..729cce41 --- /dev/null +++ b/libimagentrylist/src/result.rs @@ -0,0 +1,6 @@ +use std::result::Result as RResult; + +use error::ListError; + +pub type Result = RResult; +