Add HabitTemplate::linked_instances() function

This commit is contained in:
Matthias Beyer 2017-12-03 21:06:40 +01:00
parent 3b319fd4f0
commit 999bdf8d0d
4 changed files with 19 additions and 0 deletions

View file

@ -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" }

View file

@ -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);
}

View file

@ -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<FileLockEntry<'a>>;
/// Get instances for this template
fn linked_instances(&self) -> Result<HabitInstanceStoreIdIterator>;
/// Check whether the instance is a habit by checking its headers for the habit data
fn is_habit_template(&self) -> Result<bool>;
@ -84,6 +90,16 @@ impl HabitTemplate for Entry {
})
}
fn linked_instances(&self) -> Result<HabitInstanceStoreIdIterator> {
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<bool> {
[

View file

@ -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");