Impl Entry (now DiaryEntry) as trait

This commit is contained in:
Matthias Beyer 2017-08-29 10:52:10 +02:00
parent 9c69645b69
commit 95b7da1ed2
4 changed files with 16 additions and 96 deletions

View file

@ -21,7 +21,6 @@ use std::cmp::Ordering;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::store::Store; use libimagstore::store::Store;
use libimagstore::storeid::IntoStoreId;
use libimagerror::trace::trace_error; use libimagerror::trace::trace_error;
use chrono::offset::Local; use chrono::offset::Local;
@ -30,7 +29,7 @@ use itertools::Itertools;
use chrono::naive::NaiveDateTime; use chrono::naive::NaiveDateTime;
use chrono::Timelike; use chrono::Timelike;
use entry::Entry; use entry::DiaryEntry;
use diaryid::DiaryId; use diaryid::DiaryId;
use error::DiaryErrorKind as DEK; use error::DiaryErrorKind as DEK;
use error::MapErrInto; use error::MapErrInto;
@ -40,15 +39,6 @@ use iter::DiaryNameIterator;
trait Diary { trait Diary {
/// Wrapper around Store::get for DiaryId
fn get(&self, id: DiaryId) -> Result<Option<FileLockEntry>>;
/// Wrapper around Store::retrieve for DiaryId
fn retrieve(&self, id: DiaryId) -> Result<FileLockEntry>;
/// Wrapper around Store::delete for DiaryId
fn delete(&self, entry: Entry) -> Result<()>;
// create or get a new entry for today // create or get a new entry for today
fn new_entry_today(&self, diary_name: &str) -> Result<FileLockEntry>; fn new_entry_today(&self, diary_name: &str) -> Result<FileLockEntry>;
@ -67,31 +57,13 @@ trait Diary {
impl Diary for Store { impl Diary for Store {
/// Wrapper around Store::get for DiaryId
fn get(&self, id: DiaryId) -> Result<Option<FileLockEntry>> {
id.into_storeid().and_then(|id| self.get(id)).map_err_into(DEK::StoreWriteError)
}
/// Wrapper around Store::retrieve for DiaryId
fn retrieve(&self, id: DiaryId) -> Result<FileLockEntry> {
id.into_storeid().and_then(|id| self.retrieve(id)).map_err_into(DEK::StoreWriteError)
}
/// Wrapper around Store::delete for DiaryId
fn delete(&self, entry: Entry) -> Result<()> {
let id = entry.get_location().clone();
drop(entry);
self.delete(id).map_err_into(DEK::StoreWriteError)
}
// create or get a new entry for today // create or get a new entry for today
fn new_entry_today(&self, diary_name: &str) -> Result<FileLockEntry> { fn new_entry_today(&self, diary_name: &str) -> Result<FileLockEntry> {
let dt = Local::now(); let dt = Local::now();
let ndt = dt.naive_local(); let ndt = dt.naive_local();
let id = DiaryId::new(String::from(diary_name), ndt.year(), ndt.month(), ndt.day(), 0, 0); let id = DiaryId::new(String::from(diary_name), ndt.year(), ndt.month(), ndt.day(), 0, 0);
Diary::retrieve(self, id)
self.retrieve(id).map_err_into(DEK::StoreReadError)
} }
// create or get a new entry for today // create or get a new entry for today
@ -105,7 +77,7 @@ impl Diary for Store {
ndt.minute(), ndt.minute(),
ndt.second()); ndt.second());
Diary::retrieve(self, id) self.retrieve(id).map_err_into(DEK::StoreReadError)
} }
// Get an iterator for iterating over all entries // Get an iterator for iterating over all entries

View file

@ -17,75 +17,24 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use std::ops::Deref; use libimagstore::store::Entry;
use std::ops::DerefMut;
use libimagstore::store::FileLockEntry;
use libimagentryedit::edit::Edit;
use libimagentryedit::result::Result as EditResult;
use libimagrt::runtime::Runtime;
use diaryid::DiaryId; use diaryid::DiaryId;
use diaryid::FromStoreId; use diaryid::FromStoreId;
use result::Result; use result::Result;
#[derive(Debug)] pub trait DiaryEntry {
pub struct Entry<'a>(FileLockEntry<'a>); fn diary_id(&self) -> Result<DiaryId>;
impl<'a> Deref for Entry<'a> {
type Target = FileLockEntry<'a>;
fn deref(&self) -> &FileLockEntry<'a> {
&self.0
} }
} impl DiaryEntry for Entry {
impl<'a> DerefMut for Entry<'a> {
fn deref_mut(&mut self) -> &mut FileLockEntry<'a> {
&mut self.0
}
}
impl<'a> Entry<'a> {
pub fn new(fle: FileLockEntry<'a>) -> Entry<'a> {
Entry(fle)
}
/// Get the diary id for this entry. /// Get the diary id for this entry.
/// ///
/// TODO: calls Option::unwrap() as it assumes that an existing Entry has an ID that is parsable /// TODO: calls Option::unwrap() as it assumes that an existing Entry has an ID that is parsable
pub fn diary_id(&self) -> Result<DiaryId> { fn diary_id(&self) -> Result<DiaryId> {
DiaryId::from_storeid(&self.0.get_location().clone()) DiaryId::from_storeid(&self.get_location().clone())
} }
} }
impl<'a> Into<FileLockEntry<'a>> for Entry<'a> {
fn into(self) -> FileLockEntry<'a> {
self.0
}
}
impl<'a> From<FileLockEntry<'a>> for Entry<'a> {
fn from(fle: FileLockEntry<'a>) -> Entry<'a> {
Entry::new(fle)
}
}
impl<'a> Edit for Entry<'a> {
fn edit_content(&mut self, rt: &Runtime) -> EditResult<()> {
self.0.edit_content(rt)
}
}

