DayRule#

Working time of one calendar date. A DayRule is the value type of Worktime::dates, keyed by chrono::NaiveDate. On that date the rule replaces the weekday rule from Worktime::days, in both directions: it can turn a working weekday off or a weekend day on.

Declaration#

#[derive(Clone, Debug)]
pub enum DayRule {
    Off,
    On,
    Hours(Vec<TimeRange>),
}

Variants#

Variant Description
Off Non-working date, such as a holiday.
On Working date with the calendar's regular hours (Worktime::hours).
Hours(Vec<TimeRange>) Working date with these hours instead of the regular ones. An empty list works like Off.

The ranges in Hours follow the TimeRange rules. An invalid range is rejected with Error::Input at the path calendars[i].worktime.dates[YYYY-MM-DD].

Example#

A calendar with a holiday, a working Saturday and a short day:

use std::collections::BTreeMap;

use chrono::NaiveDate;
use plen_core::{CalendarData, DayRule, TimeRange, Worktime};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let date = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).ok_or("invalid date");

    let mut dates = BTreeMap::new();
    dates.insert(date(2026, 12, 25)?, DayRule::Off);
    dates.insert(date(2026, 12, 19)?, DayRule::On);
    dates.insert(
        date(2026, 12, 24)?,
        DayRule::Hours(vec![TimeRange { start: 8 * 60, end: 12 * 60 }]),
    );

    let calendar = CalendarData {
        id: "global".into(),
        worktime: Some(Worktime { dates, ..Worktime::default() }),
    };
    println!("{calendar:?}");
    Ok(())
}

The example uses chrono for the date keys, so add it next to the library: cargo add chrono.