From f98afd4b01e97467b74fa68f65bbd92ce471bbe6 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 21 Sep 2016 16:49:12 +0200 Subject: [PATCH 1/2] Move dependencies --- imag-diary/Cargo.toml | 3 --- imag-diary/src/main.rs | 1 - libimagdiary/Cargo.toml | 3 +++ libimagdiary/src/lib.rs | 1 + 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/imag-diary/Cargo.toml b/imag-diary/Cargo.toml index 2d0df647..618b16e6 100644 --- a/imag-diary/Cargo.toml +++ b/imag-diary/Cargo.toml @@ -21,9 +21,6 @@ path = "../libimagentryedit" [dependencies.libimagentrylist] path = "../libimagentrylist" -[dependencies.libimagentryview] -path = "../libimagentryview" - [dependencies.libimagerror] path = "../libimagerror" diff --git a/imag-diary/src/main.rs b/imag-diary/src/main.rs index 7a34f1ef..42b02fe0 100644 --- a/imag-diary/src/main.rs +++ b/imag-diary/src/main.rs @@ -21,7 +21,6 @@ extern crate chrono; extern crate libimagdiary; extern crate libimagentryedit; extern crate libimagentrylist; -extern crate libimagentryview; extern crate libimaginteraction; extern crate libimagrt; extern crate libimagstore; diff --git a/libimagdiary/Cargo.toml b/libimagdiary/Cargo.toml index 5ac1b268..3f921ade 100644 --- a/libimagdiary/Cargo.toml +++ b/libimagdiary/Cargo.toml @@ -27,3 +27,6 @@ path = "../libimagrt" [dependencies.libimagentryedit] path = "../libimagentryedit" +[dependencies.libimagentryview] +path = "../libimagentryview" + diff --git a/libimagdiary/src/lib.rs b/libimagdiary/src/lib.rs index 5af7eb50..b668a48d 100644 --- a/libimagdiary/src/lib.rs +++ b/libimagdiary/src/lib.rs @@ -26,6 +26,7 @@ extern crate itertools; #[macro_use] extern crate libimagutil; #[macro_use] extern crate libimagerror; extern crate libimagentryedit; +extern crate libimagentryview; extern crate libimagrt; module_entry_path_mod!("diary"); From 8e331f1f083e912f689e5ebd8a8c39b8e5d1a895 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 21 Sep 2016 16:45:23 +0200 Subject: [PATCH 2/2] Move viewer implementation for imag-diary to libimagdiary --- imag-diary/src/view.rs | 27 +++++++---------------- libimagdiary/src/error.rs | 3 ++- libimagdiary/src/lib.rs | 2 ++ libimagdiary/src/viewer.rs | 45 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 20 deletions(-) create mode 100644 libimagdiary/src/viewer.rs diff --git a/imag-diary/src/view.rs b/imag-diary/src/view.rs index 742bc262..444c7131 100644 --- a/imag-diary/src/view.rs +++ b/imag-diary/src/view.rs @@ -1,30 +1,19 @@ use libimagdiary::diary::Diary; -use libimagentryview::viewer::Viewer; -use libimagentryview::builtin::plain::PlainViewer; +use libimagdiary::viewer::DiaryViewer as DV; use libimagrt::runtime::Runtime; -use libimagerror::trace::trace_error; +use libimagerror::trace::MapErrTrace; use libimagutil::warn_exit::warn_exit; use util::get_diary_name; pub fn view(rt: &Runtime) { let diaryname = get_diary_name(rt).unwrap_or_else(|| warn_exit("No diary name", 1)); - let diary = Diary::open(rt.store(), &diaryname[..]); - let show_header = rt.cli().subcommand_matches("view").unwrap().is_present("show-header"); + let diary = Diary::open(rt.store(), &diaryname[..]); + let hdr = rt.cli().subcommand_matches("view").unwrap().is_present("show-header"); - match diary.entries() { - Ok(entries) => { - let pv = PlainViewer::new(show_header); - for entry in entries.into_iter().filter_map(Result::ok) { - let id = entry.diary_id(); - println!("{} :\n", id); - if let Err(e) = pv.view_entry(&entry) { - trace_error(&e); - }; - println!("\n---\n"); - } - }, - Err(e) => trace_error(&e), - } + diary.entries() + .and_then(|entries| DV::new(hdr).view_entries(entries.into_iter().filter_map(Result::ok))) + .map_err_trace() + .ok(); } diff --git a/libimagdiary/src/error.rs b/libimagdiary/src/error.rs index 5cb37915..9ad53604 100644 --- a/libimagdiary/src/error.rs +++ b/libimagdiary/src/error.rs @@ -7,7 +7,8 @@ generate_error_module!( DiaryEditError => "Cannot edit diary entry", PathConversionError => "Error while converting paths internally", EntryNotInDiary => "Entry not in Diary", - IOError => "IO Error" + IOError => "IO Error", + ViewError => "Error viewing diary entry" ); ); diff --git a/libimagdiary/src/lib.rs b/libimagdiary/src/lib.rs index b668a48d..1a808283 100644 --- a/libimagdiary/src/lib.rs +++ b/libimagdiary/src/lib.rs @@ -39,3 +39,5 @@ pub mod is_in_diary; pub mod entry; pub mod iter; pub mod result; +pub mod viewer; + diff --git a/libimagdiary/src/viewer.rs b/libimagdiary/src/viewer.rs new file mode 100644 index 00000000..63c4f030 --- /dev/null +++ b/libimagdiary/src/viewer.rs @@ -0,0 +1,45 @@ +//! A diary viewer built on libimagentryview. + +use entry::Entry; +use error::DiaryErrorKind as DEK; +use error::MapErrInto; +use result::Result; + +use libimagentryview::viewer::Viewer; +use libimagentryview::builtin::plain::PlainViewer; + +/// This viewer does _not_ implement libimagentryview::viewer::Viewer because we need to be able to +/// call some diary-type specific functions on the entries passed to this. +/// +/// This type is mainly just written to be constructed-called-deleted in one go: +/// +/// ```ignore +/// DiaryViewer::new(show_header).view_entries(entries); +/// ``` +/// +pub struct DiaryViewer(PlainViewer); + +impl DiaryViewer { + + pub fn new(show_header: bool) -> DiaryViewer { + DiaryViewer(PlainViewer::new(show_header)) + } + + /// View all entries from the iterator, or stop immediately if an error occurs, returning that + /// error. + pub fn view_entries<'a, I: Iterator>>(&self, entries: I) -> Result<()> { + for entry in entries { + let id = entry.diary_id(); + println!("{} :\n", id); + let _ = try!(self.0 + .view_entry(&entry) + .map_err_into(DEK::ViewError) + .map_err_into(DEK::IOError)); + println!("\n---\n"); + } + + Ok(()) + } + +} +