From 999bdf8d0dcddfe440152878f679a7a77342c07f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 3 Dec 2017 21:06:40 +0100 Subject: [PATCH] Add HabitTemplate::linked_instances() function --- lib/domain/libimaghabit/Cargo.toml | 1 + lib/domain/libimaghabit/src/error.rs | 1 + lib/domain/libimaghabit/src/habit.rs | 16 ++++++++++++++++ lib/domain/libimaghabit/src/lib.rs | 1 + 4 files changed, 19 insertions(+) diff --git a/lib/domain/libimaghabit/Cargo.toml b/lib/domain/libimaghabit/Cargo.toml index c00abd51..28445e68 100644 --- a/lib/domain/libimaghabit/Cargo.toml +++ b/lib/domain/libimaghabit/Cargo.toml @@ -25,3 +25,4 @@ kairos = "0.1.0-beta-2" libimagstore = { version = "0.5.0", path = "../../../lib/core/libimagstore" } libimagerror = { version = "0.5.0", path = "../../../lib/core/libimagerror" } libimagentryedit = { version = "0.5.0", path = "../../../lib/entry/libimagentryedit" } +libimagentrylink = { version = "0.5.0", path = "../../../lib/entry/libimagentrylink" } diff --git a/lib/domain/libimaghabit/src/error.rs b/lib/domain/libimaghabit/src/error.rs index 2a0a051e..18f439c9 100644 --- a/lib/domain/libimaghabit/src/error.rs +++ b/lib/domain/libimaghabit/src/error.rs @@ -24,6 +24,7 @@ error_chain! { links { StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind); + LinkError(::libimagentrylink::error::LinkError, ::libimagentrylink::error::LinkErrorKind); KairosError(::kairos::error::KairosError, ::kairos::error::KairosErrorKind); } diff --git a/lib/domain/libimaghabit/src/habit.rs b/lib/domain/libimaghabit/src/habit.rs index 0e372b3f..3da9395c 100644 --- a/lib/domain/libimaghabit/src/habit.rs +++ b/lib/domain/libimaghabit/src/habit.rs @@ -30,13 +30,16 @@ use error::*; use iter::HabitInstanceStoreIdIterator; use util::date_to_string; use util::date_from_string; +use util::IsHabitCheck; +use libimagentrylink::internal::InternalLinker; use libimagstore::store::Store; use libimagstore::store::FileLockEntry; use libimagstore::store::Entry; use libimagstore::iter::get::StoreIdGetIteratorExtension; use libimagstore::storeid::StoreId; use libimagstore::storeid::IntoStoreId; +use libimagstore::storeid::StoreIdIterator; /// 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 @@ -51,6 +54,9 @@ pub trait HabitTemplate : Sized { /// It uses `Store::retrieve()` underneath fn create_instance<'a>(&self, store: &'a Store) -> Result>; + /// Get instances for this template + fn linked_instances(&self) -> Result; + /// Check whether the instance is a habit by checking its headers for the habit data fn is_habit_template(&self) -> Result; @@ -84,6 +90,16 @@ impl HabitTemplate for Entry { }) } + fn linked_instances(&self) -> Result { + 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)) + } + /// Check whether the instance is a habit by checking its headers for the habit data fn is_habit_template(&self) -> Result { [ diff --git a/lib/domain/libimaghabit/src/lib.rs b/lib/domain/libimaghabit/src/lib.rs index 2615455c..f193e3be 100644 --- a/lib/domain/libimaghabit/src/lib.rs +++ b/lib/domain/libimaghabit/src/lib.rs @@ -28,6 +28,7 @@ extern crate kairos; #[macro_use] extern crate libimagerror; #[macro_use] extern crate libimagstore; extern crate libimagentryedit; +extern crate libimagentrylink; module_entry_path_mod!("habit");