aboutsummaryrefslogtreecommitdiff
path: root/src/habit/traits.rs
blob: 54ec9e1d05eadf7d81afd9fb4c5869d2678bb84c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use chrono::NaiveDate;
use cursive::direction::Direction;
use cursive::event::{Event, EventResult};
use cursive::{Printer, Vec2};
use typetag;

use crate::command::GoalKind;
use crate::habit::{Bit, Count, Float, InnerData, TrackEvent};
use crate::views::ShadowView;

pub trait Habit {
    type HabitType;

    fn get_by_date(&self, date: NaiveDate) -> Option<&Self::HabitType>;
    fn goal(&self) -> u32;
    fn insert_entry(&mut self, date: NaiveDate, val: Self::HabitType);
    fn modify(&mut self, date: NaiveDate, event: TrackEvent);
    fn name(&self) -> String;
    fn reached_goal(&self, date: NaiveDate) -> bool;
    fn remaining(&self, date: NaiveDate) -> u32;
    fn set_goal(&mut self, goal: Self::HabitType);
    fn set_name(&mut self, name: impl AsRef<str>);
    fn kind(&self) -> GoalKind;

    fn inner_data_ref(&self) -> &InnerData;
    fn inner_data_mut_ref(&mut self) -> &mut InnerData;

    fn is_auto(&self) -> bool;
}

#[typetag::serde(tag = "type")]
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;
    fn remaining(&self, date: NaiveDate) -> u32;
    fn required_size(&mut self, _: Vec2) -> Vec2;
    fn take_focus(&mut self, _: Direction) -> bool;

    fn inner_data_ref(&self) -> &InnerData;
    fn inner_data_mut_ref(&mut self) -> &mut InnerData;

    fn is_auto(&self) -> bool;
}

macro_rules! auto_habit_impl {
    ($struct_name:ident) => {
        #[typetag::serde]
        impl HabitWrapper for $struct_name {
            // ShadowView
            fn draw(&self, printer: &Printer) {
                ShadowView::draw(self, printer)
            }
            fn on_event(&mut self, event: Event) -> EventResult {
                ShadowView::on_event(self, event)
            }
            fn required_size(&mut self, x: Vec2) -> Vec2 {
                ShadowView::required_size(self, x)
            }
            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 kind(&self) -> GoalKind {
                Habit::kind(self)
            }
            fn modify(&mut self, date: NaiveDate, event: TrackEvent) {
                Habit::modify(self, date, event);
            }
            fn name(&self) -> String {
                Habit::name(self)
            }
            fn inner_data_ref(&self) -> &InnerData {
                Habit::inner_data_ref(self)
            }
            fn inner_data_mut_ref(&mut self) -> &mut InnerData {
                Habit::inner_data_mut_ref(self)
            }
            fn is_auto(&self) -> bool {
                Habit::is_auto(self)
            }
        }
    };
}

macro_rules! generate_implementations {
    ($($x:ident),*) => (
        $(
            auto_habit_impl!($x);
        )*
    );
}

generate_implementations!(Count, Bit, Float);