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
use chrono::{DateTime, Datelike, Local, TimeZone, Utc};
use chrono_tz::Tz;
use serde::{Deserialize, Serialize};
use strum::EnumIter;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, EnumIter)]
#[repr(u16)]
pub enum CalendarEvent {
    Christmas = 0,
    Halloween = 1,
    AprilFools = 2,
    Easter = 3,
}

#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Calendar {
    events: Vec<CalendarEvent>,
}

impl Calendar {
    pub fn is_event(&self, event: CalendarEvent) -> bool { self.events.contains(&event) }

    pub fn events(&self) -> impl ExactSizeIterator<Item = &CalendarEvent> + '_ {
        self.events.iter()
    }

    pub fn from_events(events: Vec<CalendarEvent>) -> Self { Self { events } }

    pub fn from_tz(tz: Option<Tz>) -> Self {
        let mut this = Self::default();

        let now = match tz {
            Some(tz) => {
                let utc = Utc::now().naive_utc();
                DateTime::<Tz>::from_naive_utc_and_offset(utc, tz.offset_from_utc_datetime(&utc))
                    .naive_local()
            },
            None => Local::now().naive_local(),
        };

        if now.month() == 12 && (20..=30).contains(&now.day()) {
            this.events.push(CalendarEvent::Christmas);
        }

        if now.month() == 10 && (24..=31).contains(&now.day()) {
            this.events.push(CalendarEvent::Halloween);
        }

        if now.month() == 4 && now.day() == 1 {
            this.events.push(CalendarEvent::AprilFools);
        }

        if now.month() == 3 && now.day() == 31 || now.month() == 4 && (1..=7).contains(&now.day()) {
            this.events.push(CalendarEvent::Easter);
        }

        this
    }
}