Merge pull request #372 from matthiasbeyer/libimagentryview/init

Libimagentryview/init
This commit is contained in:
Matthias Beyer 2016-04-20 17:35:04 +02:00
commit 3f2114bf1f
7 changed files with 166 additions and 0 deletions

View file

@ -0,0 +1,10 @@
[package]
name = "libimagentryview"
version = "0.1.0"
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
[dependencies]
[dependencies.libimagstore]
path = "../libimagstore"

View file

@ -0,0 +1 @@
pub mod plain;

View 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(())
}
}

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

View 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;

View file

@ -0,0 +1,5 @@
use std::result::Result as RResult;
use error::ViewError;
pub type Result<T> = RResult<T, ViewError>;

View 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(())
}
}