diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2017-08-29 10:52:10 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2017-09-01 11:04:06 +0200 |
commit | 95b7da1ed21e4af5142b84bb49768a9c40b8dd68 (patch) | |
tree | bd46b07fa517019548a9c2cfcde1a8985980bb86 /lib/domain/libimagdiary | |
parent | 9c69645b69052fd1170484e390cbea363f784f9a (diff) |
Impl Entry (now DiaryEntry) as trait
Diffstat (limited to 'lib/domain/libimagdiary')
-rw-r--r-- | lib/domain/libimagdiary/src/diary.rs | 36 | ||||
-rw-r--r-- | lib/domain/libimagdiary/src/entry.rs | 63 | ||||
-rw-r--r-- | lib/domain/libimagdiary/src/iter.rs | 8 | ||||
-rw-r--r-- | lib/domain/libimagdiary/src/viewer.rs | 5 |
4 files changed, 16 insertions, 96 deletions
diff --git a/lib/domain/libimagdiary/src/diary.rs b/lib/domain/libimagdiary/src/diary.rs index be768fed..cf738e33 100644 --- a/lib/domain/libimagdiary/src/diary.rs +++ b/lib/domain/libimagdiary/src/diary.rs @@ -21,7 +21,6 @@ use std::cmp::Ordering; use libimagstore::store::FileLockEntry; use libimagstore::store::Store; -use libimagstore::storeid::IntoStoreId; use libimagerror::trace::trace_error; use chrono::offset::Local; @@ -30,7 +29,7 @@ use itertools::Itertools; use chrono::naive::NaiveDateTime; use chrono::Timelike; -use entry::Entry; +use entry::DiaryEntry; use diaryid::DiaryId; use error::DiaryErrorKind as DEK; use error::MapErrInto; @@ -40,15 +39,6 @@ use iter::DiaryNameIterator; 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 fn new_entry_today(&self, diary_name: &str) -> Result<FileLockEntry>; @@ -67,31 +57,13 @@ trait Diary { 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 fn new_entry_today(&self, diary_name: &str) -> Result<FileLockEntry> { let dt = Local::now(); let ndt = dt.naive_local(); 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 @@ -105,7 +77,7 @@ impl Diary for Store { ndt.minute(), ndt.second()); - Diary::retrieve(self, id) + self.retrieve(id).map_err_into(DEK::StoreReadError) } // Get an iterator for iterating over all entries diff --git a/lib/domain/libimagdiary/src/entry.rs b/lib/domain/libimagdiary/src/entry.rs index d0fb2e53..2da96199 100644 --- a/lib/domain/libimagdiary/src/entry.rs +++ b/lib/domain/libimagdiary/src/entry.rs @@ -17,75 +17,24 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // -use std::ops::Deref; -use std::ops::DerefMut; - -use libimagstore::store::FileLockEntry; -use libimagentryedit::edit::Edit; -use libimagentryedit::result::Result as EditResult; -use libimagrt::runtime::Runtime; +use libimagstore::store::Entry; use diaryid::DiaryId; use diaryid::FromStoreId; use result::Result; -#[derive(Debug)] -pub struct Entry<'a>(FileLockEntry<'a>); - -impl<'a> Deref for Entry<'a> { - type Target = FileLockEntry<'a>; - - fn deref(&self) -> &FileLockEntry<'a> { - &self.0 - } - -} - -impl<'a> DerefMut for Entry<'a> { - - fn deref_mut(&mut self) -> &mut FileLockEntry<'a> { - &mut self.0 - } - +pub trait DiaryEntry { + fn diary_id(&self) -> Result<DiaryId>; } -impl<'a> Entry<'a> { - - pub fn new(fle: FileLockEntry<'a>) -> Entry<'a> { - Entry(fle) - } +impl DiaryEntry for 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 - pub fn diary_id(&self) -> Result<DiaryId> { - DiaryId::from_storeid(&self.0.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) + fn diary_id(&self) -> Result<DiaryId> { + DiaryId::from_storeid(&self.get_location().clone()) } } -impl<'a> Edit for Entry<'a> { - - fn edit_content(&mut self, rt: &Runtime) -> EditResult<()> { - self.0.edit_content(rt) - } - -} - - diff --git a/lib/domain/libimagdiary/src/iter.rs b/lib/domain/libimagdiary/src/iter.rs index 2ed40ece..32fe407c 100644 --- a/lib/domain/libimagdiary/src/iter.rs +++ b/lib/domain/libimagdiary/src/iter.rs @@ -21,14 +21,13 @@ use std::fmt::{Debug, Formatter, Error as FmtError}; use std::result::Result as RResult; use libimagstore::store::Store; +use libimagstore::store::FileLockEntry; use libimagstore::storeid::StoreIdIterator; use libimagerror::trace::trace_error; use libimagerror::into::IntoError; use diaryid::DiaryId; use diaryid::FromStoreId; -use is_in_diary::IsInDiary; -use entry::Entry as DiaryEntry; use error::DiaryError as DE; use error::DiaryErrorKind as DEK; use error::MapErrInto; @@ -90,9 +89,9 @@ impl<'a> 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 { let next = match self.iter.next() { Some(s) => s, @@ -121,7 +120,6 @@ impl<'a> Iterator for DiaryEntryIterator<'a> { return Some(self .store .retrieve(next) - .map(|fle| DiaryEntry::new(fle)) .map_err(|e| DE::new(DEK::StoreReadError, Some(Box::new(e)))) ); } diff --git a/lib/domain/libimagdiary/src/viewer.rs b/lib/domain/libimagdiary/src/viewer.rs index 38c6e179..c0c03fc6 100644 --- a/lib/domain/libimagdiary/src/viewer.rs +++ b/lib/domain/libimagdiary/src/viewer.rs @@ -19,11 +19,12 @@ //! A diary viewer built on libimagentryview. -use entry::Entry; +use entry::DiaryEntry; use error::DiaryErrorKind as DEK; use error::MapErrInto; use result::Result; +use libimagstore::store::FileLockEntry; use libimagentryview::viewer::Viewer; use libimagentryview::builtin::plain::PlainViewer; 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 /// 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 { match entry.diary_id() { Ok(id) => println!("{} :\n", id), |