cal-core 0.2.158

Callable core lib
Documentation
use std::fmt::{Debug, Formatter};
use serde::{Serialize, Deserialize};
use chrono::{Datelike, Local};
use crate::device::device::Connector;
use crate::{ConnectState, FlowState};

#[derive(Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct DayOfWeekRouter {
    #[serde(default)]
    pub days: Vec<u32>,
    #[serde(default = "crate::device::shared::build_default_timezone")]
    pub timezone: String,
    #[serde(default = "crate::device::shared::build_connect_to")]
    pub on_match: String,
    #[serde(default = "crate::device::shared::build_connect_to")]
    pub connect_to: String,
}

impl Connector for DayOfWeekRouter {
    fn get_connect_to(&mut self, _state: &mut FlowState) -> ConnectState {
        let now = Local::now().naive_local();
        let within = self.days.contains(&now.day());
        if within {
            ConnectState::matched(self.on_match.as_str(), Some(now.to_string()), None)
        } else {
            ConnectState::default(self.connect_to.as_str(), Some(now.to_string()))
        }
    }
}

impl Debug for DayOfWeekRouter {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DayOfWeekRouter")
            .field("days", &self.days)
            .field("timezone", &self.timezone)
            .field("on_match", &self.on_match)
            .field("connect_to", &self.connect_to)
            .finish()
    }
}