From 2d3e8ae47928963dc2aafcb50876c56d3acbcc32 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 18 Apr 2016 19:46:11 +0200 Subject: [PATCH 1/8] Initial import --- libimagentryview/Cargo.toml | 6 ++++++ libimagentryview/src/lib.rs | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 libimagentryview/Cargo.toml create mode 100644 libimagentryview/src/lib.rs diff --git a/libimagentryview/Cargo.toml b/libimagentryview/Cargo.toml new file mode 100644 index 00000000..ccea5843 --- /dev/null +++ b/libimagentryview/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "libimagentryview" +version = "0.1.0" +authors = ["Matthias Beyer "] + +[dependencies] diff --git a/libimagentryview/src/lib.rs b/libimagentryview/src/lib.rs new file mode 100644 index 00000000..cf4da5ad --- /dev/null +++ b/libimagentryview/src/lib.rs @@ -0,0 +1,6 @@ +#[cfg(test)] +mod test { + #[test] + fn it_works() { + } +} From 8661842deca33df977d976f787c286cee27e7e2a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 18 Apr 2016 19:51:51 +0200 Subject: [PATCH 2/8] Add error module --- libimagentryview/src/error.rs | 80 +++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 libimagentryview/src/error.rs 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) + } + +} + From 2a61af2a9ddc294ca880c520f871987bc1881694 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 18 Apr 2016 19:52:02 +0200 Subject: [PATCH 3/8] Add result module --- libimagentryview/src/result.rs | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 libimagentryview/src/result.rs 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; From b388c6684e09bd9b3c92171d9220e825542431af Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 18 Apr 2016 19:52:14 +0200 Subject: [PATCH 4/8] Add Viewer Trait --- libimagentryview/src/viewer.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 libimagentryview/src/viewer.rs 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(()) + } +} From 2f37404cf3aea2b96e7c4247b5aa833ba08ca1f9 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 18 Apr 2016 19:53:08 +0200 Subject: [PATCH 5/8] Add dependencies: libimagstore --- libimagentryview/Cargo.toml | 4 ++++ libimagentryview/src/lib.rs | 8 ++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libimagentryview/Cargo.toml b/libimagentryview/Cargo.toml index ccea5843..e9711be0 100644 --- a/libimagentryview/Cargo.toml +++ b/libimagentryview/Cargo.toml @@ -4,3 +4,7 @@ version = "0.1.0" authors = ["Matthias Beyer "] [dependencies] + +[dependencies.libimagstore] +path = "../libimagstore" + diff --git a/libimagentryview/src/lib.rs b/libimagentryview/src/lib.rs index cf4da5ad..94eb7f41 100644 --- a/libimagentryview/src/lib.rs +++ b/libimagentryview/src/lib.rs @@ -1,6 +1,2 @@ -#[cfg(test)] -mod test { - #[test] - fn it_works() { - } -} +extern crate libimagstore; + From e1f033c0322dfe5b4e07b076db2f9331382e80a8 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 18 Apr 2016 19:53:13 +0200 Subject: [PATCH 6/8] Add modules --- libimagentryview/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libimagentryview/src/lib.rs b/libimagentryview/src/lib.rs index 94eb7f41..f1dba6fe 100644 --- a/libimagentryview/src/lib.rs +++ b/libimagentryview/src/lib.rs @@ -1,2 +1,6 @@ extern crate libimagstore; +pub mod error; +pub mod result; +pub mod viewer; + From 3ab8c9038c7df0337ebf18d8609a042ebba09c58 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 18 Apr 2016 19:58:47 +0200 Subject: [PATCH 7/8] Enable lints --- libimagentryview/src/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/libimagentryview/src/lib.rs b/libimagentryview/src/lib.rs index f1dba6fe..35d15053 100644 --- a/libimagentryview/src/lib.rs +++ b/libimagentryview/src/lib.rs @@ -1,3 +1,19 @@ +#![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; From c95647261603007bd43d2f14554af34bb035acf5 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 18 Apr 2016 19:59:05 +0200 Subject: [PATCH 8/8] Add builtin module with PlainViewer --- libimagentryview/src/builtin/mod.rs | 1 + libimagentryview/src/builtin/plain.rs | 30 +++++++++++++++++++++++++++ libimagentryview/src/lib.rs | 1 + 3 files changed, 32 insertions(+) create mode 100644 libimagentryview/src/builtin/mod.rs create mode 100644 libimagentryview/src/builtin/plain.rs 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/lib.rs b/libimagentryview/src/lib.rs index 35d15053..73833b7d 100644 --- a/libimagentryview/src/lib.rs +++ b/libimagentryview/src/lib.rs @@ -17,6 +17,7 @@ extern crate libimagstore; pub mod error; +pub mod builtin; pub mod result; pub mod viewer;