Add cli submodule for building and parsing CLI subcomponents

This commit is contained in:
Matthias Beyer 2016-03-26 16:15:43 +01:00
parent 19f89c5864
commit 9a9b7fcc58
7 changed files with 67 additions and 8 deletions

View file

@ -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 <mail@beyermatthias.de>")
.version("0.1")
.about("List entries")
}
pub fn list_subcommand_name() -> &'static str {
"list"
}

View file

@ -4,6 +4,7 @@ extern crate toml;
extern crate libimagstore;
pub mod cli;
pub mod error;
pub mod lister;
pub mod listers;

View file

@ -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<Item = FileLockEntry<'a>>>(&self, entries: I) -> Result<()>;

View file

@ -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<Item = FileLockEntry<'b>>>(&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))))
})
})
}
}

View file

@ -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))))
})
})
}
}

View file

@ -1,2 +1,3 @@
pub mod core;
pub mod line;
pub mod path;

View file

@ -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 {