From 964b168f189575bb994318aa0d8a28c1480980fc Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 22 Mar 2018 13:51:11 +0100 Subject: [PATCH 1/3] Add "retrieve" variants for retrieving instances and fix create implementation to use `Store::create()`, which did the wrong thing. --- lib/domain/libimaghabit/src/habit.rs | 32 +++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/domain/libimaghabit/src/habit.rs b/lib/domain/libimaghabit/src/habit.rs index c273e3dd..f5d984fb 100644 --- a/lib/domain/libimaghabit/src/habit.rs +++ b/lib/domain/libimaghabit/src/habit.rs @@ -62,6 +62,12 @@ pub trait HabitTemplate : Sized { /// `::chrono::Local::today().naive_local()`. fn create_instance_today<'a>(&self, store: &'a Store) -> Result>; + /// Same as `HabitTemplate::create_instance_with_date()` but uses `Store::retrieve` internally. + fn retrieve_instance_with_date<'a>(&self, store: &'a Store, date: &NaiveDate) -> Result>; + + /// Same as `HabitTemplate::create_instance_today()` but uses `Store::retrieve` internally. + fn retrieve_instance_today<'a>(&self, store: &'a Store) -> Result>; + /// Get instances for this template fn linked_instances(&self) -> Result; @@ -94,7 +100,7 @@ impl HabitTemplate for Entry { let date = date_to_string(date); let id = instance_id_for_name_and_datestr(&name, &date)?; - store.retrieve(id) + store.create(id) .map_err(From::from) .and_then(|mut entry| { { @@ -112,6 +118,30 @@ impl HabitTemplate for Entry { self.create_instance_with_date(store, &Local::today().naive_local()) } + fn retrieve_instance_with_date<'a>(&self, store: &'a Store, date: &NaiveDate) -> Result> { + let name = self.habit_name()?; + let comment = self.habit_comment()?; + let date = date_to_string(date); + let id = instance_id_for_name_and_datestr(&name, &date)?; + + store.create(id) + .map_err(From::from) + .and_then(|mut entry| { + { + let _ = entry.set_isflag::()?; + let hdr = entry.get_header_mut(); + 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) + }) + } + + fn retrieve_instance_today<'a>(&self, store: &'a Store) -> Result> { + self.retrieve_instance_with_date(store, &Local::today().naive_local()) + } + fn linked_instances(&self) -> Result { let iter = self .get_internal_links()? From 2232f0153fe36460938b12a7fb3c5e91ac6add20 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 22 Mar 2018 13:54:15 +0100 Subject: [PATCH 2/3] Dedup code --- lib/domain/libimaghabit/src/habit.rs | 38 +++++++++++++--------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/lib/domain/libimaghabit/src/habit.rs b/lib/domain/libimaghabit/src/habit.rs index f5d984fb..ea8ed7d4 100644 --- a/lib/domain/libimaghabit/src/habit.rs +++ b/lib/domain/libimaghabit/src/habit.rs @@ -102,16 +102,7 @@ impl HabitTemplate for Entry { store.create(id) .map_err(From::from) - .and_then(|mut entry| { - { - let _ = entry.set_isflag::()?; - let hdr = entry.get_header_mut(); - 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) - }) + .and_then(|entry| postprocess_instance(entry, name, date, comment)) } fn create_instance_today<'a>(&self, store: &'a Store) -> Result> { @@ -126,16 +117,7 @@ impl HabitTemplate for Entry { store.create(id) .map_err(From::from) - .and_then(|mut entry| { - { - let _ = entry.set_isflag::()?; - let hdr = entry.get_header_mut(); - 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) - }) + .and_then(|entry| postprocess_instance(entry, name, date, comment)) } fn retrieve_instance_today<'a>(&self, store: &'a Store) -> Result> { @@ -394,3 +376,19 @@ pub mod builder { } +fn postprocess_instance<'a>(mut entry: FileLockEntry<'a>, + name: String, + date: String, + comment: String) + -> Result> +{ + { + let _ = entry.set_isflag::()?; + let hdr = entry.get_header_mut(); + 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) +} + From 247b9499a7ee68b7fb7fb92e06b313ef20f4e782 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 22 Mar 2018 13:59:01 +0100 Subject: [PATCH 3/3] Refactor for less verbose code --- lib/domain/libimaghabit/src/habit.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/domain/libimaghabit/src/habit.rs b/lib/domain/libimaghabit/src/habit.rs index ea8ed7d4..4548fb66 100644 --- a/lib/domain/libimaghabit/src/habit.rs +++ b/lib/domain/libimaghabit/src/habit.rs @@ -258,6 +258,7 @@ pub mod builder { use libimagstore::storeid::IntoStoreId; use libimagstore::store::FileLockEntry; use libimagentryutil::isa::Is; + use libimagutil::debug_result::DebugResult; use error::HabitError as HE; use error::HabitErrorKind as HEK; @@ -307,14 +308,17 @@ pub mod builder { HE::from_kind(HEK::HabitBuilderMissing(s)) } - let name = try!(self.name.ok_or_else(|| mkerr("name"))); - debug!("Success: Name present"); + let name = self.name + .ok_or_else(|| mkerr("name")) + .map_dbg_str("Success: Name present")?; - let dateobj = try!(self.basedate.ok_or_else(|| mkerr("date"))); - debug!("Success: Date present"); + let dateobj = self.basedate + .ok_or_else(|| mkerr("date")) + .map_dbg_str("Success: Date present")?; - let recur = try!(self.recurspec.ok_or_else(|| mkerr("recurspec"))); - debug!("Success: Recurr spec present"); + let recur = self.recurspec + .ok_or_else(|| mkerr("recurspec")) + .map_dbg_str("Success: Recurr spec present")?; if let Some(until) = self.untildate { debug!("Success: Until-Date present");