diff options
author | Akshay <[email protected]> | 2020-02-10 16:55:03 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2020-02-10 16:55:03 +0000 |
commit | 1dc25e3f2ec309bcb530457ebabb9a90877b4178 (patch) | |
tree | 1c5eb0621f951e72a9d008e6851e81523bff3f72 /src/habit.rs | |
parent | b7041c51f2cf55f8c26bd852162776aed2111de5 (diff) |
implement goals
Diffstat (limited to 'src/habit.rs')
-rw-r--r-- | src/habit.rs | 15 |
1 files changed, 14 insertions, 1 deletions
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; | |||
6 | pub struct Habit<T> { | 6 | pub struct Habit<T> { |
7 | name: String, | 7 | name: String, |
8 | stats: HashMap<NaiveDate, T>, | 8 | stats: HashMap<NaiveDate, T>, |
9 | goal: T, | ||
9 | } | 10 | } |
10 | 11 | ||
11 | impl<T> Habit<T> | 12 | impl<T> Habit<T> |
12 | where | 13 | where |
13 | T: Copy, | 14 | T: Copy, |
14 | { | 15 | { |
15 | pub fn new(name: &str) -> Habit<T> { | 16 | pub fn new(name: &str, goal: T) -> Habit<T> { |
16 | return Habit { | 17 | return Habit { |
17 | name: name.to_owned(), | 18 | name: name.to_owned(), |
18 | stats: HashMap::new(), | 19 | stats: HashMap::new(), |
20 | goal, | ||
19 | }; | 21 | }; |
20 | } | 22 | } |
21 | 23 | ||
@@ -40,6 +42,9 @@ impl Habit<bool> { | |||
40 | self.insert_entry(date, true); | 42 | self.insert_entry(date, true); |
41 | } | 43 | } |
42 | } | 44 | } |
45 | pub fn reached_goal(&self, date: NaiveDate) -> bool { | ||
46 | *self.get_by_date(date).unwrap_or(&false) | ||
47 | } | ||
43 | } | 48 | } |
44 | 49 | ||
45 | impl Habit<u32> { | 50 | impl Habit<u32> { |
@@ -60,4 +65,12 @@ impl Habit<u32> { | |||
60 | pub fn set(&mut self, date: NaiveDate, val: u32) { | 65 | pub fn set(&mut self, date: NaiveDate, val: u32) { |
61 | *self.stats.entry(date).or_insert(val) = val; | 66 | *self.stats.entry(date).or_insert(val) = val; |
62 | } | 67 | } |
68 | pub fn reached_goal(&self, date: NaiveDate) -> bool { | ||
69 | if let Some(v) = self.get_by_date(date) { | ||
70 | if *v >= self.goal { | ||
71 | return true; | ||
72 | } | ||
73 | } | ||
74 | return false; | ||
75 | } | ||
63 | } | 76 | } |