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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
use crate::types::*;
use chrono::prelude::Local;
use chrono::Datelike;
use chrono::{DateTime as chronoDateTime, Timelike};
use struct_iterable::Iterable;
use crate::utils::compare_dyn_any_values;

macro_rules! impl_eval_fns {
    ($struct:ident) => {
        impl crate::types::datekindEvals for $struct {
            /// Method pass Date or DateTime and returns true if Date or DateTime's year field is a leap year
            fn isLeapYear(&self) -> bool {
                if (self.year % 4 == 0 && self.year % 100 != 0) || self.year % 400 == 0 {
                    return true;
                }
                false
            }
            /// Method returns the day of the week as a String of the Date or DateTime passed to it.
            fn weekday(&self) -> Result<String, std::io::Error> {
                let weekdays: Vec<&str> = vec![
                    "Sunday",
                    "Monday",
                    "Tuesday",
                    "Wednesday",
                    "Thursday",
                    "Friday",
                    "Saturday",
                ];
                return Ok(weekdays[self
                    .weekday_as_int()
                    .expect("Error converting date to week number")
                    as usize]
                    .to_string());
            }
            /// Method returns the day of the week as a i8 with 0 being Sunday
            fn weekday_as_int(&self) -> Result<i8, std::io::Error> {
                let first_two_digits_year: i32 = self.year as i32 % 100;
                let mut num: i8 = ((self.day
                    + ((13
                        * (if self.month == 1 || self.month == 2 {
                            self.month + 10
                        } else {
                            self.month - 2
                        })
                        - 1)
                        / 5)
                    + first_two_digits_year as i8
                    + (first_two_digits_year as i8 / 4)
                    + (self
                        .last_two_digits_year()
                        .parse::<i8>()
                        .expect("Failed to unwrap last two digits to i8")
                        / 4)
                    - 2 * self
                        .last_two_digits_year()
                        .parse::<i8>()
                        .expect("Failed to unwrap last two digits to i8"))
                    % 7
                    + 1);
                if num == 7 {
                    num = 0;
                }
                return Ok(num);
            }
            /// returns true if both params share the same day
            fn sharesDay(&self, date2: &$struct) -> bool {
                if self.day == date2.day {
                    return true;
                }
                false
            }
            /// returns true if both params share the same year
            fn sharesYear(&self, date2: &$struct) -> bool {
                if self.year == date2.year {
                    return true;
                }
                false
            }
            /// returns true if both params share the same month
            fn sharesMonth(&self, date2: &$struct) -> bool {
                if self.month == date2.month {
                    return true;
                }
                false
            }
        }
    };
}
impl Date {
    pub fn is_valid(&self) -> bool {
        let month_lengths: std::collections::HashMap<i32, i32> = std::collections::HashMap::from([
            (1, 31),
            (2, if self.isLeapYear() { 29 } else { 28 }),
            (3, 31),
            (4, 30),
            (5, 31),
            (6, 30),
            (7, 31),
            (8, 31),
            (9, 30),
            (10, 31),
            (11, 30),
            (12, 31),
        ]);
        if self.day > 0 && self.day <= *month_lengths.get(&2).unwrap() as i8 && self.month > 0 && self.month < 13 && self.year % 1 == 0 {
            true
        } else {
            false
        }
    }
    /// Returns a Vector of &str shared by each Date in Vector params (Returns the same as allShare, just different implementation of it.)
    pub fn allShareEL(vec: Vec<Date>) -> Vec<&'static str> {
        let mut terms: Vec<&'static str> = vec!["day", "month", "year"];
        let mut shared_terms: Vec<&'static str> = vec![];
        let base = Date {
            day: vec.get(0).expect("Vec has length 0").day,
            month: vec.get(0).unwrap().month,
            year: vec.get(0).unwrap().year,

        };
        for date in vec {
            for ((field_name, field_value), (_, base_value)) in date.iter().zip(base.iter()) {
                if !compare_dyn_any_values(field_value, base_value).unwrap() {
                    if let Some(index) = terms.iter().position(|&x| x == field_name) {
                        shared_terms.push(terms[index]);
                        terms.remove(terms.iter().position(|&x| x == field_name).unwrap());
                        }
                }
            }
        }
    terms
}   
    /// Returns a Vector of the shared fields in each Date from Vector
    pub fn allShare(vec: Vec<Date>) -> Vec<&'static str> {
        let mut shares_terms: Vec<&'static str> = vec!["day", "month", "year"];
        let (day, month, year) = (vec.get(0).expect("Date Vector has no terms").day, vec.get(0).unwrap().month, vec.get(0).unwrap().year);
        for date in vec {
            if date.day != day {
                if let Some(index) = shares_terms.iter().position(|&x| x == "day") {
                shares_terms.remove(index);
                }
            }
            if date.month != month{
                if let Some(index) = shares_terms.iter().position(|&x| x == "month") {
                shares_terms.remove(index);
                }
            }
            if date.year != year {
                if let Some(index) = shares_terms.iter().position(|&x| x == "year") {
                shares_terms.remove(index);
                }
            }
        }
        shares_terms
    }
    /// Takes a snapshot of the current date in Local time
    pub fn snapshot_date() -> crate::types::Date {
        let local: chronoDateTime<Local> = Local::now();
        crate::types::Date {
            day: local.day() as i8,
            month: local.month() as i8,
            year: local.year() as i16,
        }
    }
    /// same function of sharesDay, sharesMonth, sharesYear, but adds comparison field as a param.
    pub fn DateShares(&self, datetime2: &Date, compare_type: &str) -> Result<bool, &str> {
        match compare_type {
            "day" => {
                if self.day == datetime2.day {
                    Ok(true)
                } else {
                    Ok(false)
                }
            }
            "month" => {
                if self.month == datetime2.month {
                    Ok(true)
                } else {
                    Ok(false)
                }
            }
            "year" => {
                if self.year == datetime2.year {
                    Ok(true)
                } else {
                    Ok(false)
                }
            }
            &_ => return Err("Invalid compare type"),
        }
    }
    /// takes self and Date fields and returns true if self is after the second, date, and false if not.
    pub fn is_after(&self, date: Date) -> bool {
        if compare_nums(self.year, date.year) == two_nums::larger {
            true
        } else if compare_nums(self.year, date.year) == two_nums::smaller {
            false
        } else if compare_nums(self.month as i16, date.month as i16) == two_nums::larger {
            true
        } else if compare_nums(self.month as i16, date.month as i16) == two_nums::smaller {
            false
        } else if compare_nums(self.day as i16, date.day as i16) == two_nums::larger {
            true
        } else if compare_nums(self.day as i16, date.day as i16) == two_nums::smaller {
            false
        } else {
            false
        }
    }
    /// The reverse of is_after
    pub fn is_before(&self, date: Date) -> bool {
        !self.is_after(date)
    }
}
fn compare_nums(first: i16, second: i16) -> two_nums {
    match first > second {
        true => two_nums::larger,
        false => {
            if first < second {
                two_nums::smaller
            } else {
                two_nums::equal
            }
        }
    }
}

