aboutsummaryrefslogtreecommitdiff
path: root/src/habit.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-02-26 06:56:37 +0000
committerAkshay <[email protected]>2020-02-26 06:56:37 +0000
commit39b21b4501e48da56ddbdbadc97c3ccf32746029 (patch)
treeaea052dd3265a4d0ebaf372df1edf0e3c8466c5e /src/habit.rs
parentce5f500998f05fac13c6472681eedb7702055a7f (diff)
fix null entry with bits, add remaining api
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}