From fbcc966a3da8a75842c6b8843a9fd7f1edb0db15 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 24 Feb 2021 11:57:37 +0530 Subject: add GoalKinds - this allows for changing habit goals easily - easier to add new habits to dijo in newer versions --- src/habit/bit.rs | 4 ++++ src/habit/count.rs | 4 ++++ src/habit/traits.rs | 20 ++++++++++++++++---- 3 files changed, 24 insertions(+), 4 deletions(-) (limited to 'src/habit') 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; use chrono::NaiveDate; use serde::{Deserialize, Serialize}; +use crate::command::GoalKind; use crate::habit::prelude::default_auto; use crate::habit::traits::Habit; use crate::habit::{InnerData, TrackEvent}; @@ -66,6 +67,9 @@ impl Habit for Bit { fn set_name(&mut self, n: impl AsRef) { self.name = n.as_ref().to_owned(); } + fn kind(&self) -> GoalKind { + GoalKind::Bit + } fn set_goal(&mut self, g: Self::HabitType) { self.goal = g; } 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; use chrono::NaiveDate; use serde::{Deserialize, Serialize}; +use crate::command::GoalKind; use crate::habit::prelude::default_auto; use crate::habit::traits::Habit; use crate::habit::{InnerData, TrackEvent}; @@ -42,6 +43,9 @@ impl Habit for Count { fn set_name(&mut self, n: impl AsRef) { self.name = n.as_ref().to_owned(); } + fn kind(&self) -> GoalKind { + GoalKind::Count(self.goal) + } fn set_goal(&mut self, g: Self::HabitType) { self.goal = g; } 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; use cursive::direction::Direction; use cursive::event::{Event, EventResult}; use cursive::{Printer, Vec2}; - use typetag; -use crate::habit::{Bit, Count, InnerData, TrackEvent}; +use crate::command::GoalKind; +use crate::habit::{Bit, Count, Float, InnerData, TrackEvent}; use crate::views::ShadowView; pub trait Habit { @@ -20,6 +20,7 @@ pub trait Habit { fn remaining(&self, date: NaiveDate) -> u32; fn set_goal(&mut self, goal: Self::HabitType); fn set_name(&mut self, name: impl AsRef); + fn kind(&self) -> GoalKind; fn inner_data_ref(&self) -> &InnerData; fn inner_data_mut_ref(&mut self) -> &mut InnerData; @@ -31,6 +32,7 @@ pub trait Habit { pub trait HabitWrapper: erased_serde::Serialize { fn draw(&self, printer: &Printer); fn goal(&self) -> u32; + fn kind(&self) -> GoalKind; fn modify(&mut self, date: NaiveDate, event: TrackEvent); fn name(&self) -> String; fn on_event(&mut self, event: Event) -> EventResult; @@ -69,6 +71,9 @@ macro_rules! auto_habit_impl { fn goal(&self) -> u32 { Habit::goal(self) } + fn kind(&self) -> GoalKind { + Habit::kind(self) + } fn modify(&mut self, date: NaiveDate, event: TrackEvent) { Habit::modify(self, date, event); } @@ -88,5 +93,12 @@ macro_rules! auto_habit_impl { }; } -auto_habit_impl!(Count); -auto_habit_impl!(Bit); +macro_rules! generate_implementations { + ($($x:ident),*) => ( + $( + auto_habit_impl!($x); + )* + ); +} + +generate_implementations!(Count, Bit, Float); -- cgit v1.2.3