diff options
Diffstat (limited to 'src/habit/count.rs')
-rw-r--r-- | src/habit/count.rs | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/src/habit/count.rs b/src/habit/count.rs new file mode 100644 index 0000000..a0e0aee --- /dev/null +++ b/src/habit/count.rs | |||
@@ -0,0 +1,102 @@ | |||
1 | use std::collections::HashMap; | ||
2 | |||
3 | use chrono::NaiveDate; | ||
4 | use serde::{Deserialize, Serialize}; | ||
5 | |||
6 | use crate::habit::traits::Habit; | ||
7 | use crate::habit::{TrackEvent, ViewMode}; | ||
8 | |||
9 | #[derive(Debug, Serialize, Deserialize)] | ||
10 | pub struct Count { | ||
11 | name: String, | ||
12 | stats: HashMap<NaiveDate, u32>, | ||
13 | goal: u32, | ||
14 | |||
15 | #[serde(skip)] | ||
16 | view_month_offset: u32, | ||
17 | |||
18 | #[serde(skip)] | ||
19 | view_mode: ViewMode, | ||
20 | } | ||
21 | |||
22 | impl Count { | ||
23 | pub fn new(name: impl AsRef<str>, goal: u32) -> Self { | ||
24 | return Count { | ||
25 | name: name.as_ref().to_owned(), | ||
26 | stats: HashMap::new(), | ||
27 | goal, | ||
28 | view_month_offset: 0, | ||
29 | view_mode: ViewMode::Day, | ||
30 | }; | ||
31 | } | ||
32 | } | ||
33 | |||
34 | impl Habit for Count { | ||
35 | type HabitType = u32; | ||
36 | |||
37 | fn name(&self) -> String { | ||
38 | return self.name.clone(); | ||
39 | } | ||
40 | fn set_name(&mut self, n: impl AsRef<str>) { | ||
41 | self.name = n.as_ref().to_owned(); | ||
42 | } | ||
43 | fn set_goal(&mut self, g: Self::HabitType) { | ||
44 | self.goal = g; | ||
45 | } | ||
46 | fn get_by_date(&self, date: NaiveDate) -> Option<&Self::HabitType> { | ||
47 | self.stats.get(&date) | ||
48 | } | ||
49 | fn insert_entry(&mut self, date: NaiveDate, val: Self::HabitType) { | ||
50 | *self.stats.entry(date).or_insert(val) = val; | ||
51 | } | ||
52 | fn reached_goal(&self, date: NaiveDate) -> bool { | ||
53 | if let Some(val) = self.stats.get(&date) { | ||
54 | if val >= &self.goal { | ||
55 | return true; | ||
56 | } | ||
57 | } | ||
58 | return false; | ||
59 | } | ||
60 | fn remaining(&self, date: NaiveDate) -> u32 { | ||
61 | if self.reached_goal(date) { | ||
62 | return 0; | ||
63 | } else { | ||
64 | if let Some(val) = self.stats.get(&date) { | ||
65 | return self.goal - val; | ||
66 | } else { | ||
67 | return self.goal; | ||
68 | } | ||
69 | } | ||
70 | } | ||
71 | fn goal(&self) -> u32 { | ||
72 | return self.goal; | ||
73 | } | ||
74 | fn modify(&mut self, date: NaiveDate, event: TrackEvent) { | ||
75 | if let Some(val) = self.stats.get_mut(&date) { | ||
76 | match event { | ||
77 | TrackEvent::Increment => *val += 1, | ||
78 | TrackEvent::Decrement => { | ||
79 | if *val > 0 { | ||
80 | *val -= 1 | ||
81 | } else { | ||
82 | *val = 0 | ||
83 | }; | ||
84 | } | ||
85 | } | ||
86 | } else { | ||
87 | self.insert_entry(date, 1); | ||
88 | } | ||
89 | } | ||
90 | fn set_view_month_offset(&mut self, offset: u32) { | ||
91 | self.view_month_offset = offset; | ||
92 | } | ||
93 | fn view_month_offset(&self) -> u32 { | ||
94 | self.view_month_offset | ||
95 | } | ||
96 | fn set_view_mode(&mut self, mode: ViewMode) { | ||
97 | self.view_mode = mode; | ||
98 | } | ||
99 | fn view_mode(&self) -> ViewMode { | ||
100 | self.view_mode | ||
101 | } | ||
102 | } | ||