use chrono::{DateTime, Datelike, Local, TimeZone, Utc}; use chrono_tz::Tz; use serde::{Deserialize, Serialize}; #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] #[repr(u16)] pub enum CalendarEvent { Christmas = 0, } #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] pub struct Calendar { events: Vec, } impl Calendar { pub fn is_event(&self, event: CalendarEvent) -> bool { self.events.contains(&event) } pub fn events(&self) -> impl ExactSizeIterator + '_ { self.events.iter() } pub fn from_events(events: Vec) -> Self { Self { events } } pub fn from_tz(tz: Option) -> Self { let mut this = Self::default(); let now = match tz { Some(tz) => { let utc = Utc::now().naive_utc(); DateTime::::from_utc(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); } this } }