aboutsummaryrefslogtreecommitdiff
path: root/src/habit
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-02-24 06:27:37 +0000
committerAkshay <[email protected]>2021-02-24 06:27:37 +0000
commitfbcc966a3da8a75842c6b8843a9fd7f1edb0db15 (patch)
treebe2851277e689fec2f1629ca7a03bba1c8931446 /src/habit
parent3169cb6c8056b46a6f08533014366b8c441971f8 (diff)
add GoalKinds
- this allows for changing habit goals easily - easier to add new habits to dijo in newer versions
Diffstat (limited to 'src/habit')
-rw-r--r--src/habit/bit.rs4
-rw-r--r--src/habit/count.rs4
-rw-r--r--src/habit/traits.rs20
3 files changed, 24 insertions, 4 deletions
diff --git a/src/habit/bit.rs b/src/habit/bit.rs
index da64ece..c0d5f70 100644
--- a/src/habit/bit.rs
+++ b/src/habit/bit.rs
@@ -4,6 +4,7 @@ use std::default::Default;
4use chrono::NaiveDate; 4use chrono::NaiveDate;
5use serde::{Deserialize, Serialize}; 5use serde::{Deserialize, Serialize};
6 6
7use crate::command::GoalKind;
7use crate::habit::prelude::default_auto; 8use crate::habit::prelude::default_auto;
8use crate::habit::traits::Habit; 9use crate::habit::traits::Habit;
9use crate::habit::{InnerData, TrackEvent}; 10use crate::habit::{InnerData, TrackEvent};
@@ -66,6 +67,9 @@ impl Habit for Bit {
66 fn set_name(&mut self, n: impl AsRef<str>) { 67 fn set_name(&mut self, n: impl AsRef<str>) {
67 self.name = n.as_ref().to_owned(); 68 self.name = n.as_ref().to_owned();
68 } 69 }
70 fn kind(&self) -> GoalKind {
71 GoalKind::Bit
72 }
69 fn set_goal(&mut self, g: Self::HabitType) { 73 fn set_goal(&mut self, g: Self::HabitType) {
70 self.goal = g; 74 self.goal = g;
71 } 75 }
diff --git a/src/habit/count.rs b/src/habit/count.rs
index 09fd399..75b51cc 100644
--- a/src/habit/count.rs
+++ b/src/habit/count.rs
@@ -4,6 +4,7 @@ use std::default::Default;
4use chrono::NaiveDate; 4use chrono::NaiveDate;
5use serde::{Deserialize, Serialize}; 5use serde::{Deserialize, Serialize};
6 6
7use crate::command::GoalKind;
7use crate::habit::prelude::default_auto; 8use crate::habit::prelude::default_auto;
8use crate::habit::traits::Habit; 9use crate::habit::traits::Habit;
9use crate::habit::{InnerData, TrackEvent}; 10use crate::habit::{InnerData, TrackEvent};
@@ -42,6 +43,9 @@ impl Habit for Count {
42 fn set_name(&mut self, n: impl AsRef<str>) { 43 fn set_name(&mut self, n: impl AsRef<str>) {
43 self.name = n.as_ref().to_owned(); 44 self.name = n.as_ref().to_owned();
44 } 45 }
46 fn kind(&self) -> GoalKind {
47 GoalKind::Count(self.goal)
48 }
45 fn set_goal(&mut self, g: Self::HabitType) { 49 fn set_goal(&mut self, g: Self::HabitType) {
46 self.goal = g; 50 self.goal = g;
47 } 51 }
diff --git a/src/habit/traits.rs b/src/habit/traits.rs
index 24d941d..54ec9e1 100644
--- a/src/habit/traits.rs
+++ b/src/habit/traits.rs
@@ -2,10 +2,10 @@ use chrono::NaiveDate;
2use cursive::direction::Direction; 2use cursive::direction::Direction;
3use cursive::event::{Event, EventResult}; 3use cursive::event::{Event, EventResult};
4use cursive::{Printer, Vec2}; 4use cursive::{Printer, Vec2};
5
6use typetag; 5use typetag;
7 6
8use crate::habit::{Bit, Count, InnerData, TrackEvent}; 7use crate::command::GoalKind;
8use crate::habit::{Bit, Count, Float, InnerData, TrackEvent};
9use crate::views::ShadowView; 9use crate::views::ShadowView;
10 10
11pub trait Habit { 11pub trait Habit {
@@ -20,6 +20,7 @@ pub trait Habit {
20 fn remaining(&self, date: NaiveDate) -> u32; 20 fn remaining(&self, date: NaiveDate) -> u32;
21 fn set_goal(&mut self, goal: Self::HabitType); 21 fn set_goal(&mut self, goal: Self::HabitType);
22 fn set_name(&mut self, name: impl AsRef<str>); 22 fn set_name(&mut self, name: impl AsRef<str>);
23 fn kind(&self) -> GoalKind;
23 24
24 fn inner_data_ref(&self) -> &InnerData; 25 fn inner_data_ref(&self) -> &InnerData;
25 fn inner_data_mut_ref(&mut self) -> &mut InnerData; 26 fn inner_data_mut_ref(&mut self) -> &mut InnerData;
@@ -31,6 +32,7 @@ pub trait Habit {
31pub trait HabitWrapper: erased_serde::Serialize { 32pub trait HabitWrapper: erased_serde::Serialize {
32 fn draw(&self, printer: &Printer); 33 fn draw(&self, printer: &Printer);
33 fn goal(&self) -> u32; 34 fn goal(&self) -> u32;
35 fn kind(&self) -> GoalKind;
34 fn modify(&mut self, date: NaiveDate, event: TrackEvent); 36 fn modify(&mut self, date: NaiveDate, event: TrackEvent);
35 fn name(&self) -> String; 37 fn name(&self) -> String;
36 fn on_event(&mut self, event: Event) -> EventResult; 38 fn on_event(&mut self, event: Event) -> EventResult;
@@ -69,6 +71,9 @@ macro_rules! auto_habit_impl {
69 fn goal(&self) -> u32 { 71 fn goal(&self) -> u32 {
70 Habit::goal(self) 72 Habit::goal(self)
71 } 73 }
74 fn kind(&self) -> GoalKind {
75 Habit::kind(self)
76 }
72 fn modify(&mut self, date: NaiveDate, event: TrackEvent) { 77 fn modify(&mut self, date: NaiveDate, event: TrackEvent) {
73 Habit::modify(self, date, event); 78 Habit::modify(self, date, event);
74 } 79 }
@@ -88,5 +93,12 @@ macro_rules! auto_habit_impl {
88 }; 93 };
89} 94}
90 95
91auto_habit_impl!(Count); 96macro_rules! generate_implementations {
92auto_habit_impl!(Bit); 97 ($($x:ident),*) => (
98 $(
99 auto_habit_impl!($x);
100 )*
101 );
102}
103
104generate_implementations!(Count, Bit, Float);