Merge pull request #372 from matthiasbeyer/libimagentryview/init
Libimagentryview/init
This commit is contained in:
commit
3f2114bf1f
7 changed files with 166 additions and 0 deletions
10
libimagentryview/Cargo.toml
Normal file
10
libimagentryview/Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
[package]
|
||||||
|
name = "libimagentryview"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
|
||||||
|
[dependencies.libimagstore]
|
||||||
|
path = "../libimagstore"
|
||||||
|
|
1
libimagentryview/src/builtin/mod.rs
Normal file
1
libimagentryview/src/builtin/mod.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod plain;
|
30
libimagentryview/src/builtin/plain.rs
Normal file
30
libimagentryview/src/builtin/plain.rs
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
use libimagstore::store::Entry;
|
||||||
|
|
||||||
|
use viewer::Viewer;
|
||||||
|
use result::Result;
|
||||||
|
|
||||||
|
pub struct PlainViewer {
|
||||||
|
show_header: bool
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PlainViewer {
|
||||||
|
|
||||||
|
pub fn new(show_header: bool) -> PlainViewer {
|
||||||
|
PlainViewer {
|
||||||
|
show_header: show_header,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Viewer for PlainViewer {
|
||||||
|
|
||||||
|
fn view_entry(&self, e: &Entry) -> Result<()> {
|
||||||
|
if self.show_header {
|
||||||
|
println!("{}", e.get_header().header());
|
||||||
|
}
|
||||||
|
println!("{}", e.get_content());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
80
libimagentryview/src/error.rs
Normal file
80
libimagentryview/src/error.rs
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
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 ViewErrorKind {
|
||||||
|
}
|
||||||
|
|
||||||
|
fn counter_error_type_as_str(e: &ViewErrorKind) -> &'static str {
|
||||||
|
match e {
|
||||||
|
_ => "",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for ViewErrorKind {
|
||||||
|
|
||||||
|
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 ViewError {
|
||||||
|
err_type: ViewErrorKind,
|
||||||
|
cause: Option<Box<Error>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ViewError {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a new ViewError from an ViewErrorKind, optionally with cause
|
||||||
|
*/
|
||||||
|
pub fn new(errtype: ViewErrorKind, cause: Option<Box<Error>>)
|
||||||
|
-> ViewError
|
||||||
|
{
|
||||||
|
ViewError {
|
||||||
|
err_type: errtype,
|
||||||
|
cause: cause,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the error type of this ViewError
|
||||||
|
*/
|
||||||
|
pub fn err_type(&self) -> ViewErrorKind {
|
||||||
|
self.err_type.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for ViewError {
|
||||||
|
|
||||||
|
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
|
||||||
|
try!(write!(fmt, "[{}]", counter_error_type_as_str(&self.err_type.clone())));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error for ViewError {
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
23
libimagentryview/src/lib.rs
Normal file
23
libimagentryview/src/lib.rs
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#![deny(
|
||||||
|
dead_code,
|
||||||
|
non_camel_case_types,
|
||||||
|
non_snake_case,
|
||||||
|
path_statements,
|
||||||
|
trivial_numeric_casts,
|
||||||
|
unstable_features,
|
||||||
|
unused_allocation,
|
||||||
|
unused_import_braces,
|
||||||
|
unused_imports,
|
||||||
|
unused_must_use,
|
||||||
|
unused_mut,
|
||||||
|
unused_qualifications,
|
||||||
|
while_true,
|
||||||
|
)]
|
||||||
|
|
||||||
|
extern crate libimagstore;
|
||||||
|
|
||||||
|
pub mod error;
|
||||||
|
pub mod builtin;
|
||||||
|
pub mod result;
|
||||||
|
pub mod viewer;
|
||||||
|
|
5
libimagentryview/src/result.rs
Normal file
5
libimagentryview/src/result.rs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
use std::result::Result as RResult;
|
||||||
|
|
||||||
|
use error::ViewError;
|
||||||
|
|
||||||
|
pub type Result<T> = RResult<T, ViewError>;
|
17
libimagentryview/src/viewer.rs
Normal file
17
libimagentryview/src/viewer.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
use libimagstore::store::Entry;
|
||||||
|
|
||||||
|
use result::Result;
|
||||||
|
|
||||||
|
pub trait Viewer {
|
||||||
|
|
||||||
|
fn view_entry(&self, e: &Entry) -> Result<()>;
|
||||||
|
|
||||||
|
fn view_entries<I: Iterator<Item = Entry>>(&self, entries: I) -> Result<()> {
|
||||||
|
for entry in entries {
|
||||||
|
if let Err(e) = self.view_entry(&entry) {
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue