diff --git a/libimagentrylist/src/cli.rs b/libimagentrylist/src/cli.rs new file mode 100644 index 00000000..c3823550 --- /dev/null +++ b/libimagentrylist/src/cli.rs @@ -0,0 +1,13 @@ +use clap::{Arg, App, SubCommand}; + +pub fn build_list_cli_component<'a, 'b>() -> App<'a, 'b> { + SubCommand::with_name(list_subcommand_name()) + .author("Matthias Beyer ") + .version("0.1") + .about("List entries") +} + +pub fn list_subcommand_name() -> &'static str { + "list" +} + diff --git a/libimagentrylist/src/lib.rs b/libimagentrylist/src/lib.rs index 9093ba4c..e42eb98d 100644 --- a/libimagentrylist/src/lib.rs +++ b/libimagentrylist/src/lib.rs @@ -4,6 +4,7 @@ extern crate toml; extern crate libimagstore; +pub mod cli; pub mod error; pub mod lister; pub mod listers; diff --git a/libimagentrylist/src/lister.rs b/libimagentrylist/src/lister.rs index b54328e6..2913a7a6 100644 --- a/libimagentrylist/src/lister.rs +++ b/libimagentrylist/src/lister.rs @@ -1,8 +1,10 @@ +use clap::ArgMatches; + use libimagstore::store::FileLockEntry; use result::Result; -pub trait Lister { +pub trait Lister : Sized { fn list<'a, I: Iterator>>(&self, entries: I) -> Result<()>; diff --git a/libimagentrylist/src/listers/core.rs b/libimagentrylist/src/listers/core.rs new file mode 100644 index 00000000..e7c5bb84 --- /dev/null +++ b/libimagentrylist/src/listers/core.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 CoreLister<'a> { + lister: &'a Fn(&Entry) -> String, +} + +impl<'a> CoreLister<'a> { + + pub fn new(lister: &'a Fn(&Entry) -> String) -> CoreLister<'a> { + CoreLister { + lister: lister, + } + } + +} + +impl<'a> Lister for CoreLister<'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/line.rs b/libimagentrylist/src/listers/line.rs index 214f4a05..04220e34 100644 --- a/libimagentrylist/src/listers/line.rs +++ b/libimagentrylist/src/listers/line.rs @@ -1,22 +1,22 @@ use std::io::stdout; use std::io::Write; -use std::ops::Deref; +use cli::list_subcommand_name; use lister::Lister; use result::Result; +use clap::ArgMatches; use libimagstore::store::FileLockEntry; -use libimagstore::store::Entry; pub struct LineLister<'a> { - lister: &'a Fn(&Entry) -> String, + unknown_output: &'a str, } impl<'a> LineLister<'a> { - pub fn new(lister: &'a Fn(&Entry) -> String) -> LineLister<'a> { + pub fn new(unknown_output: &'a str) -> LineLister<'a> { LineLister { - lister: lister, + unknown_output: unknown_output, } } @@ -30,11 +30,11 @@ impl<'a> Lister for LineLister<'a> { entries.fold(Ok(()), |accu, entry| { accu.and_then(|_| { - write!(stdout(), "{:?}\n", (self.lister)(entry.deref())) + write!(stdout(), "{:?}\n", + entry.get_location().to_str().unwrap_or(self.unknown_output)) .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 4f0ca92e..d4e06f93 100644 --- a/libimagentrylist/src/listers/mod.rs +++ b/libimagentrylist/src/listers/mod.rs @@ -1,2 +1,3 @@ +pub mod core; pub mod line; pub mod path; diff --git a/libimagentrylist/src/listers/path.rs b/libimagentrylist/src/listers/path.rs index 285802e2..5e9d82b5 100644 --- a/libimagentrylist/src/listers/path.rs +++ b/libimagentrylist/src/listers/path.rs @@ -2,9 +2,11 @@ use std::io::stdout; use std::io::Write; use std::ops::Deref; +use cli::list_subcommand_name; use lister::Lister; use result::Result; +use clap::ArgMatches; use libimagstore::store::FileLockEntry; pub struct PathLister {