Add extension to check whether an Entry or a StoreId points to a habit instance/template

This commit is contained in:
Matthias Beyer 2017-11-24 18:54:55 +01:00
parent 9af2f34b58
commit b181b488a2

View file

@ -25,6 +25,9 @@ use error::Result;
use habit::HabitTemplate;
use instance::HabitInstance;
use libimagstore::storeid::StoreId;
use libimagstore::store::Entry;
pub const NAIVE_DATE_STRING_FORMAT : &'static str = "%Y-%m-%d";
pub fn date_to_string(ndt: &NaiveDate) -> String {
@ -50,3 +53,40 @@ impl<H> IsValidHabitObj for H
// Empty
}
pub trait IsHabitCheck {
fn is_habit(&self) -> bool;
fn is_habit_instance(&self) -> bool;
fn is_habit_template(&self) -> bool;
}
impl IsHabitCheck for StoreId {
fn is_habit(&self) -> bool {
self.is_in_collection(&["habit"])
}
fn is_habit_instance(&self) -> bool {
self.is_in_collection(&["habit", "instance"])
}
fn is_habit_template(&self) -> bool {
self.is_in_collection(&["habit", "template"])
}
}
impl IsHabitCheck for Entry {
/// Helper function to check whether an entry is a habit (either instance or template)
fn is_habit(&self) -> bool {
self.get_location().is_habit()
}
/// Check whether an entry is a habit instance
fn is_habit_instance(&self) -> bool {
self.get_location().is_habit_instance()
}
/// Check whether an entry is a habit template
fn is_habit_template(&self) -> bool {
self.get_location().is_habit_template()
}
}