From b62d7eab856f46ab3138ccc933574020b4d7d0c9 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 22 Mar 2018 14:15:54 +0100 Subject: [PATCH] Fix: libimaghabit::habit::HabitTemplate should link created instances to the template --- lib/domain/libimaghabit/src/habit.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/domain/libimaghabit/src/habit.rs b/lib/domain/libimaghabit/src/habit.rs index 7119e9d5..e3963b7a 100644 --- a/lib/domain/libimaghabit/src/habit.rs +++ b/lib/domain/libimaghabit/src/habit.rs @@ -55,18 +55,19 @@ pub trait HabitTemplate : Sized { /// /// 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) + fn create_instance_with_date<'a>(&mut self, store: &'a Store, date: &NaiveDate) -> Result>; /// 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>; + fn create_instance_today<'a>(&mut 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>; + fn retrieve_instance_with_date<'a>(&mut 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>; + fn retrieve_instance_today<'a>(&mut self, store: &'a Store) -> Result>; /// Get instances for this template fn linked_instances(&self) -> Result; @@ -96,7 +97,7 @@ 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> { + fn create_instance_with_date<'a>(&mut self, store: &'a Store, date: &NaiveDate) -> Result> { let name = self.habit_name()?; let comment = self.habit_comment()?; let date = date_to_string(date); @@ -104,14 +105,14 @@ impl HabitTemplate for Entry { store.create(id) .map_err(From::from) - .and_then(|entry| postprocess_instance(entry, name, date, comment)) + .and_then(|entry| postprocess_instance(entry, name, date, comment, self)) } - fn create_instance_today<'a>(&self, store: &'a Store) -> Result> { + fn create_instance_today<'a>(&mut self, store: &'a Store) -> Result> { self.create_instance_with_date(store, &Local::today().naive_local()) } - fn retrieve_instance_with_date<'a>(&self, store: &'a Store, date: &NaiveDate) -> Result> { + fn retrieve_instance_with_date<'a>(&mut self, store: &'a Store, date: &NaiveDate) -> Result> { let name = self.habit_name()?; let comment = self.habit_comment()?; let date = date_to_string(date); @@ -119,10 +120,10 @@ impl HabitTemplate for Entry { store.create(id) .map_err(From::from) - .and_then(|entry| postprocess_instance(entry, name, date, comment)) + .and_then(|entry| postprocess_instance(entry, name, date, comment, self)) } - fn retrieve_instance_today<'a>(&self, store: &'a Store) -> Result> { + fn retrieve_instance_today<'a>(&mut self, store: &'a Store) -> Result> { self.retrieve_instance_with_date(store, &Local::today().naive_local()) } @@ -401,7 +402,8 @@ pub mod builder { fn postprocess_instance<'a>(mut entry: FileLockEntry<'a>, name: String, date: String, - comment: String) + comment: String, + template: &mut Entry) -> Result> { { @@ -411,6 +413,9 @@ fn postprocess_instance<'a>(mut entry: FileLockEntry<'a>, let _ = hdr.insert("habit.instance.date", Value::String(date))?; let _ = hdr.insert("habit.instance.comment", Value::String(comment))?; } + + entry.add_internal_link(template)?; + Ok(entry) }