aboutsummaryrefslogtreecommitdiff
path: root/src/habit.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/habit.rs')
-rw-r--r--src/habit.rs15
1 files changed, 14 insertions, 1 deletions
diff --git a/src/habit.rs b/src/habit.rs
index cd1703e..8527ac2 100644
--- a/src/habit.rs
+++ b/src/habit.rs
@@ -33,6 +33,7 @@ pub trait HabitTrait {
33 fn get_by_date(&self, date: NaiveDate) -> Option<&HabitType>; 33 fn get_by_date(&self, date: NaiveDate) -> Option<&HabitType>;
34 fn insert_entry(&mut self, date: NaiveDate, val: HabitType); 34 fn insert_entry(&mut self, date: NaiveDate, val: HabitType);
35 fn reached_goal(&self, date: NaiveDate) -> bool; 35 fn reached_goal(&self, date: NaiveDate) -> bool;
36 fn remaining(&self, date: NaiveDate) -> u32;
36} 37}
37 38
38#[derive(Serialize, Debug)] 39#[derive(Serialize, Debug)]
@@ -73,7 +74,7 @@ impl Habit {
73 } 74 }
74 } else { 75 } else {
75 match self.goal { 76 match self.goal {
76 HabitType::Bit(_) => self.insert_entry(date, HabitType::Bit(false)), 77 HabitType::Bit(_) => self.insert_entry(date, HabitType::Bit(true)),
77 HabitType::Count(_) => self.insert_entry(date, HabitType::Count(0)), 78 HabitType::Count(_) => self.insert_entry(date, HabitType::Count(0)),
78 } 79 }
79 } 80 }
@@ -109,4 +110,16 @@ impl HabitTrait for Habit {
109 } 110 }
110 return false; 111 return false;
111 } 112 }
113 fn remaining(&self, date: NaiveDate) -> u32 {
114 if self.reached_goal(date) {
115 return 0;
116 } else if let Some(val) = self.stats.get(&date) {
117 match val {
118 HabitType::Bit(_) => return 1,
119 HabitType::Count(c) => return self.goal.inner_count() - *c,
120 }
121 } else {
122 return 0;
123 }
124 }
112} 125}