aboutsummaryrefslogtreecommitdiff
path: root/src/habit.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-05-02 11:12:29 +0100
committerAkshay <[email protected]>2020-05-02 11:12:29 +0100
commit3b576d2c39e63f05c1c919579f61866a7798dc1e (patch)
treebb723fd387a394420dd3b6d767501f3c2dad90bd /src/habit.rs
parentb1581c83ff6157041b040cf7b05799dfb4f4a50b (diff)
typetag dosen't support generics, use macro hack until then
Diffstat (limited to 'src/habit.rs')
-rw-r--r--src/habit.rs69
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 @@
1use std::collections::HashMap; 1use std::collections::HashMap;
2 2
3use chrono::NaiveDate; 3use chrono::NaiveDate;
4use serde::Serialize; 4use serde::{Deserialize, Serialize};
5 5
6use cursive::direction::Direction; 6use cursive::direction::Direction;
7use cursive::event::{Event, EventResult}; 7use 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)]
19pub struct CustomBool(bool); 19pub struct CustomBool(bool);
20 20
21use std::fmt; 21use 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")]
56pub trait HabitWrapper: erased_serde::Serialize { 57pub 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
66use erased_serde::serialize_trait_object; 67macro_rules! auto_habit_impl {
67serialize_trait_object!(HabitWrapper); 68 ($struct_name:ident) => {
68 69 #[typetag::serde]
69impl<T> HabitWrapper for T 70 impl HabitWrapper for $struct_name {
70where 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)] 96auto_habit_impl!(Count);
97auto_habit_impl!(Bit);
98
99#[derive(Debug, Serialize, Deserialize)]
99pub struct Count { 100pub 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)]
174pub struct Bit { 175pub struct Bit {
175 name: String, 176 name: String,
176 stats: HashMap<NaiveDate, CustomBool>, 177 stats: HashMap<NaiveDate, CustomBool>,