Merge pull request #1354 from matthiasbeyer/libimaghabit/check-is-done-for-date

Add helper to check whether an instance exists for a date
This commit is contained in:
Matthias Beyer 2018-03-22 14:36:32 +01:00 committed by GitHub
commit e4fc2e8010
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -80,6 +80,8 @@ pub trait HabitTemplate : Sized {
fn habit_comment(&self) -> Result<String>; fn habit_comment(&self) -> Result<String>;
fn habit_until_date(&self) -> Result<Option<String>>; fn habit_until_date(&self) -> Result<Option<String>>;
fn instance_exists_for_date(&self, date: &NaiveDate) -> Result<bool>;
/// Create a StoreId for a habit name and a date the habit should be instantiated for /// Create a StoreId for a habit name and a date the habit should be instantiated for
fn instance_id_for(habit_name: &String, habit_date: &NaiveDate) -> Result<StoreId>; fn instance_id_for(habit_name: &String, habit_date: &NaiveDate) -> Result<StoreId>;
} }
@ -222,6 +224,22 @@ impl HabitTemplate for Entry {
.map(|os| os.map(String::from)) .map(|os| os.map(String::from))
} }
fn instance_exists_for_date(&self, date: &NaiveDate) -> Result<bool> {
let name = self.habit_name()?;
let date = date_to_string(date);
for link in self.get_internal_links()? {
let sid = link.get_store_id();
let instance_id = instance_id_for_name_and_datestr(&name, &date)?;
if sid.is_habit_instance() && instance_id == *sid {
return Ok(true);
}
}
return Ok(false);
}
fn instance_id_for(habit_name: &String, habit_date: &NaiveDate) -> Result<StoreId> { fn instance_id_for(habit_name: &String, habit_date: &NaiveDate) -> Result<StoreId> {
instance_id_for_name_and_datestr(habit_name, &date_to_string(habit_date)) instance_id_for_name_and_datestr(habit_name, &date_to_string(habit_date))
} }