From b181b488a2a4285cec338d0328c05cbeb1098486 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 24 Nov 2017 18:54:55 +0100 Subject: [PATCH] Add extension to check whether an Entry or a StoreId points to a habit instance/template --- lib/domain/libimaghabit/src/util.rs | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/lib/domain/libimaghabit/src/util.rs b/lib/domain/libimaghabit/src/util.rs index 3d60cc58..4acd6282 100644 --- a/lib/domain/libimaghabit/src/util.rs +++ b/lib/domain/libimaghabit/src/util.rs @@ -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 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() + } +} +