impl DateTime {
    /// Checks if a DateTime is a valid day
    pub fn is_valid(&self) -> bool {
        if (Date{day: self.day, month: self.month, year: self.year}).is_valid() && self.second >= 0 && self.second < 60 && self.minute >= 0 && self.minute < 60 && self.hour > 0 && self.hour < 24{
            true
        } else {
            false
        }
    }
    /// Takes a Vector of DateTimes and returns all field values they share as a vector of &str. (Same function  of allShare just different implementation)
    pub fn allShareEL(vec: Vec<DateTime>) -> Vec<&'static str> {
        let mut terms: Vec<&'static str> = vec!["second", "minute", "hour", "day", "month", "year"];
        let mut shared_terms: Vec<&'static str> = vec![];
        let base = DateTime {
            second: vec.get(0).expect("Date Vector has no terms").second,
            minute: vec.get(0).unwrap().minute,
            hour: vec.get(0).unwrap().hour,
            day: vec.get(0).unwrap().day,
            month: vec.get(0).unwrap().month,
            year: vec.get(0).unwrap().year,

        };
        for date in vec {
            for ((field_name, field_value), (_, base_value)) in date.iter().zip(base.iter()) {
                if !compare_dyn_any_values(field_value, base_value).unwrap() {
                    if let Some(index) = terms.iter().position(|&x| x == field_name) {
                        shared_terms.push(terms[index]);
                        terms.remove(terms.iter().position(|&x| x == field_name).unwrap());
                        }
                }
            }
        }
    terms
}   
/// Takes a Vector of DateTimes and returns a Vector of &strs of the field values they share
pub fn allShare(vec: Vec<DateTime>) -> Vec<&'static str> {
    let mut shares_terms: Vec<&'static str> = vec!["second","minute","hour","day", "month", "year"];
    let (second, minute, hour, day, month, year) = (vec.get(0).expect("Date Vector has no terms").second, vec.get(0).unwrap().minute, vec.get(0).unwrap().hour, vec.get(0).unwrap().day, vec.get(0).unwrap().month, vec.get(0).unwrap().year);
    for date in vec {
        if date.second != second {
            if let Some(index) = shares_terms.iter().position(|&x| x == "second") {
            shares_terms.remove(index);
            }
        }
        if date.minute != minute {
            if let Some(index) = shares_terms.iter().position(|&x| x == "minute") {
            shares_terms.remove(index);
            }
        }
        if date.hour != hour {
            if let Some(index) = shares_terms.iter().position(|&x| x == "hour") {
            shares_terms.remove(index);
            }
        }
        if date.day != day {
            if let Some(index) = shares_terms.iter().position(|&x| x == "day") {
            shares_terms.remove(index);
            }
        }
        if date.month != month{
            if let Some(index) = shares_terms.iter().position(|&x| x == "month") {
            shares_terms.remove(index);
            }
        }
        if date.year != year {
            if let Some(index) = shares_terms.iter().position(|&x| x == "year") {
            shares_terms.remove(index);
            }
        }
    }
    shares_terms
}
/// Takes a snapshot of the current local DateTime as a DateTime
    pub fn snapshot_datetime() -> crate::types::DateTime {
        let local: chronoDateTime<Local> = Local::now();
        crate::types::DateTime {
            second: local.second() as i8,
            minute: local.minute() as i8,
            hour: local.hour() as i8,
            day: local.day() as i8,
            month: local.month() as i8,
            year: local.year() as i16,
        }
    }
    /// Returns true if two DateTimes passed share the same second value
    pub fn sharesSecond(&self, datetime2: DateTime) -> bool {
        if self.second == datetime2.second {
            return true;
        }
        false
    }
    /// Returns true if two DateTimes passed share the same minute value
    pub fn sharesMinute(&self, datetime2: DateTime) -> bool {
        if self.minute == datetime2.minute {
            return true;
        }
        false
    }
    /// Returns true if two DateTimes passed share the same hour value
    pub fn sharesHour(&self, datetime2: DateTime) -> bool {
        if self.hour == datetime2.hour {
            return true;
        }
        false
    }
    /// Returns true if two DateTimes passed share the same compare_type passed
    pub fn DateTimeShares(&self, datetime2: &Self, compare_type: &str) -> Result<bool, &str> {
        match compare_type {
            "second" => {
                if self.second == datetime2.second {
                    Ok(true)
                } else {
                    Ok(false)
                }
            }
            "minute" => {
                if self.minute == datetime2.minute {
                    Ok(true)
                } else {
                    Ok(false)
                }
            }
            "hour" => {
                if self.hour == datetime2.hour {
                    Ok(true)
                } else {
                    Ok(false)
                }
            }
            "day" => {
                if self.day == datetime2.day {
                    Ok(true)
                } else {
                    Ok(false)
                }
            }
            "month" => {
                if self.month == datetime2.month {
                    Ok(true)
                } else {
                    Ok(false)
                }
            }
            "year" => {
                if self.year == datetime2.year {
                    Ok(true)
                } else {
                    Ok(false)
                }
            }
            &_ => {
                return Err("Invalid compare type");
            }
        }
    }
}

impl_eval_fns!(Date);
impl_eval_fns!(DateTime);