diff options
Diffstat (limited to 'src/habit.rs')
-rw-r--r-- | src/habit.rs | 69 |
1 files changed, 35 insertions, 34 deletions
diff --git a/src/habit.rs b/src/habit.rs index 45eeefa..13a2bd2 100644 --- a/src/habit.rs +++ b/src/habit.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | use std::collections::HashMap; | 1 | use std::collections::HashMap; |
2 | 2 | ||
3 | use chrono::NaiveDate; | 3 | use chrono::NaiveDate; |
4 | use serde::Serialize; | 4 | use serde::{Deserialize, Serialize}; |
5 | 5 | ||
6 | use cursive::direction::Direction; | 6 | use cursive::direction::Direction; |
7 | use cursive::event::{Event, EventResult}; | 7 | use cursive::event::{Event, EventResult}; |
@@ -15,7 +15,7 @@ pub enum TrackEvent { | |||
15 | Decrement, | 15 | Decrement, |
16 | } | 16 | } |
17 | 17 | ||
18 | #[derive(Copy, Clone, Debug, Serialize)] | 18 | #[derive(Copy, Clone, Debug, Serialize, Deserialize)] |
19 | pub struct CustomBool(bool); | 19 | pub struct CustomBool(bool); |
20 | 20 | ||
21 | use std::fmt; | 21 | use std::fmt; |
@@ -53,6 +53,7 @@ pub trait Habit { | |||
53 | fn modify(&mut self, date: NaiveDate, event: TrackEvent); | 53 | fn modify(&mut self, date: NaiveDate, event: TrackEvent); |
54 | } | 54 | } |
55 | 55 | ||
56 | #[typetag::serde(tag = "type")] | ||
56 | pub trait HabitWrapper: erased_serde::Serialize { | 57 | pub trait HabitWrapper: erased_serde::Serialize { |
57 | fn remaining(&self, date: NaiveDate) -> u32; | 58 | fn remaining(&self, date: NaiveDate) -> u32; |
58 | fn total(&self) -> u32; | 59 | fn total(&self) -> u32; |
@@ -63,39 +64,39 @@ pub trait HabitWrapper: erased_serde::Serialize { | |||
63 | fn take_focus(&mut self, _: Direction) -> bool; | 64 | fn take_focus(&mut self, _: Direction) -> bool; |
64 | } | 65 | } |
65 | 66 | ||
66 | use erased_serde::serialize_trait_object; | 67 | macro_rules! auto_habit_impl { |
67 | serialize_trait_object!(HabitWrapper); | 68 | ($struct_name:ident) => { |
68 | 69 | #[typetag::serde] | |
69 | impl<T> HabitWrapper for T | 70 | impl HabitWrapper for $struct_name { |
70 | where | 71 | fn remaining(&self, date: NaiveDate) -> u32 { |
71 | T: Habit + ShadowView, | 72 | Habit::remaining(self, date) |
72 | T: Serialize, | 73 | } |
73 | T::HabitType: std::fmt::Display, | 74 | fn total(&self) -> u32 { |
74 | { | 75 | Habit::total(self) |
75 | fn remaining(&self, date: NaiveDate) -> u32 { | 76 | } |
76 | Habit::remaining(self, date) | 77 | fn modify(&mut self, date: NaiveDate, event: TrackEvent) { |
77 | } | 78 | Habit::modify(self, date, event); |
78 | fn total(&self) -> u32 { | 79 | } |
79 | Habit::total(self) | 80 | fn draw(&self, printer: &Printer) { |
80 | } | 81 | ShadowView::draw(self, printer) |
81 | fn modify(&mut self, date: NaiveDate, event: TrackEvent) { | 82 | } |
82 | Habit::modify(self, date, event); | 83 | fn on_event(&mut self, event: Event) -> EventResult { |
83 | } | 84 | ShadowView::on_event(self, event) |
84 | fn draw(&self, printer: &Printer) { | 85 | } |
85 | ShadowView::draw(self, printer) | 86 | fn required_size(&mut self, x: Vec2) -> Vec2 { |
86 | } | 87 | ShadowView::required_size(self, x) |
87 | fn on_event(&mut self, event: Event) -> EventResult { | 88 | } |
88 | ShadowView::on_event(self, event) | 89 | fn take_focus(&mut self, d: Direction) -> bool { |
89 | } | 90 | ShadowView::take_focus(self, d) |
90 | fn required_size(&mut self, x: Vec2) -> Vec2 { | 91 | } |
91 | ShadowView::required_size(self, x) | 92 | } |
92 | } | 93 | }; |
93 | fn take_focus(&mut self, d: Direction) -> bool { | ||
94 | ShadowView::take_focus(self, d) | ||
95 | } | ||
96 | } | 94 | } |
97 | 95 | ||
98 | #[derive(Debug, Serialize)] | 96 | auto_habit_impl!(Count); |
97 | auto_habit_impl!(Bit); | ||
98 | |||
99 | #[derive(Debug, Serialize, Deserialize)] | ||
99 | pub struct Count { | 100 | pub struct Count { |
100 | name: String, | 101 | name: String, |
101 | stats: HashMap<NaiveDate, u32>, | 102 | stats: HashMap<NaiveDate, u32>, |
@@ -170,7 +171,7 @@ impl Habit for Count { | |||
170 | } | 171 | } |
171 | } | 172 | } |
172 | 173 | ||
173 | #[derive(Debug, Serialize)] | 174 | #[derive(Debug, Serialize, Deserialize)] |
174 | pub struct Bit { | 175 | pub struct Bit { |
175 | name: String, | 176 | name: String, |
176 | stats: HashMap<NaiveDate, CustomBool>, | 177 | stats: HashMap<NaiveDate, CustomBool>, |