From 1f133eac3b98112a87df9d4421113d1b9179c56d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 3 Jan 2018 17:21:58 +0100 Subject: [PATCH] Use libimagentryutil::isa::Is in libimaghabit --- lib/domain/libimaghabit/Cargo.toml | 2 +- lib/domain/libimaghabit/src/error.rs | 1 + lib/domain/libimaghabit/src/habit.rs | 38 ++++++++++++++----------- lib/domain/libimaghabit/src/instance.rs | 15 ++++------ lib/domain/libimaghabit/src/lib.rs | 2 +- 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/lib/domain/libimaghabit/Cargo.toml b/lib/domain/libimaghabit/Cargo.toml index cde44ab1..8025734d 100644 --- a/lib/domain/libimaghabit/Cargo.toml +++ b/lib/domain/libimaghabit/Cargo.toml @@ -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" } diff --git a/lib/domain/libimaghabit/src/error.rs b/lib/domain/libimaghabit/src/error.rs index 71abfb26..6eeee329 100644 --- a/lib/domain/libimaghabit/src/error.rs +++ b/lib/domain/libimaghabit/src/error.rs @@ -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 { diff --git a/lib/domain/libimaghabit/src/habit.rs b/lib/domain/libimaghabit/src/habit.rs index 29794101..37bce217 100644 --- a/lib/domain/libimaghabit/src/habit.rs +++ b/lib/domain/libimaghabit/src/habit.rs @@ -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; } +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> { @@ -93,10 +98,11 @@ impl HabitTemplate for Entry { .map_err(From::from) .and_then(|mut entry| { { + let _ = entry.set_isflag::()?; 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 { - [ - "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::().map_err(From::from) } fn habit_name(&self) -> Result { @@ -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, @@ -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::()?; + { + 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))); diff --git a/lib/domain/libimaghabit/src/instance.rs b/lib/domain/libimaghabit/src/instance.rs index 773ccaba..1b66f04a 100644 --- a/lib/domain/libimaghabit/src/instance.rs +++ b/lib/domain/libimaghabit/src/instance.rs @@ -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; } +provide_kindflag_path!(pub IsHabitInstance, "habit.instance.is_habit_instance"); + impl HabitInstance for Entry { fn is_habit_instance(&self) -> Result { - [ - "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::().map_err(From::from) } fn get_date(&self) -> Result { diff --git a/lib/domain/libimaghabit/src/lib.rs b/lib/domain/libimaghabit/src/lib.rs index 62e18f45..06545d6c 100644 --- a/lib/domain/libimaghabit/src/lib.rs +++ b/lib/domain/libimaghabit/src/lib.rs @@ -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");