2017-05-30 18:25:00 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2018-02-07 01:48:53 +00:00
|
|
|
// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2017-05-30 18:25:00 +00:00
|
|
|
//
|
|
|
|
// 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;
|
2017-05-30 18:25:00 +00:00
|
|
|
use libimagstore::store::Store;
|
|
|
|
use libimagerror::trace::trace_error;
|
2018-02-20 16:18:46 +00:00
|
|
|
use libimagentryutil::isa::Is;
|
2017-05-30 18:25:00 +00:00
|
|
|
|
2017-07-15 18:17:50 +00:00
|
|
|
use chrono::offset::Local;
|
2017-05-30 18:25:00 +00:00
|
|
|
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-05-30 18:25:00 +00:00
|
|
|
|
2018-02-20 16:18:46 +00:00
|
|
|
use entry::IsDiaryEntry;
|
2017-05-30 18:25:00 +00:00
|
|
|
use diaryid::DiaryId;
|
2018-03-02 20:25:48 +00:00
|
|
|
use diaryid::FromStoreId;
|
2017-05-30 18:25:00 +00:00
|
|
|
use error::DiaryErrorKind as DEK;
|
2017-09-03 14:13:08 +00:00
|
|
|
use error::ResultExt;
|
2017-09-03 19:39:32 +00:00
|
|
|
use error::Result;
|
2017-05-30 18:25:00 +00:00
|
|
|
use iter::DiaryEntryIterator;
|
2017-08-28 19:58:43 +00:00
|
|
|
use iter::DiaryNameIterator;
|
2017-05-30 18:25:00 +00:00
|
|
|
|
2017-08-29 14:04:11 +00:00
|
|
|
pub trait Diary {
|
2017-05-30 18:25:00 +00:00
|
|
|
|
|
|
|
// 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-05-30 18:25:00 +00:00
|
|
|
|
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-05-30 18:25:00 +00:00
|
|
|
|
2018-06-08 00:19:55 +00:00
|
|
|
fn new_entry_at(&self, diary_name: &str, ndt: &NaiveDateTime) -> 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 {
|
2017-05-30 18:25:00 +00:00
|
|
|
|
|
|
|
// 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-05-30 18:25:00 +00:00
|
|
|
let dt = Local::now();
|
|
|
|
let ndt = dt.naive_local();
|
2018-01-04 10:42:27 +00:00
|
|
|
let id = DiaryId::new(String::from(diary_name), ndt.year(), ndt.month(), ndt.day(), 0, 0, 0);
|
2017-05-30 18:25:00 +00:00
|
|
|
|
2018-02-20 16:18:46 +00:00
|
|
|
let mut entry = self.retrieve(id).chain_err(|| DEK::StoreReadError)?;
|
|
|
|
let _ = entry.set_isflag::<IsDiaryEntry>()?;
|
|
|
|
Ok(entry)
|
2017-05-30 18:25:00 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2018-06-08 00:19:55 +00:00
|
|
|
self.new_entry_at(diary_name, &ndt)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_entry_at(&self, diary_name: &str, ndt: &NaiveDateTime) -> Result<FileLockEntry> {
|
2017-08-28 19:58:43 +00:00
|
|
|
let id = DiaryId::new(String::from(diary_name),
|
2017-09-04 21:02:45 +00:00
|
|
|
ndt.year(),
|
|
|
|
ndt.month(),
|
|
|
|
ndt.day(),
|
|
|
|
ndt.hour(),
|
2018-01-04 10:42:27 +00:00
|
|
|
ndt.minute(),
|
|
|
|
ndt.second());
|
2017-08-28 19:58:43 +00:00
|
|
|
|
2018-02-20 16:18:46 +00:00
|
|
|
let mut entry = self.retrieve(id).chain_err(|| DEK::StoreReadError)?;
|
|
|
|
let _ = entry.set_isflag::<IsDiaryEntry>()?;
|
|
|
|
Ok(entry)
|
2017-05-30 18:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get an iterator for iterating over all entries
|
2017-08-28 19:58:43 +00:00
|
|
|
fn entries(&self, diary_name: &str) -> Result<DiaryEntryIterator> {
|
2018-02-20 16:28:54 +00:00
|
|
|
debug!("Building iterator for module 'diary' with diary name = '{}'", diary_name);
|
2018-03-02 20:25:48 +00:00
|
|
|
Store::entries(self)
|
|
|
|
.map(|iter| DiaryEntryIterator::new(String::from(diary_name), iter.without_store()))
|
2017-09-03 14:13:08 +00:00
|
|
|
.chain_err(|| DEK::StoreReadError)
|
2017-05-30 18:25:00 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 15:28:54 +00:00
|
|
|
/// get the id of the youngest entry
|
|
|
|
///
|
|
|
|
/// TODO: We collect internally here. We shouldn't do that. Solution unclear.
|
2017-08-28 19:58:43 +00:00
|
|
|
fn get_youngest_entry_id(&self, diary_name: &str) -> Option<Result<DiaryId>> {
|
2018-04-30 15:28:54 +00:00
|
|
|
use error::DiaryError as DE;
|
|
|
|
|
2017-08-28 19:58:43 +00:00
|
|
|
match Diary::entries(self, diary_name) {
|
2017-05-30 18:25:00 +00:00
|
|
|
Err(e) => Some(Err(e)),
|
|
|
|
Ok(entries) => {
|
2018-04-30 15:28:54 +00:00
|
|
|
let mut sorted_entries = vec![];
|
|
|
|
|
|
|
|
for entry in entries {
|
|
|
|
let entry = match entry {
|
|
|
|
Ok(e) => DiaryId::from_storeid(&e),
|
|
|
|
Err(e) => return Some(Err(e).map_err(DE::from)),
|
|
|
|
};
|
|
|
|
|
|
|
|
sorted_entries.push(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
sorted_entries.into_iter().sorted_by(|a, b| {
|
2017-08-28 19:58:43 +00:00
|
|
|
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()
|
2018-04-22 12:10:24 +00:00
|
|
|
.rev()
|
2017-08-28 19:58:43 +00:00
|
|
|
//.map(|sidres| sidres.map(|sid| DiaryId::from_storeid(&sid)))
|
|
|
|
.next()
|
2017-05-30 18:25:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 19:58:43 +00:00
|
|
|
/// Get all diary names
|
|
|
|
fn diary_names(&self) -> Result<DiaryNameIterator> {
|
2018-04-24 12:01:40 +00:00
|
|
|
self.entries()
|
|
|
|
.map(|it| DiaryNameIterator::new(it.without_store()))
|
|
|
|
.map_err(::error::DiaryError::from)
|
2017-05-30 18:25:00 +00:00
|
|
|
}
|
2017-08-28 19:58:43 +00:00
|
|
|
|
2017-05-30 18:25:00 +00:00
|
|
|
}
|
|
|
|
|