diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-04-22 16:20:29 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-06-04 15:34:02 +0200 |
commit | 0363a6a4d41ece736da473e60c33e458b83e197d (patch) | |
tree | dfafed51dd7ba5e1e378cf055dd7d9cabb004fce /libimagdiary | |
parent | 97c710baf89f4513ca902bc0286514b73a46e905 (diff) |
Add Diary type
Diffstat (limited to 'libimagdiary')
-rw-r--r-- | libimagdiary/src/diary.rs | 103 | ||||
-rw-r--r-- | libimagdiary/src/lib.rs | 1 |
2 files changed, 104 insertions, 0 deletions
diff --git a/libimagdiary/src/diary.rs b/libimagdiary/src/diary.rs new file mode 100644 index 00000000..edd58c87 --- /dev/null +++ b/libimagdiary/src/diary.rs @@ -0,0 +1,103 @@ +use std::cmp::Ordering; + +use libimagstore::store::Store; +use libimagstore::storeid::IntoStoreId; + +use chrono::offset::local::Local; +use chrono::Datelike; +use itertools::Itertools; + +use entry::Entry; +use diaryid::DiaryId; +use error::DiaryError as DE; +use error::DiaryErrorKind as DEK; +use result::Result; +use iter::DiaryEntryIterator; +use is_in_diary::IsInDiary; + +#[derive(Debug)] +pub struct Diary<'a> { + store: &'a Store, + name: &'a str, +} + +impl<'a> Diary<'a> { + + pub fn open(store: &'a Store, name: &'a str) -> Diary<'a> { + Diary { + store: store, + name: name, + } + } + + // create or get a new entry for today + pub fn new_entry_today(&self) -> Result<Entry> { + let dt = Local::now(); + let ndt = dt.naive_local(); + + // Currenty we only have support for per-day entries + let id = DiaryId::new(String::from(self.name), ndt.year(), ndt.month(), ndt.day(), 0, 0); + + self.retrieve(id) + } + + pub fn retrieve(&self, id: DiaryId) -> Result<Entry> { + self.store + .retrieve(id.into_storeid()) + .map(|fle| Entry::new(fle)) + .map_err(|e| DE::new(DEK::StoreWriteError, Some(Box::new(e)))) + } + + // Get an iterator for iterating over all entries + pub fn entries(&self) -> Result<DiaryEntryIterator<'a>> { + self.store + .retrieve_for_module("diary") + .map(|iter| DiaryEntryIterator::new(self.name, self.store, iter)) + .map_err(|e| DE::new(DEK::StoreReadError, Some(Box::new(e)))) + } + + pub fn delete_entry(&self, entry: Entry) -> Result<()> { + if !entry.is_in_diary(self.name) { + return Err(DE::new(DEK::EntryNotInDiary, None)); + } + let id = entry.get_location().clone(); + drop(entry); + + self.store.delete(id) + .map_err(|e| DE::new(DEK::StoreWriteError, Some(Box::new(e)))) + } + + pub fn get_youngest_entry(&self) -> Option<Result<Entry>> { + match self.entries() { + Err(e) => Some(Err(e)), + Ok(entries) => { + entries.sorted_by(|a, b| { + match (a, b) { + (&Ok(ref a), &Ok(ref b)) => { + let a : NaiveDateTime = a.diary_id().into(); + let b : NaiveDateTime = b.diary_id().into(); + + a.cmp(&b) + }, + + (&Ok(_), &Err(ref e)) => { + trace_error(e); + Ordering::Less + }, + (&Err(ref e), &Ok(_)) => { + trace_error(e); + Ordering::Greater + }, + (&Err(ref e1), &Err(ref e2)) => { + trace_error(e1); + trace_error(e2); + Ordering::Equal + }, + } + }).into_iter().next() + } + } + } + +} + diff --git a/libimagdiary/src/lib.rs b/libimagdiary/src/lib.rs index 25d0ec04..5f4567e7 100644 --- a/libimagdiary/src/lib.rs +++ b/libimagdiary/src/lib.rs @@ -15,6 +15,7 @@ module_entry_path_mod!("diary", "0.1.0"); pub mod error; pub mod diaryid; +pub mod diary; pub mod is_in_diary; pub mod entry; pub mod iter; |