From 1dc25e3f2ec309bcb530457ebabb9a90877b4178 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 10 Feb 2020 22:25:03 +0530 Subject: implement goals --- src/habit.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/habit.rs b/src/habit.rs index 4f05217..88e1cb6 100644 --- a/src/habit.rs +++ b/src/habit.rs @@ -6,16 +6,18 @@ use chrono::NaiveDate; pub struct Habit { name: String, stats: HashMap, + goal: T, } impl Habit where T: Copy, { - pub fn new(name: &str) -> Habit { + pub fn new(name: &str, goal: T) -> Habit { return Habit { name: name.to_owned(), stats: HashMap::new(), + goal, }; } @@ -40,6 +42,9 @@ impl Habit { self.insert_entry(date, true); } } + pub fn reached_goal(&self, date: NaiveDate) -> bool { + *self.get_by_date(date).unwrap_or(&false) + } } impl Habit { @@ -60,4 +65,12 @@ impl Habit { pub fn set(&mut self, date: NaiveDate, val: u32) { *self.stats.entry(date).or_insert(val) = val; } + pub fn reached_goal(&self, date: NaiveDate) -> bool { + if let Some(v) = self.get_by_date(date) { + if *v >= self.goal { + return true; + } + } + return false; + } } -- cgit v1.2.3