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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use super::{Day, Goal, TimeFilter};
use crate::models::budget::Budget;
use crate::models::repetition::Repetition;
use chrono::NaiveDateTime;
use log::info;
use serde::{Deserialize, Deserializer};

impl From<String> for Day {
    fn from(day: String) -> Self {
        info!("From<String> day-string: {:?}", day);

        match day.to_lowercase().as_str() {
            "fri" => Day::Fri,
            "sat" => Day::Sat,
            "sun" => Day::Sun,
            "mon" => Day::Mon,
            "tue" => Day::Tue,
            "wed" => Day::Wed,
            "thu" => Day::Thu,
            _ => panic!("Invalid day selection"),
        }
    }
}

impl From<Day> for String {
    fn from(day: Day) -> Self {
        info!("From<Days> day: {:?}", day);
        match day {
            Day::Fri => "Fri".into(),
            Day::Sat => "Sat".into(),
            Day::Sun => "Sun".into(),
            Day::Mon => "Mon".into(),
            Day::Tue => "Tue".into(),
            Day::Wed => "Wed".into(),
            Day::Thu => "Thu".into(),
        }
    }
}

// Todo 2023-05-05  | Check all these setters - Why are they needed? Why public?
impl Goal {
    pub fn new(id: usize) -> Self {
        Self {
            id: id.to_string(),
            title: String::from("Test"),
            ..Default::default()
        }
    }

    pub fn title(mut self, title: &str) -> Self {
        self.title = title.to_string();
        self
    }

    pub fn duration(mut self, min_duration: usize) -> Self {
        self.min_duration = Some(min_duration);
        self
    }

    pub fn repeat(mut self, repetition: Repetition) -> Self {
        self.repeat = Some(repetition);
        self
    }

    pub fn start(mut self, start: NaiveDateTime) -> Self {
        self.start = Some(start);
        self
    }

    pub fn deadline(mut self, deadline: NaiveDateTime) -> Self {
        self.deadline = Some(deadline);
        self
    }

    pub fn deserialize_budget_vec<'de, D>(deserializer: D) -> Result<Option<Vec<Budget>>, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s: Option<Vec<Budget>> = Option::deserialize(deserializer)?;
        if let Some(s) = s {
            if !s.is_empty() {
                return Ok(Some(s));
            }
        }
        Ok(None)
    }
}

// imple Disply for TimeFilter
impl std::fmt::Display for TimeFilter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "TimeFilter [ after_time: {:?}, before_time: {:?}, on_days: {:?}, not_on: {:?} ]",
            self.after_time, self.before_time, self.on_days, self.not_on
        )
    }
}

#[cfg(test)]
mod tests {
    mod serializing_deserializing {
        use crate::models::budget::Budget;
        use crate::models::budget::BudgetType::Weekly;
        use crate::models::goal::Goal;

        #[test]
        fn deserializing_of_empty_budgetlist_produces_none() {
            let goal_deserialized: Goal = serde_json::from_str(
                r#"{    
                  "id": "1",
                  "title": "testing new api",
                  "budgets": []
                  }"#,
            )
            .unwrap();
            assert_eq!(goal_deserialized.budgets, None);
        }

        #[test]
        fn deserializing_of_non_empty_budgetlist_produces_correct_budgetlist() {
            let goal_deserialized: Goal = serde_json::from_str(
                r#"{    
                  "id": "1",
                  "title": "testing new api",
                  "budgets": [{
                    "budget_type": "Weekly",
                    "min": 40
                    }]
                  }"#,
            )
            .unwrap();
            assert_eq!(
                goal_deserialized.budgets,
                Some(vec![Budget {
                    budget_type: Weekly,
                    min: Some(40),
                    max: None
                }])
            );
        }

        // test that we can add a 'created_at' field without breaking the deserialization
        // the unwrap() would panic if it was impossible
        #[test]
        fn extra_fields_are_ignored() {
            let _: Goal = serde_json::from_str(
                r#"{    
                  "id": "1",
                  "title": "testing new api",
                  "created_at": "2023-09-03T10:38:35.505Z"
                  }"#,
            )
            .unwrap();
        }
    }
}