View file

@ -21,14 +21,13 @@ use std::fmt::{Debug, Formatter, Error as FmtError};
use std::result::Result as RResult; use std::result::Result as RResult;
use libimagstore::store::Store; use libimagstore::store::Store;
use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreIdIterator; use libimagstore::storeid::StoreIdIterator;
use libimagerror::trace::trace_error; use libimagerror::trace::trace_error;
use libimagerror::into::IntoError; use libimagerror::into::IntoError;
use diaryid::DiaryId; use diaryid::DiaryId;
use diaryid::FromStoreId; use diaryid::FromStoreId;
use is_in_diary::IsInDiary;
use entry::Entry as DiaryEntry;
use error::DiaryError as DE; use error::DiaryError as DE;
use error::DiaryErrorKind as DEK; use error::DiaryErrorKind as DEK;
use error::MapErrInto; use error::MapErrInto;
@ -90,9 +89,9 @@ impl<'a> DiaryEntryIterator<'a> {
} }
impl<'a> Iterator for DiaryEntryIterator<'a> { impl<'a> Iterator for DiaryEntryIterator<'a> {
type Item = Result<DiaryEntry<'a>>; type Item = Result<FileLockEntry<'a>>;
fn next(&mut self) -> Option<Result<DiaryEntry<'a>>> { fn next(&mut self) -> Option<Self::Item> {
loop { loop {
let next = match self.iter.next() { let next = match self.iter.next() {
Some(s) => s, Some(s) => s,
@ -121,7 +120,6 @@ impl<'a> Iterator for DiaryEntryIterator<'a> {
return Some(self return Some(self
.store .store
.retrieve(next) .retrieve(next)
.map(|fle| DiaryEntry::new(fle))
.map_err(|e| DE::new(DEK::StoreReadError, Some(Box::new(e)))) .map_err(|e| DE::new(DEK::StoreReadError, Some(Box::new(e))))
); );
} }

View file

@ -19,11 +19,12 @@
//! A diary viewer built on libimagentryview. //! A diary viewer built on libimagentryview.
use entry::Entry; use entry::DiaryEntry;
use error::DiaryErrorKind as DEK; use error::DiaryErrorKind as DEK;
use error::MapErrInto; use error::MapErrInto;
use result::Result; use result::Result;
use libimagstore::store::FileLockEntry;
use libimagentryview::viewer::Viewer; use libimagentryview::viewer::Viewer;
use libimagentryview::builtin::plain::PlainViewer; use libimagentryview::builtin::plain::PlainViewer;
use libimagerror::trace::trace_error; use libimagerror::trace::trace_error;
@ -47,7 +48,7 @@ impl DiaryViewer {
/// View all entries from the iterator, or stop immediately if an error occurs, returning that /// View all entries from the iterator, or stop immediately if an error occurs, returning that
/// error. /// error.
pub fn view_entries<'a, I: Iterator<Item = Entry<'a>>>(&self, entries: I) -> Result<()> { pub fn view_entries<'a, I: Iterator<Item = FileLockEntry<'a>>>(&self, entries: I) -> Result<()> {
for entry in entries { for entry in entries {
match entry.diary_id() { match entry.diary_id() {
Ok(id) => println!("{} :\n", id), Ok(id) => println!("{} :\n", id),