Move viewer implementation for imag-diary to libimagdiary

This commit is contained in:
Matthias Beyer 2016-09-21 16:45:23 +02:00
parent f98afd4b01
commit 8e331f1f08
4 changed files with 57 additions and 20 deletions

View file

@ -1,8 +1,7 @@
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;
@ -10,21 +9,11 @@ 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

@ -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

@ -39,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(())
}
}