Use libimagentryutil::isa::Is in libimaghabit

This commit is contained in:
Matthias Beyer 2018-01-03 17:21:58 +01:00
parent 00d09e618e
commit 1f133eac3b
5 changed files with 29 additions and 29 deletions

View file

@ -19,10 +19,10 @@ log = "0.3"
toml = "0.4"
toml-query = "0.4.0"
error-chain = "0.11"
is-match = "0.1"
kairos = "0.1.0-beta-2"
libimagstore = { version = "0.6.0", path = "../../../lib/core/libimagstore" }
libimagerror = { version = "0.6.0", path = "../../../lib/core/libimagerror" }
libimagentryedit = { version = "0.6.0", path = "../../../lib/entry/libimagentryedit" }
libimagentrylink = { version = "0.6.0", path = "../../../lib/entry/libimagentrylink" }
libimagentryutil = { version = "0.6.0", path = "../../../lib/entry/libimagentryutil" }

View file

@ -26,6 +26,7 @@ error_chain! {
StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind);
LinkError(::libimagentrylink::error::LinkError, ::libimagentrylink::error::LinkErrorKind);
KairosError(::kairos::error::KairosError, ::kairos::error::KairosErrorKind);
EntryUtilError(::libimagentryutil::error::EntryUtilError, ::libimagentryutil::error::EntryUtilErrorKind);
}
foreign_links {

View file

@ -31,6 +31,7 @@ use iter::HabitInstanceStoreIdIterator;
use util::date_to_string;
use util::IsHabitCheck;
use util::get_string_header_from_entry;
use instance::IsHabitInstance;
use libimagentrylink::internal::InternalLinker;
use libimagstore::store::Store;
@ -39,6 +40,8 @@ use libimagstore::store::Entry;
use libimagstore::storeid::StoreId;
use libimagstore::storeid::IntoStoreId;
use libimagstore::storeid::StoreIdIterator;
use libimagentryutil::isa::Is;
use libimagentryutil::isa::IsKindHeaderPathProvider;
/// 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
@ -81,6 +84,8 @@ pub trait HabitTemplate : Sized {
fn instance_id_for(habit_name: &String, habit_date: &NaiveDate) -> Result<StoreId>;
}
provide_kindflag_path!(pub IsHabitTemplate, "habit.template.is_habit_template");
impl HabitTemplate for Entry {
fn create_instance_with_date<'a>(&self, store: &'a Store, date: &NaiveDate) -> Result<FileLockEntry<'a>> {
@ -93,10 +98,11 @@ impl HabitTemplate for Entry {
.map_err(From::from)
.and_then(|mut entry| {
{
let _ = entry.set_isflag::<IsHabitInstance>()?;
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))?;
let _ = hdr.insert("habit.instance.name", Value::String(name))?;
let _ = hdr.insert("habit.instance.date", Value::String(date))?;
let _ = hdr.insert("habit.instance.comment", Value::String(comment))?;
}
Ok(entry)
})
@ -190,16 +196,7 @@ impl HabitTemplate for Entry {
/// 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",
"habit.template.basedate",
"habit.template.comment",
].iter().fold(Ok(true), |acc, path| acc.and_then(|_| {
self.get_header()
.read(path)
.map(|o| is_match!(o, Some(&Value::String(_))))
.map_err(From::from)
}))
self.is::<IsHabitTemplate>().map_err(From::from)
}
fn habit_name(&self) -> Result<String> {
@ -248,11 +245,13 @@ pub mod builder {
use libimagstore::storeid::StoreId;
use libimagstore::storeid::IntoStoreId;
use libimagstore::store::FileLockEntry;
use libimagentryutil::isa::Is;
use error::HabitError as HE;
use error::HabitErrorKind as HEK;
use error::*;
use util::date_to_string;
use habit::IsHabitTemplate;
pub struct HabitBuilder {
name: Option<String>,
@ -324,10 +323,15 @@ pub mod builder {
debug!("Creating entry in store for: {:?}", sid);
let mut entry = try!(store.create(sid));
try!(entry.get_header_mut().insert("habit.template.name", Value::String(name)));
try!(entry.get_header_mut().insert("habit.template.basedate", Value::String(date)));
try!(entry.get_header_mut().insert("habit.template.recurspec", Value::String(recur)));
try!(entry.get_header_mut().insert("habit.template.comment", Value::String(comment)));
let _ = entry.set_isflag::<IsHabitTemplate>()?;
{
let h = entry.get_header_mut();
let _ = h.insert("habit.template.name", Value::String(name))?;
let _ = h.insert("habit.template.basedate", Value::String(date))?;
let _ = h.insert("habit.template.recurspec", Value::String(recur))?;
let _ = h.insert("habit.template.comment", Value::String(comment))?;
}
if let Some(until) = self.untildate {
let until = date_to_string(&until);
try!(entry.get_header_mut().insert("habit.template.until", Value::String(until)));

View file

@ -26,6 +26,8 @@ use error::*;
use util::*;
use libimagstore::store::Entry;
use libimagentryutil::isa::Is;
use libimagentryutil::isa::IsKindHeaderPathProvider;
/// An instance of a habit is created for each time a habit is done.
///
@ -45,18 +47,11 @@ pub trait HabitInstance {
fn get_template_name(&self) -> Result<String>;
}
provide_kindflag_path!(pub IsHabitInstance, "habit.instance.is_habit_instance");
impl HabitInstance for Entry {
fn is_habit_instance(&self) -> Result<bool> {
[
"habit.instance.name",
"habit.instance.date",
"habit.instance.comment",
].iter().fold(Ok(true), |acc, path| acc.and_then(|_| {
self.get_header()
.read(path)
.map(|o| is_match!(o, Some(&Value::String(_))))
.map_err(From::from)
}))
self.is::<IsHabitInstance>().map_err(From::from)
}
fn get_date(&self) -> Result<NaiveDate> {

View file

@ -23,12 +23,12 @@ extern crate toml_query;
extern crate kairos;
#[macro_use] extern crate log;
#[macro_use] extern crate error_chain;
#[macro_use] extern crate is_match;
#[macro_use] extern crate libimagstore;
extern crate libimagerror;
extern crate libimagentryedit;
extern crate libimagentrylink;
#[macro_use] extern crate libimagentryutil;
module_entry_path_mod!("habit");