use crate::error::Error;
use crate::model::BlockStatus;
use crate::tags::{Role, Selector};
use crate::time::{BlackoutSpec, Date, Duration, RuleZone, TimeOfDay, Weekday, WindowSpec};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Rule {
pub id: String,
pub kind: RuleKind,
pub who: Selector,
pub when: Selector,
}
impl Rule {
#[must_use]
pub fn new(id: impl Into<String>, kind: RuleKind) -> Self {
Self {
id: id.into(),
kind,
who: Selector::Role(Role::Host),
when: Selector::True,
}
}
#[must_use]
pub fn with_id(mut self, id: impl Into<String>) -> Self {
self.id = id.into();
self
}
#[must_use]
pub fn with_who(mut self, who: Selector) -> Self {
self.who = who;
self
}
#[must_use]
pub fn with_when(mut self, when: Selector) -> Self {
self.when = when;
self
}
#[must_use]
pub fn and_who(mut self, selector: Selector) -> Self {
let existing = core::mem::replace(&mut self.who, Selector::True);
self.who = match existing {
Selector::True => selector,
Selector::All(mut clauses) => {
clauses.push(selector);
Selector::All(clauses)
}
other => Selector::all([other, selector]),
};
self
}
#[must_use]
pub fn except_who(self, selector: Selector) -> Self {
self.and_who(Selector::not(selector))
}
#[must_use]
pub fn unless(mut self, selector: Selector) -> Self {
let excluded = Selector::not(selector);
let existing = core::mem::replace(&mut self.when, Selector::True);
self.when = if matches!(existing, Selector::True) {
excluded
} else {
Selector::all([existing, excluded])
};
self
}
#[must_use]
pub fn window(&self) -> Option<&WindowSpec> {
match &self.kind {
RuleKind::Window(spec) => Some(spec),
_ => None,
}
}
#[must_use]
pub fn blackout(&self) -> Option<&BlackoutSpec> {
match &self.kind {
RuleKind::Blackout(spec) => Some(spec),
_ => None,
}
}
#[must_use]
pub fn clearance(&self) -> Option<&Clearance> {
match &self.kind {
RuleKind::Clearance(clearance) => Some(clearance),
_ => None,
}
}
#[must_use]
pub fn lead_time(&self) -> Option<&LeadTime> {
match &self.kind {
RuleKind::LeadTime(lead_time) => Some(lead_time),
_ => None,
}
}
#[must_use]
pub fn status_policy(&self) -> Option<&StatusPolicy> {
match &self.kind {
RuleKind::StatusPolicy(policy) => Some(policy),
_ => None,
}
}
#[must_use]
pub fn rest(&self) -> Option<&Rest> {
match &self.kind {
RuleKind::Rest(rest) => Some(rest),
_ => None,
}
}
#[must_use]
pub fn cap(&self) -> Option<&Cap> {
match &self.kind {
RuleKind::Cap(cap) => Some(cap),
_ => None,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum RuleKind {
Window(WindowSpec),
Blackout(BlackoutSpec),
Clearance(Clearance),
LeadTime(LeadTime),
StatusPolicy(StatusPolicy),
Rest(Rest),
Cap(Cap),
}
impl RuleKind {
#[must_use]
pub const fn as_str(&self) -> &'static str {
match self {
Self::Window(_) => "window",
Self::Blackout(_) => "blackout",
Self::Clearance(_) => "clearance",
Self::LeadTime(_) => "lead_time",
Self::StatusPolicy(_) => "status_policy",
Self::Rest(_) => "rest",
Self::Cap(_) => "cap",
}
}
}
impl core::fmt::Display for RuleKind {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Cap {
pub count: Selector,
pub measure: CapMeasure,
pub limit: u32,
pub period: CapPeriod,
pub per: CapPer,
pub zone: RuleZone,
pub week_starts: Weekday,
}
impl Cap {
#[must_use]
pub const fn new(
count: Selector,
measure: CapMeasure,
limit: u32,
period: CapPeriod,
per: CapPer,
zone: RuleZone,
week_starts: Weekday,
) -> Self {
Self {
count,
measure,
limit,
period,
per,
zone,
week_starts,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum CapMeasure {
Count,
Minutes,
}
impl CapMeasure {
#[must_use]
pub const fn as_str(&self) -> &'static str {
match self {
Self::Count => "count",
Self::Minutes => "minutes",
}
}
}
impl core::fmt::Display for CapMeasure {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CapPeriod {
Day,
Week,
Month,
}
impl CapPeriod {
#[must_use]
pub const fn as_str(&self) -> &'static str {
match self {
Self::Day => "day",
Self::Week => "week",
Self::Month => "month",
}
}
}
impl core::fmt::Display for CapPeriod {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum CapPer {
Each,
Pooled,
}
impl CapPer {
#[must_use]
pub const fn as_str(&self) -> &'static str {
match self {
Self::Each => "each",
Self::Pooled => "pooled",
}
}
}
impl core::fmt::Display for CapPer {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Rest {
pub late_after: TimeOfDay,
pub not_before: TimeOfDay,
}
impl Rest {
#[must_use]
pub const fn new(late_after: TimeOfDay, not_before: TimeOfDay) -> Self {
Self {
late_after,
not_before,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Clearance {
pub before: Duration,
pub after: Duration,
pub min_gap: Duration,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct LeadTime {
pub min: Duration,
pub max: Option<LeadTimeMax>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LeadTimeMax {
Duration(Duration),
Days(u32),
WorkingDays {
count: u32,
calendar: String,
},
Date(Date),
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct StatusPolicy {
pub entries: Vec<StatusPolicyEntry>,
}
impl StatusPolicy {
#[must_use]
pub fn decide(&self, status: BlockStatus, tags: &[crate::tags::Tag]) -> Option<StatusEffect> {
self.entries
.iter()
.find(|entry| entry.matches(status, tags))
.map(|entry| entry.effect)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StatusPolicyEntry {
pub status: Option<BlockStatus>,
pub tags: Option<Selector>,
pub effect: StatusEffect,
}
impl StatusPolicyEntry {
#[must_use]
pub fn status(status: BlockStatus, effect: StatusEffect) -> Self {
Self {
status: Some(status),
tags: None,
effect,
}
}
#[must_use]
pub fn tags(tags: Selector, effect: StatusEffect) -> Self {
Self {
status: None,
tags: Some(tags),
effect,
}
}
#[must_use]
pub fn status_and_tags(status: BlockStatus, tags: Selector, effect: StatusEffect) -> Self {
Self {
status: Some(status),
tags: Some(tags),
effect,
}
}
#[must_use]
pub fn matches(&self, status: BlockStatus, tags: &[crate::tags::Tag]) -> bool {
if self.status.is_some_and(|matched| matched != status) {
return false;
}
self.tags
.as_ref()
.is_none_or(|selector| selector.matches_item(tags))
}
pub fn validate(&self) -> Result<(), Error> {
if self.status.is_none() && self.tags.is_none() {
return Err(Error::StatusPolicyEntryEmpty);
}
Ok(())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum StatusEffect {
Ignore,
Block,
}
impl StatusEffect {
#[must_use]
pub const fn as_str(&self) -> &'static str {
match self {
Self::Ignore => "ignore",
Self::Block => "block",
}
}
}
impl core::fmt::Display for StatusEffect {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}