use serde::{Deserialize, Serialize};
pub(crate) const SECONDS_PER_NORMAL_DAY: u64 = 86_400;
pub(crate) const SECONDS_PER_NORMAL_WEEK: u64 = 7 * SECONDS_PER_NORMAL_DAY;
pub(crate) const MON_FRI: [bool; 7] = [true, true, true, true, true, false, false];
pub(crate) const MON_ONLY: [bool; 7] = [true, false, false, false, false, false, false];
pub(crate) const MON_THU: [bool; 7] = [true, true, true, true, false, false, false];
pub(crate) const FRI: [bool; 7] = [false, false, false, false, true, false, false];
pub(crate) const TUE_ONLY: [bool; 7] = [false, true, false, false, false, false, false];
pub(crate) const TUE_FRI: [bool; 7] = [false, true, true, true, true, false, false];
pub(crate) const SUN_ONLY: [bool; 7] = [false, false, false, false, false, false, true];
pub(crate) const SUN_PLUS_MON_THU: [bool; 7] = [true, true, true, true, false, false, true];
pub(crate) const ALL_DAYS: [bool; 7] = [true, true, true, true, true, true, true];
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct SessionRule {
pub days: [bool; 7],
pub open_ssm: u32,
pub close_ssm: u32,
}
impl SessionRule {
pub fn new(days: [bool; 7], open_ssm: u32, close_ssm: u32) -> Result<Self, SessionRuleError> {
let rule = Self {
days,
open_ssm,
close_ssm,
};
rule.validate()?;
Ok(rule)
}
pub fn validate(&self) -> Result<(), SessionRuleError> {
if self.open_ssm >= SECONDS_PER_NORMAL_DAY_U32 {
return Err(SessionRuleError::OpenOutOfRange {
open_ssm: self.open_ssm,
});
}
if self.close_ssm > SECONDS_PER_NORMAL_DAY_U32 {
return Err(SessionRuleError::CloseOutOfRange {
close_ssm: self.close_ssm,
});
}
if !self.days.iter().any(|&enabled| enabled) {
return Err(SessionRuleError::NoEnabledDays);
}
Ok(())
}
#[must_use]
pub const fn wraps_to_next_day(&self) -> bool {
self.open_ssm >= self.close_ssm
}
}
const SECONDS_PER_NORMAL_DAY_U32: u32 = 86_400;
const _: () = assert!(SECONDS_PER_NORMAL_DAY_U32 as u64 == SECONDS_PER_NORMAL_DAY);
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SessionRuleError {
OpenOutOfRange {
open_ssm: u32,
},
CloseOutOfRange {
close_ssm: u32,
},
NoEnabledDays,
}
impl core::fmt::Display for SessionRuleError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::OpenOutOfRange { open_ssm } => {
write!(f, "open_ssm {open_ssm} is outside 0..86400")
}
Self::CloseOutOfRange { close_ssm } => {
write!(f, "close_ssm {close_ssm} is outside 0..=86400")
}
Self::NoEnabledDays => write!(f, "no weekday is enabled"),
}
}
}
impl std::error::Error for SessionRuleError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SessionKind {
Regular,
Extended,
Both,
}
pub(crate) fn normal_week_rule_intervals(rule: &SessionRule) -> Vec<(u64, u64)> {
let mut intervals = Vec::with_capacity(8);
for (weekday, enabled) in (0_u64..).zip(rule.days) {
if !enabled {
continue;
}
let start = weekday
.saturating_mul(SECONDS_PER_NORMAL_DAY)
.saturating_add(u64::from(rule.open_ssm));
let duration = if rule.wraps_to_next_day() {
SECONDS_PER_NORMAL_DAY
.saturating_sub(u64::from(rule.open_ssm))
.saturating_add(u64::from(rule.close_ssm))
} else {
u64::from(rule.close_ssm - rule.open_ssm)
};
let end = start.saturating_add(duration);
if end <= SECONDS_PER_NORMAL_WEEK {
intervals.push((start, end));
} else {
intervals.push((start, SECONDS_PER_NORMAL_WEEK));
intervals.push((0, end.saturating_sub(SECONDS_PER_NORMAL_WEEK)));
}
}
intervals
}