aboutsummaryrefslogtreecommitdiff
path: root/src/habit/traits.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-07-12 15:57:35 +0100
committerAkshay <[email protected]>2020-07-12 15:57:35 +0100
commite4e05dd72187c22d7c51b216a16b6bb5d7e88572 (patch)
tree854f6dcb731c80897afc19a2daf1e78346a66afc /src/habit/traits.rs
parent0abafde050a5cabaedb189e4cb7c77a7f14c11d3 (diff)
refactor habit.rs into habit module
Diffstat (limited to 'src/habit/traits.rs')
-rw-r--r--src/habit/traits.rs94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/habit/traits.rs b/src/habit/traits.rs
new file mode 100644
index 0000000..e28e55d
--- /dev/null
+++ b/src/habit/traits.rs
@@ -0,0 +1,94 @@
1use chrono::NaiveDate;
2use cursive::direction::Direction;
3use cursive::event::{Event, EventResult};
4use cursive::{Printer, Vec2};
5
6use typetag;
7
8use crate::habit::{Bit, Count, TrackEvent, ViewMode};
9use crate::views::ShadowView;
10
11pub trait Habit {
12 type HabitType;
13
14 fn set_name(&mut self, name: impl AsRef<str>);
15 fn set_goal(&mut self, goal: Self::HabitType);
16 fn name(&self) -> String;
17 fn get_by_date(&self, date: NaiveDate) -> Option<&Self::HabitType>;
18 fn insert_entry(&mut self, date: NaiveDate, val: Self::HabitType);
19 fn reached_goal(&self, date: NaiveDate) -> bool;
20 fn remaining(&self, date: NaiveDate) -> u32;
21 fn goal(&self) -> u32;
22 fn modify(&mut self, date: NaiveDate, event: TrackEvent);
23
24 fn set_view_month_offset(&mut self, offset: u32);
25 fn view_month_offset(&self) -> u32;
26
27 fn set_view_mode(&mut self, mode: ViewMode);
28 fn view_mode(&self) -> ViewMode;
29}
30
31#[typetag::serde(tag = "type")]
32pub trait HabitWrapper: erased_serde::Serialize {
33 fn remaining(&self, date: NaiveDate) -> u32;
34 fn goal(&self) -> u32;
35 fn modify(&mut self, date: NaiveDate, event: TrackEvent);
36 fn draw(&self, printer: &Printer);
37 fn on_event(&mut self, event: Event) -> EventResult;
38 fn required_size(&mut self, _: Vec2) -> Vec2;
39 fn take_focus(&mut self, _: Direction) -> bool;
40 fn get_name(&self) -> String;
41
42 fn set_view_month_offset(&mut self, offset: u32);
43 fn view_month_offset(&self) -> u32;
44
45 fn set_view_mode(&mut self, mode: ViewMode);
46 fn view_mode(&self) -> ViewMode;
47}
48
49macro_rules! auto_habit_impl {
50 ($struct_name:ident) => {
51 #[typetag::serde]
52 impl HabitWrapper for $struct_name {
53 fn remaining(&self, date: NaiveDate) -> u32 {
54 Habit::remaining(self, date)
55 }
56 fn goal(&self) -> u32 {
57 Habit::goal(self)
58 }
59 fn modify(&mut self, date: NaiveDate, event: TrackEvent) {
60 Habit::modify(self, date, event);
61 }
62 fn draw(&self, printer: &Printer) {
63 ShadowView::draw(self, printer)
64 }
65 fn on_event(&mut self, event: Event) -> EventResult {
66 ShadowView::on_event(self, event)
67 }
68 fn required_size(&mut self, x: Vec2) -> Vec2 {
69 ShadowView::required_size(self, x)
70 }
71 fn take_focus(&mut self, d: Direction) -> bool {
72 ShadowView::take_focus(self, d)
73 }
74 fn get_name(&self) -> String {
75 Habit::name(self)
76 }
77 fn set_view_month_offset(&mut self, offset: u32) {
78 Habit::set_view_month_offset(self, offset)
79 }
80 fn view_month_offset(&self) -> u32 {
81 Habit::view_month_offset(self)
82 }
83 fn set_view_mode(&mut self, mode: ViewMode) {
84 Habit::set_view_mode(self, mode)
85 }
86 fn view_mode(&self) -> ViewMode {
87 Habit::view_mode(self)
88 }
89 }
90 };
91}
92
93auto_habit_impl!(Count);
94auto_habit_impl!(Bit);