From 3ded40d04f49983e7907366536dbc94917cee666 Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 14 Jul 2020 17:20:36 +0530 Subject: more boilerplate for auto-trackable habits add funding.yml also :^) --- src/habit/bit.rs | 10 +++++++++- src/habit/count.rs | 10 +++++++++- src/habit/prelude.rs | 4 ++++ src/habit/traits.rs | 28 +++++++++++++++++++--------- 4 files changed, 41 insertions(+), 11 deletions(-) (limited to 'src/habit') diff --git a/src/habit/bit.rs b/src/habit/bit.rs index 292b96a..3386182 100644 --- a/src/habit/bit.rs +++ b/src/habit/bit.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use chrono::NaiveDate; use serde::{Deserialize, Serialize}; +use crate::habit::prelude::default_auto; use crate::habit::traits::Habit; use crate::habit::{TrackEvent, ViewMode}; use crate::CONFIGURATION; @@ -37,6 +38,9 @@ pub struct Bit { stats: HashMap, goal: CustomBool, + #[serde(default = "default_auto")] + auto: bool, + #[serde(skip)] view_month_offset: u32, @@ -45,11 +49,12 @@ pub struct Bit { } impl Bit { - pub fn new(name: impl AsRef) -> Self { + pub fn new(name: impl AsRef, auto: bool) -> Self { return Bit { name: name.as_ref().to_owned(), stats: HashMap::new(), goal: CustomBool(true), + auto, view_month_offset: 0, view_mode: ViewMode::Day, }; @@ -114,4 +119,7 @@ impl Habit for Bit { fn view_mode(&self) -> ViewMode { self.view_mode } + fn is_auto(&self) -> bool { + self.auto + } } diff --git a/src/habit/count.rs b/src/habit/count.rs index a0e0aee..1bdf920 100644 --- a/src/habit/count.rs +++ b/src/habit/count.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use chrono::NaiveDate; use serde::{Deserialize, Serialize}; +use crate::habit::prelude::default_auto; use crate::habit::traits::Habit; use crate::habit::{TrackEvent, ViewMode}; @@ -12,6 +13,9 @@ pub struct Count { stats: HashMap, goal: u32, + #[serde(default = "default_auto")] + auto: bool, + #[serde(skip)] view_month_offset: u32, @@ -20,11 +24,12 @@ pub struct Count { } impl Count { - pub fn new(name: impl AsRef, goal: u32) -> Self { + pub fn new(name: impl AsRef, goal: u32, auto: bool) -> Self { return Count { name: name.as_ref().to_owned(), stats: HashMap::new(), goal, + auto, view_month_offset: 0, view_mode: ViewMode::Day, }; @@ -99,4 +104,7 @@ impl Habit for Count { fn view_mode(&self) -> ViewMode { self.view_mode } + fn is_auto(&self) -> bool { + self.auto + } } diff --git a/src/habit/prelude.rs b/src/habit/prelude.rs index 9196f00..b8b2bb2 100644 --- a/src/habit/prelude.rs +++ b/src/habit/prelude.rs @@ -18,3 +18,7 @@ impl std::default::Default for ViewMode { ViewMode::Day } } + +pub fn default_auto() -> bool { + false +} diff --git a/src/habit/traits.rs b/src/habit/traits.rs index e28e55d..5092bc5 100644 --- a/src/habit/traits.rs +++ b/src/habit/traits.rs @@ -26,6 +26,8 @@ pub trait Habit { fn set_view_mode(&mut self, mode: ViewMode); fn view_mode(&self) -> ViewMode; + + fn is_auto(&self) -> bool; } #[typetag::serde(tag = "type")] @@ -44,21 +46,15 @@ pub trait HabitWrapper: erased_serde::Serialize { fn set_view_mode(&mut self, mode: ViewMode); fn view_mode(&self) -> ViewMode; + + fn is_auto(&self) -> bool; } macro_rules! auto_habit_impl { ($struct_name:ident) => { #[typetag::serde] impl HabitWrapper for $struct_name { - fn remaining(&self, date: NaiveDate) -> u32 { - Habit::remaining(self, date) - } - fn goal(&self) -> u32 { - Habit::goal(self) - } - fn modify(&mut self, date: NaiveDate, event: TrackEvent) { - Habit::modify(self, date, event); - } + // ShadowView fn draw(&self, printer: &Printer) { ShadowView::draw(self, printer) } @@ -71,6 +67,17 @@ macro_rules! auto_habit_impl { fn take_focus(&mut self, d: Direction) -> bool { ShadowView::take_focus(self, d) } + + // Habit + fn remaining(&self, date: NaiveDate) -> u32 { + Habit::remaining(self, date) + } + fn goal(&self) -> u32 { + Habit::goal(self) + } + fn modify(&mut self, date: NaiveDate, event: TrackEvent) { + Habit::modify(self, date, event); + } fn get_name(&self) -> String { Habit::name(self) } @@ -86,6 +93,9 @@ macro_rules! auto_habit_impl { fn view_mode(&self) -> ViewMode { Habit::view_mode(self) } + fn is_auto(&self) -> bool { + Habit::is_auto(self) + } } }; } -- cgit v1.2.3