vpf-runner 0.4.1

The reference runner of the Verifiable Plan Format (VPF): runs a plan step by step and proves each step.
Documentation
//! When runs start: on a schedule, every so many minutes, or when files change.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use jiff::civil::{Date, Time, Weekday};
use jiff::{Timestamp, Zoned};
use vpf::{Day, Plan};

use crate::folder;

/// The dates between which runs start: none before `from`, none after `until`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Window {
    /// No run starts before it.
    pub from: Option<Timestamp>,
    /// No run starts after it.
    pub until: Option<Timestamp>,
}

impl Window {
    /// The dates a plan's start names. A checked plan writes them as dates and times with their offset.
    pub fn of(plan: &Plan) -> Self {
        let read = |text: Option<&str>| text.and_then(|text| text.trim().parse::<Timestamp>().ok());
        Self {
            from: read(plan.runs_from()),
            until: read(plan.runs_until()),
        }
    }

    /// Whether a run that would start at `at` is too late.
    pub fn over(&self, at: Timestamp) -> bool {
        self.until.is_some_and(|until| at > until)
    }
}

fn weekday(day: Day) -> Weekday {
    match day {
        Day::Mon => Weekday::Monday,
        Day::Tue => Weekday::Tuesday,
        Day::Wed => Weekday::Wednesday,
        Day::Thu => Weekday::Thursday,
        Day::Fri => Weekday::Friday,
        Day::Sat => Weekday::Saturday,
        Day::Sun => Weekday::Sunday,
    }
}

/// The next time after `now` that falls on one of the days of the week or of the month, at the time of day
/// `HH:MM`, in `now`'s time zone. A day of the month past the end of a month falls on its last day, once.
///
/// # Panics
///
/// When `time` is not `HH:MM`, or there are neither days nor dates, which a checked plan never has.
pub fn next_schedule(days: &[Day], dates: &[i64], time: &str, now: &Zoned) -> Zoned {
    let (hour, minute) = time.split_once(':').expect("a time as HH:MM");
    let at = Time::new(
        hour.parse().expect("an hour"),
        minute.parse().expect("a minute"),
        0,
        0,
    )
    .expect("a time of day");
    let wanted: Vec<Weekday> = days.iter().copied().map(weekday).collect();
    let on_a_date = |date: Date| {
        let last = i64::from(date.days_in_month());
        dates
            .iter()
            .any(|&wanted| wanted.min(last) == i64::from(date.day()))
    };
    // Every day of the month comes within two months.
    (0..=62)
        .filter_map(|ahead| now.date().checked_add(jiff::Span::new().days(ahead)).ok())
        .filter(|date| wanted.contains(&date.weekday()) || on_a_date(*date))
        .filter_map(|date| date.to_datetime(at).to_zoned(now.time_zone().clone()).ok())
        .find(|start| start > now)
        .expect("a day within two months")
}

/// What the files that match the start's paths hold, to see when one changes.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Snapshot(BTreeMap<PathBuf, u64>);

impl Snapshot {
    /// What the matching files hold now.
    pub fn take(base: &Path, paths: &[String]) -> Self {
        Self(folder::contents(base, paths))
    }
}