imag/lib/domain/libimagdiary/src/diary.rs

135 lines
4.6 KiB
Rust
Raw Normal View History

//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
use std::cmp::Ordering;
2017-08-28 19:58:43 +00:00
use libimagstore::store::FileLockEntry;
use libimagstore::store::Store;
use libimagerror::trace::trace_error;
2017-07-15 18:17:50 +00:00
use chrono::offset::Local;
use chrono::Datelike;
use itertools::Itertools;
2017-07-15 18:17:50 +00:00
use chrono::naive::NaiveDateTime;
2017-08-28 19:58:43 +00:00
use chrono::Timelike;
2017-08-29 08:52:10 +00:00
use entry::DiaryEntry;
use diaryid::DiaryId;
use error::DiaryErrorKind as DEK;
2017-09-03 14:13:08 +00:00
use error::ResultExt;
use error::Result;
use iter::DiaryEntryIterator;
2017-08-28 19:58:43 +00:00
use iter::DiaryNameIterator;
pub trait Diary {
// create or get a new entry for today
2017-08-28 19:58:43 +00:00
fn new_entry_today(&self, diary_name: &str) -> Result<FileLockEntry>;
2017-08-28 19:58:43 +00:00
// create or get a new entry for now
fn new_entry_now(&self, diary_name: &str) -> Result<FileLockEntry>;
2017-08-28 19:58:43 +00:00
// Get an iterator for iterating over all entries of a Diary
fn entries(&self, diary_name: &str) -> Result<DiaryEntryIterator>;
fn get_youngest_entry_id(&self, diary_name: &str) -> Option<Result<DiaryId>>;
/// Get all diary names
fn diary_names(&self) -> Result<DiaryNameIterator>;
}
impl Diary for Store {
// create or get a new entry for today
2017-08-28 19:58:43 +00:00
fn new_entry_today(&self, diary_name: &str) -> Result<FileLockEntry> {
let dt = Local::now();
let ndt = dt.naive_local();
2017-08-28 19:58:43 +00:00
let id = DiaryId::new(String::from(diary_name), ndt.year(), ndt.month(), ndt.day(), 0, 0);
self.retrieve(id).chain_err(|| DEK::StoreReadError)
}
2017-08-28 19:58:43 +00:00
fn new_entry_now(&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(),
ndt.hour(),
ndt.minute());
2017-08-28 19:58:43 +00:00
self.retrieve(id).chain_err(|| DEK::StoreReadError)
}
// Get an iterator for iterating over all entries
2017-08-28 19:58:43 +00:00
fn entries(&self, diary_name: &str) -> Result<DiaryEntryIterator> {
self.retrieve_for_module("diary")
.map(|iter| DiaryEntryIterator::new(self, String::from(diary_name), iter))
2017-09-03 14:13:08 +00:00
.chain_err(|| DEK::StoreReadError)
}
2017-08-28 19:58:43 +00:00
fn get_youngest_entry_id(&self, diary_name: &str) -> Option<Result<DiaryId>> {
match Diary::entries(self, diary_name) {
Err(e) => Some(Err(e)),
Ok(entries) => {
2017-08-28 19:58:43 +00:00
entries
.map(|e| e.and_then(|e| e.diary_id()))
.sorted_by(|a, b| {
match (a, b) {
(&Ok(ref a), &Ok(ref b)) => {
let a : NaiveDateTime = a.clone().into();
let b : NaiveDateTime = b.clone().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()
//.map(|sidres| sidres.map(|sid| DiaryId::from_storeid(&sid)))
.next()
}
}
}
2017-08-28 19:58:43 +00:00
/// Get all diary names
fn diary_names(&self) -> Result<DiaryNameIterator> {
self.retrieve_for_module("diary")
.chain_err(|| DEK::StoreReadError)
2017-08-28 19:58:43 +00:00
.map(DiaryNameIterator::new)
}
2017-08-28 19:58:43 +00:00
}