imag/lib/domain/libimaghabit/src/habit.rs

362 lines
13 KiB
Rust
Raw Normal View History

2017-09-01 18:41:09 +00:00
//
// 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 toml::Value;
use toml_query::read::TomlValueReadExt;
use toml_query::insert::TomlValueInsertExt;
use chrono::NaiveDateTime;
use chrono::Local;
use chrono::NaiveDate;
use error::HabitError as HE;
use error::HabitErrorKind as HEK;
use error::*;
use iter::HabitInstanceStoreIdIterator;
use util::date_to_string;
use util::IsHabitCheck;
use util::get_string_header_from_entry;
2017-09-01 18:41:09 +00:00
use libimagentrylink::internal::InternalLinker;
2017-09-01 18:41:09 +00:00
use libimagstore::store::Store;
use libimagstore::store::FileLockEntry;
use libimagstore::store::Entry;
use libimagstore::storeid::StoreId;
2017-09-01 18:41:09 +00:00
use libimagstore::storeid::IntoStoreId;
use libimagstore::storeid::StoreIdIterator;
2017-09-01 18:41:09 +00:00
/// A HabitTemplate is a "template" of a habit. A user may define a habit "Eat vegetable".
/// If the user ate a vegetable, she should create a HabitInstance from the Habit with the
/// appropriate date (and optionally a comment) set.
pub trait HabitTemplate : Sized {
/// Create an instance from this habit template
///
/// By default creates an instance with the name of the template, the current time and the
/// current date and copies the comment from the template to the instance.
///
/// It uses `Store::retrieve()` underneath. So if there is already an instance for the day
/// passed, this will simply return the instance.
fn create_instance_with_date<'a>(&self, store: &'a Store, date: &NaiveDate)
-> Result<FileLockEntry<'a>>;
/// Shortcut for calling `Self::create_instance_with_date()` with an instance of
/// `::chrono::Local::today().naive_local()`.
fn create_instance_today<'a>(&self, store: &'a Store) -> Result<FileLockEntry<'a>>;
2017-09-01 18:41:09 +00:00
/// Get instances for this template
fn linked_instances(&self) -> Result<HabitInstanceStoreIdIterator>;
/// Get the date of the next date when the habit should be done
2017-12-22 09:59:27 +00:00
fn next_instance_date_after(&self, base: &NaiveDateTime) -> Result<Option<NaiveDate>>;
2017-12-05 19:27:33 +00:00
/// Get the date of the next date when the habit should be done
2017-12-22 09:59:27 +00:00
fn next_instance_date(&self) -> Result<Option<NaiveDate>>;
2017-12-05 19:27:33 +00:00
2017-09-01 18:41:09 +00:00
/// Check whether the instance is a habit by checking its headers for the habit data
fn is_habit_template(&self) -> Result<bool>;
fn habit_name(&self) -> Result<String>;
2017-10-22 10:31:48 +00:00
fn habit_basedate(&self) -> Result<String>;
fn habit_recur_spec(&self) -> Result<String>;
2017-09-01 18:41:09 +00:00
fn habit_comment(&self) -> Result<String>;
2017-12-22 12:20:11 +00:00
fn habit_until_date(&self) -> Result<Option<String>>;
2017-09-01 18:41:09 +00:00
/// Create a StoreId for a habit name and a date the habit should be instantiated for
fn instance_id_for(habit_name: &String, habit_date: &NaiveDate) -> Result<StoreId>;
2017-09-01 18:41:09 +00:00
}
impl HabitTemplate for Entry {
fn create_instance_with_date<'a>(&self, store: &'a Store, date: &NaiveDate) -> Result<FileLockEntry<'a>> {
let name = self.habit_name()?;
2017-09-01 18:41:09 +00:00
let comment = self.habit_comment()?;
let date = date_to_string(date);
let id = instance_id_for_name_and_datestr(&name, &date)?;
2017-09-01 18:41:09 +00:00
store.retrieve(id)
.map_err(From::from)
.and_then(|mut entry| {
{
2018-01-04 18:34:21 +00:00
let hdr = entry.get_header_mut();
hdr.insert("habit.instance.name", Value::String(name))?;
hdr.insert("habit.instance.date", Value::String(date))?;
hdr.insert("habit.instance.comment", Value::String(comment))?;
2017-09-01 18:41:09 +00:00
}
Ok(entry)
})
}
fn create_instance_today<'a>(&self, store: &'a Store) -> Result<FileLockEntry<'a>> {
self.create_instance_with_date(store, &Local::today().naive_local())
}
fn linked_instances(&self) -> Result<HabitInstanceStoreIdIterator> {
let iter = self
.get_internal_links()?
.map(|link| link.get_store_id().clone())
.filter(IsHabitCheck::is_habit_instance);
let sidi = StoreIdIterator::new(Box::new(iter));
Ok(HabitInstanceStoreIdIterator::new(sidi))
}
2017-12-22 09:59:27 +00:00
fn next_instance_date_after(&self, base: &NaiveDateTime) -> Result<Option<NaiveDate>> {
2017-12-05 19:27:33 +00:00
use kairos::timetype::TimeType;
use kairos::parser::parse;
use kairos::parser::Parsed;
use kairos::iter::extensions::Every;
let date_from_s = |r: String| -> Result<TimeType> {
match parse(&r)? {
Parsed::TimeType(tt) => Ok(tt),
Parsed::Iterator(_) => {
Err(format!("'{}' yields an iterator. Cannot use.", r).into())
},
}
};
debug!("Base is {:?}", base);
2017-12-05 19:27:33 +00:00
let basedate = date_from_s(self.habit_basedate()?)?;
debug!("Basedate is {:?}", basedate);
2017-12-05 19:27:33 +00:00
let increment = date_from_s(self.habit_recur_spec()?)?;
debug!("Increment is {:?}", increment);
2017-12-05 19:27:33 +00:00
2017-12-22 12:20:11 +00:00
let until = self.habit_until_date()?.map(|s| -> Result<_> {
try!(date_from_s(s))
.calculate()?
.get_moment()
.map(Clone::clone)
.ok_or_else(|| {
let kind : HEK = "until-date seems to have non-date value".to_owned().into();
HE::from_kind(kind)
})
});
2017-12-22 09:59:27 +00:00
debug!("Until-Date is {:?}", basedate);
2017-12-05 19:27:33 +00:00
for element in basedate.every(increment)? {
debug!("Calculating: {:?}", element);
let element = element?.calculate()?;
debug!(" = {:?}", element);
2017-12-05 19:27:33 +00:00
if let Some(ndt) = element.get_moment() {
if ndt >= base {
debug!("-> {:?} >= {:?}", ndt, base);
2017-12-22 12:20:11 +00:00
if let Some(u) = until {
if ndt > &(u?) {
return Ok(None);
} else {
return Ok(Some(ndt.date()));
}
2017-12-22 09:59:27 +00:00
} else {
return Ok(Some(ndt.date()));
}
2017-12-05 19:27:33 +00:00
}
} else {
return Err("Iterator seems to return bogus values.".to_owned().into());
}
}
unreachable!() // until we have habit-end-date support
}
/// Get the date of the next date when the habit should be done
2017-12-22 09:59:27 +00:00
fn next_instance_date(&self) -> Result<Option<NaiveDate>> {
use kairos::timetype::TimeType;
let today = TimeType::today();
let today = today.get_moment().unwrap(); // we know this is safe.
debug!("Today is {:?}", today);
self.next_instance_date_after(&today.date().and_hms(0, 0, 0))
}
2017-09-01 18:41:09 +00:00
/// Check whether the instance is a habit by checking its headers for the habit data
fn is_habit_template(&self) -> Result<bool> {
[
"habit.template.name",
2017-10-22 10:31:48 +00:00
"habit.template.basedate",
2017-09-01 18:41:09 +00:00
"habit.template.comment",
2017-12-22 10:03:42 +00:00
].iter().fold(Ok(true), |acc, path| acc.and_then(|_| {
2017-09-01 18:41:09 +00:00
self.get_header()
.read(path)
.map(|o| is_match!(o, Some(&Value::String(_))))
.map_err(From::from)
}))
}
fn habit_name(&self) -> Result<String> {
get_string_header_from_entry(self, "habit.template.name")
2017-09-01 18:41:09 +00:00
}
2017-10-22 10:31:48 +00:00
fn habit_basedate(&self) -> Result<String> {
get_string_header_from_entry(self, "habit.template.basedate")
2017-10-22 10:31:48 +00:00
}
fn habit_recur_spec(&self) -> Result<String> {
get_string_header_from_entry(self, "habit.template.recurspec")
2017-09-01 18:41:09 +00:00
}
fn habit_comment(&self) -> Result<String> {
get_string_header_from_entry(self, "habit.template.comment")
2017-09-01 18:41:09 +00:00
}
2017-12-22 12:20:11 +00:00
fn habit_until_date(&self) -> Result<Option<String>> {
self.get_header()
.read("habit.template.until")?
.map(|v| v.as_str().map(String::from))
.ok_or(HEK::HeaderTypeError("habit.template.until", "String").into())
2017-12-22 09:59:27 +00:00
}
fn instance_id_for(habit_name: &String, habit_date: &NaiveDate) -> Result<StoreId> {
instance_id_for_name_and_datestr(habit_name, &date_to_string(habit_date))
}
}
fn instance_id_for_name_and_datestr(habit_name: &String, habit_date: &String) -> Result<StoreId> {
use module_path::ModuleEntryPath;
ModuleEntryPath::new(format!("instance/{}-{}", habit_name, habit_date))
.into_storeid()
.map_err(HE::from)
2017-09-01 18:41:09 +00:00
}
pub mod builder {
use toml::Value;
use toml_query::insert::TomlValueInsertExt;
use chrono::NaiveDate;
use libimagstore::store::Store;
use libimagstore::storeid::StoreId;
use libimagstore::storeid::IntoStoreId;
use libimagstore::store::FileLockEntry;
use error::HabitError as HE;
use error::HabitErrorKind as HEK;
use error::*;
use util::date_to_string;
2017-09-01 18:41:09 +00:00
pub struct HabitBuilder {
name: Option<String>,
comment: Option<String>,
2017-10-22 10:31:48 +00:00
basedate: Option<NaiveDate>,
recurspec: Option<String>,
2017-12-22 09:59:27 +00:00
untildate: Option<NaiveDate>,
2017-09-01 18:41:09 +00:00
}
impl HabitBuilder {
2017-12-03 17:41:21 +00:00
pub fn with_name(mut self, name: String) -> Self {
2017-09-01 18:41:09 +00:00
self.name = Some(name);
self
}
2017-12-03 17:41:21 +00:00
pub fn with_comment(mut self, comment: String) -> Self {
2017-09-01 18:41:09 +00:00
self.comment = Some(comment);
self
}
2017-12-03 17:41:21 +00:00
pub fn with_basedate(mut self, date: NaiveDate) -> Self {
2017-10-22 10:31:48 +00:00
self.basedate = Some(date);
self
}
2017-12-03 17:41:21 +00:00
pub fn with_recurspec(mut self, spec: String) -> Self {
2017-10-22 10:31:48 +00:00
self.recurspec = Some(spec);
2017-09-01 18:41:09 +00:00
self
}
2017-12-22 09:59:27 +00:00
pub fn with_until(mut self, date: NaiveDate) -> Self {
self.untildate = Some(date);
self
}
2017-09-01 18:41:09 +00:00
pub fn build<'a>(self, store: &'a Store) -> Result<FileLockEntry<'a>> {
#[inline]
fn mkerr(s: &'static str) -> HE {
HE::from_kind(HEK::HabitBuilderMissing(s))
}
let name = try!(self.name.ok_or_else(|| mkerr("name")));
2017-12-03 17:44:35 +00:00
debug!("Success: Name present");
2017-10-22 10:31:48 +00:00
let dateobj = try!(self.basedate.ok_or_else(|| mkerr("date")));
2017-12-03 17:44:35 +00:00
debug!("Success: Date present");
2017-10-22 10:31:48 +00:00
let recur = try!(self.recurspec.ok_or_else(|| mkerr("recurspec")));
2017-12-03 17:44:35 +00:00
debug!("Success: Recurr spec present");
2017-12-22 12:20:11 +00:00
if let Some(until) = self.untildate {
debug!("Success: Until-Date present");
if dateobj > until {
let e = HE::from_kind(HEK::HabitBuilderLogicError("until-date before start date"));
return Err(e);
}
2017-12-22 09:59:27 +00:00
}
2017-10-22 10:31:48 +00:00
if let Err(e) = ::kairos::parser::parse(&recur) {
return Err(e).map_err(From::from);
}
2017-09-01 18:41:09 +00:00
let date = date_to_string(&dateobj);
2017-12-03 17:44:35 +00:00
debug!("Success: Date valid");
2017-09-01 18:41:09 +00:00
let comment = self.comment.unwrap_or_else(|| String::new());
let sid = try!(build_habit_template_sid(&name));
2017-12-03 17:44:35 +00:00
debug!("Creating entry in store for: {:?}", sid);
2017-09-01 18:41:09 +00:00
let mut entry = try!(store.create(sid));
try!(entry.get_header_mut().insert("habit.template.name", Value::String(name)));
2017-10-22 10:31:48 +00:00
try!(entry.get_header_mut().insert("habit.template.basedate", Value::String(date)));
try!(entry.get_header_mut().insert("habit.template.recurspec", Value::String(recur)));
2017-09-01 18:41:09 +00:00
try!(entry.get_header_mut().insert("habit.template.comment", Value::String(comment)));
2017-12-22 12:20:11 +00:00
if let Some(until) = self.untildate {
let until = date_to_string(&until);
try!(entry.get_header_mut().insert("habit.template.until", Value::String(until)));
}
2017-09-01 18:41:09 +00:00
2017-12-03 17:44:35 +00:00
debug!("Success: Created entry in store and set headers");
2017-09-01 18:41:09 +00:00
Ok(entry)
}
}
impl Default for HabitBuilder {
fn default() -> Self {
HabitBuilder {
name: None,
comment: None,
2017-10-22 10:31:48 +00:00
basedate: None,
recurspec: None,
2017-12-22 09:59:27 +00:00
untildate: None,
2017-09-01 18:41:09 +00:00
}
}
}
/// Buld a StoreId for a Habit from a date object and a name of a habit
fn build_habit_template_sid(name: &String) -> Result<StoreId> {
use module_path::ModuleEntryPath;
ModuleEntryPath::new(format!("template/{}", name)).into_storeid().map_err(From::from)
}
}