diff --git a/libimagentryview/Cargo.toml b/libimagentryview/Cargo.toml new file mode 100644 index 00000000..e9711be0 --- /dev/null +++ b/libimagentryview/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "libimagentryview" +version = "0.1.0" +authors = ["Matthias Beyer "] + +[dependencies] + +[dependencies.libimagstore] +path = "../libimagstore" + diff --git a/libimagentryview/src/builtin/mod.rs b/libimagentryview/src/builtin/mod.rs new file mode 100644 index 00000000..6c83de88 --- /dev/null +++ b/libimagentryview/src/builtin/mod.rs @@ -0,0 +1 @@ +pub mod plain; diff --git a/libimagentryview/src/builtin/plain.rs b/libimagentryview/src/builtin/plain.rs new file mode 100644 index 00000000..02ff5fc8 --- /dev/null +++ b/libimagentryview/src/builtin/plain.rs @@ -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(()) + } + +} diff --git a/libimagentryview/src/error.rs b/libimagentryview/src/error.rs new file mode 100644 index 00000000..108529cc --- /dev/null +++ b/libimagentryview/src/error.rs @@ -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>, +} + +impl ViewError { + + /** + * Build a new ViewError from an ViewErrorKind, optionally with cause + */ + pub fn new(errtype: ViewErrorKind, cause: Option>) + -> 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) + } + +} + diff --git a/libimagentryview/src/lib.rs b/libimagentryview/src/lib.rs new file mode 100644 index 00000000..73833b7d --- /dev/null +++ b/libimagentryview/src/lib.rs @@ -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; + diff --git a/libimagentryview/src/result.rs b/libimagentryview/src/result.rs new file mode 100644 index 00000000..4d5786fa --- /dev/null +++ b/libimagentryview/src/result.rs @@ -0,0 +1,5 @@ +use std::result::Result as RResult; + +use error::ViewError; + +pub type Result = RResult; diff --git a/libimagentryview/src/viewer.rs b/libimagentryview/src/viewer.rs new file mode 100644 index 00000000..72ffbd7d --- /dev/null +++ b/libimagentryview/src/viewer.rs @@ -0,0 +1,17 @@ +use libimagstore::store::Entry; + +use result::Result; + +pub trait Viewer { + + fn view_entry(&self, e: &Entry) -> Result<()>; + + fn view_entries>(&self, entries: I) -> Result<()> { + for entry in entries { + if let Err(e) = self.view_entry(&entry) { + return Err(e); + } + } + Ok(()) + } +}