Impl Entry (now DiaryEntry) as trait
This commit is contained in:
parent
9c69645b69
commit
95b7da1ed2
4 changed files with 16 additions and 96 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
pub trait DiaryEntry {
|
||||
fn diary_id(&self) -> Result<DiaryId>;
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
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())
|
||||
fn diary_id(&self) -> Result<DiaryId> {
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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))))
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
|
|
Loading…
Reference in a new issue