Merge pull request #762 from matthiasbeyer/imag-diary/move-viewer-impl

imag-diary: move viewer impl
This commit is contained in:
Matthias Beyer 2016-09-22 08:09:06 +02:00 committed by GitHub
commit 54120852ce
7 changed files with 61 additions and 24 deletions

View file

@ -21,9 +21,6 @@ path = "../libimagentryedit"
[dependencies.libimagentrylist] [dependencies.libimagentrylist]
path = "../libimagentrylist" path = "../libimagentrylist"
[dependencies.libimagentryview]
path = "../libimagentryview"
[dependencies.libimagerror] [dependencies.libimagerror]
path = "../libimagerror" path = "../libimagerror"

View file

@ -21,7 +21,6 @@ extern crate chrono;
extern crate libimagdiary; extern crate libimagdiary;
extern crate libimagentryedit; extern crate libimagentryedit;
extern crate libimagentrylist; extern crate libimagentrylist;
extern crate libimagentryview;
extern crate libimaginteraction; extern crate libimaginteraction;
extern crate libimagrt; extern crate libimagrt;
extern crate libimagstore; extern crate libimagstore;

View file

@ -1,30 +1,19 @@
use libimagdiary::diary::Diary; use libimagdiary::diary::Diary;
use libimagentryview::viewer::Viewer; use libimagdiary::viewer::DiaryViewer as DV;
use libimagentryview::builtin::plain::PlainViewer;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagerror::trace::trace_error; use libimagerror::trace::MapErrTrace;
use libimagutil::warn_exit::warn_exit; use libimagutil::warn_exit::warn_exit;
use util::get_diary_name; use util::get_diary_name;
pub fn view(rt: &Runtime) { pub fn view(rt: &Runtime) {
let diaryname = get_diary_name(rt).unwrap_or_else(|| warn_exit("No diary name", 1)); let diaryname = get_diary_name(rt).unwrap_or_else(|| warn_exit("No diary name", 1));
let diary = Diary::open(rt.store(), &diaryname[..]); let diary = Diary::open(rt.store(), &diaryname[..]);
let show_header = rt.cli().subcommand_matches("view").unwrap().is_present("show-header"); let hdr = rt.cli().subcommand_matches("view").unwrap().is_present("show-header");
match diary.entries() { diary.entries()
Ok(entries) => { .and_then(|entries| DV::new(hdr).view_entries(entries.into_iter().filter_map(Result::ok)))
let pv = PlainViewer::new(show_header); .map_err_trace()
for entry in entries.into_iter().filter_map(Result::ok) { .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),
}
} }

View file

@ -27,3 +27,6 @@ path = "../libimagrt"
[dependencies.libimagentryedit] [dependencies.libimagentryedit]
path = "../libimagentryedit" path = "../libimagentryedit"
[dependencies.libimagentryview]
path = "../libimagentryview"

View file

@ -7,7 +7,8 @@ generate_error_module!(
DiaryEditError => "Cannot edit diary entry", DiaryEditError => "Cannot edit diary entry",
PathConversionError => "Error while converting paths internally", PathConversionError => "Error while converting paths internally",
EntryNotInDiary => "Entry not in Diary", EntryNotInDiary => "Entry not in Diary",
IOError => "IO Error" IOError => "IO Error",
ViewError => "Error viewing diary entry"
); );
); );

View file

@ -26,6 +26,7 @@ extern crate itertools;
#[macro_use] extern crate libimagutil; #[macro_use] extern crate libimagutil;
#[macro_use] extern crate libimagerror; #[macro_use] extern crate libimagerror;
extern crate libimagentryedit; extern crate libimagentryedit;
extern crate libimagentryview;
extern crate libimagrt; extern crate libimagrt;
module_entry_path_mod!("diary"); module_entry_path_mod!("diary");
@ -38,3 +39,5 @@ pub mod is_in_diary;
pub mod entry; pub mod entry;
pub mod iter; pub mod iter;
pub mod result; pub mod result;
pub mod viewer;

View file

@ -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<Item = Entry<'a>>>(&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(())
}
}