From 13fa3281983a6aeb1e94db3fdcf18c480ecf073a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 24 Mar 2016 13:55:10 +0100 Subject: [PATCH 1/6] Initial import --- libimagentrylist/Cargo.toml | 6 ++++++ libimagentrylist/src/lib.rs | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 libimagentrylist/Cargo.toml create mode 100644 libimagentrylist/src/lib.rs diff --git a/libimagentrylist/Cargo.toml b/libimagentrylist/Cargo.toml new file mode 100644 index 00000000..acfa2f99 --- /dev/null +++ b/libimagentrylist/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "libimagentrylist" +version = "0.1.0" +authors = ["Matthias Beyer "] + +[dependencies] diff --git a/libimagentrylist/src/lib.rs b/libimagentrylist/src/lib.rs new file mode 100644 index 00000000..cf4da5ad --- /dev/null +++ b/libimagentrylist/src/lib.rs @@ -0,0 +1,6 @@ +#[cfg(test)] +mod test { + #[test] + fn it_works() { + } +} From a8476c4229b6015772464214f6bae0fd8186499b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 24 Mar 2016 14:02:32 +0100 Subject: [PATCH 2/6] Add README for this library --- libimagentrylist/README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 libimagentrylist/README.md 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 + From 8b1c36a86ea50e9d20ef76923f1bb0f75f21329f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 26 Mar 2016 14:10:37 +0100 Subject: [PATCH 3/6] Add dependencies --- libimagentrylist/Cargo.toml | 6 ++++++ libimagentrylist/src/lib.rs | 11 +++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/libimagentrylist/Cargo.toml b/libimagentrylist/Cargo.toml index acfa2f99..659a81f5 100644 --- a/libimagentrylist/Cargo.toml +++ b/libimagentrylist/Cargo.toml @@ -4,3 +4,9 @@ version = "0.1.0" authors = ["Matthias Beyer "] [dependencies] +log = "0.3.5" +toml = "0.1.25" + +[dependencies.libimagstore] +path = "../libimagstore" + diff --git a/libimagentrylist/src/lib.rs b/libimagentrylist/src/lib.rs index cf4da5ad..880c1231 100644 --- a/libimagentrylist/src/lib.rs +++ b/libimagentrylist/src/lib.rs @@ -1,6 +1,5 @@ -#[cfg(test)] -mod test { - #[test] - fn it_works() { - } -} +#[macro_use] extern crate log; +extern crate toml; + +extern crate libimagstore; + From 52eb81a6f78833a2b3135f55b407e3123e789f06 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 26 Mar 2016 14:41:34 +0100 Subject: [PATCH 4/6] Add initial codebase --- libimagentrylist/src/error.rs | 83 ++++++++++++++++++++++++++++++++++ libimagentrylist/src/lib.rs | 4 ++ libimagentrylist/src/lister.rs | 10 ++++ libimagentrylist/src/result.rs | 6 +++ 4 files changed, 103 insertions(+) create mode 100644 libimagentrylist/src/error.rs create mode 100644 libimagentrylist/src/lister.rs create mode 100644 libimagentrylist/src/result.rs 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 index 880c1231..b47d1f2e 100644 --- a/libimagentrylist/src/lib.rs +++ b/libimagentrylist/src/lib.rs @@ -3,3 +3,7 @@ extern crate toml; extern crate libimagstore; +pub mod error; +pub mod lister; +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/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; + From 5fe843e5d6064b99e802f5793bc052d8355047a9 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 26 Mar 2016 14:59:03 +0100 Subject: [PATCH 5/6] 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))) + } + }) + }) + } + +} + From fdc7e2c3914bf69c5c86e1c9d805b2ed5dc2606b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 26 Mar 2016 15:27:19 +0100 Subject: [PATCH 6/6] Add LineLister --- libimagentrylist/src/listers/line.rs | 40 ++++++++++++++++++++++++++++ libimagentrylist/src/listers/mod.rs | 1 + 2 files changed, 41 insertions(+) create mode 100644 libimagentrylist/src/listers/line.rs 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 index 4da97892..4f0ca92e 100644 --- a/libimagentrylist/src/listers/mod.rs +++ b/libimagentrylist/src/listers/mod.rs @@ -1 +1,2 @@ +pub mod line; pub mod path;