use chrono::Weekday;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ScheduleRule {
pub start_hour: u8,
pub end_hour: u8,
#[serde(default)]
pub days: Vec<Weekday>,
pub download_limit: Option<u64>,
pub upload_limit: Option<u64>,
}
impl ScheduleRule {
pub fn new(
start_hour: u8,
end_hour: u8,
days: Vec<Weekday>,
download_limit: Option<u64>,
upload_limit: Option<u64>,
) -> Self {
Self {
start_hour: start_hour.min(23),
end_hour: end_hour.min(23),
days,
download_limit,
upload_limit,
}
}
pub fn all_days(
start_hour: u8,
end_hour: u8,
download_limit: Option<u64>,
upload_limit: Option<u64>,
) -> Self {
Self::new(
start_hour,
end_hour,
Vec::new(),
download_limit,
upload_limit,
)
}
pub fn weekdays(
start_hour: u8,
end_hour: u8,
download_limit: Option<u64>,
upload_limit: Option<u64>,
) -> Self {
Self::new(
start_hour,
end_hour,
vec![
Weekday::Mon,
Weekday::Tue,
Weekday::Wed,
Weekday::Thu,
Weekday::Fri,
],
download_limit,
upload_limit,
)
}
pub fn weekends(
start_hour: u8,
end_hour: u8,
download_limit: Option<u64>,
upload_limit: Option<u64>,
) -> Self {
Self::new(
start_hour,
end_hour,
vec![Weekday::Sat, Weekday::Sun],
download_limit,
upload_limit,
)
}
pub fn matches(&self, hour: u8, weekday: Weekday) -> bool {
if !self.days.is_empty() && !self.days.contains(&weekday) {
return false;
}
if self.start_hour <= self.end_hour {
hour >= self.start_hour && hour <= self.end_hour
} else {
hour >= self.start_hour || hour <= self.end_hour
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct BandwidthLimits {
pub download: Option<u64>,
pub upload: Option<u64>,
}