Merge pull request #267 from matthiasbeyer/libimagentrylist/init
Libimagentrylist/init
This commit is contained in:
commit
2ed64d5b2e
9 changed files with 229 additions and 0 deletions
12
libimagentrylist/Cargo.toml
Normal file
12
libimagentrylist/Cargo.toml
Normal file
|
@ -0,0 +1,12 @@
|
|||
[package]
|
||||
name = "libimagentrylist"
|
||||
version = "0.1.0"
|
||||
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
|
||||
|
||||
[dependencies]
|
||||
log = "0.3.5"
|
||||
toml = "0.1.25"
|
||||
|
||||
[dependencies.libimagstore]
|
||||
path = "../libimagstore"
|
||||
|
12
libimagentrylist/README.md
Normal file
12
libimagentrylist/README.md
Normal file
|
@ -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
|
||||
|
83
libimagentrylist/src/error.rs
Normal file
83
libimagentrylist/src/error.rs
Normal file
|
@ -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<Box<Error>>,
|
||||
}
|
||||
|
||||
impl ListError {
|
||||
|
||||
/**
|
||||
* Build a new ListError from an ListErrorKind, optionally with cause
|
||||
*/
|
||||
pub fn new(errtype: ListErrorKind, cause: Option<Box<Error>>) -> 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)
|
||||
}
|
||||
|
||||
}
|
||||
|
10
libimagentrylist/src/lib.rs
Normal file
10
libimagentrylist/src/lib.rs
Normal file
|
@ -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;
|
||||
|
10
libimagentrylist/src/lister.rs
Normal file
10
libimagentrylist/src/lister.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
use libimagstore::store::FileLockEntry;
|
||||
|
||||
use result::Result;
|
||||
|
||||
pub trait Lister {
|
||||
|
||||
fn list<'a, I: Iterator<Item = FileLockEntry<'a>>>(&self, entries: I) -> Result<()>;
|
||||
|
||||
}
|
||||
|
40
libimagentrylist/src/listers/line.rs
Normal file
40
libimagentrylist/src/listers/line.rs
Normal 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 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<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))))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
2
libimagentrylist/src/listers/mod.rs
Normal file
2
libimagentrylist/src/listers/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
pub mod line;
|
||||
pub mod path;
|
54
libimagentrylist/src/listers/path.rs
Normal file
54
libimagentrylist/src/listers/path.rs
Normal file
|
@ -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<Item = FileLockEntry<'a>>>(&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)))
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
6
libimagentrylist/src/result.rs
Normal file
6
libimagentrylist/src/result.rs
Normal file
|
@ -0,0 +1,6 @@
|
|||
use std::result::Result as RResult;
|
||||
|
||||
use error::ListError;
|
||||
|
||||
pub type Result<T> = RResult<T, ListError>;
|
||||
|
Loading…
Reference in a new issue