Cotton code
99 lines · 3.8 KB · RustRaw
//! PACE: how pressing a record is, as one of five bands — a transform over a//! date or moment, a done state and a repeat (contract 1 and 2: typed values//! in, a typed form out; no table, no column, no words).//!//! A card draws its band as the paper's tint (the theme's `paces`) and as an//! animal from that band's plate set; the plate is `animal(record id)`, so a//! card keeps its animal while its pace holds and changes it only when the//! pace changes.use serde::Serialize;use crate::records::page::Moment;/// The bands, most pressing first.#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]#[serde(rename_all = "lowercase")]pub enum Pace {    /// Owed and behind: a day gone, or an hour passed today.    Late,    /// Owed today or within the next [`SOON_DAYS`] days.    Soon,    /// Owed later, or repeating with no day of its own.    Upcoming,    /// Owed by no day.    Someday,    /// Finished.    Done,}/// How many days after today still read as soon: today, tomorrow and the/// day after are the "alert" band; the day after that walks.pub const SOON_DAYS: i64 = 2;/// How many plates each band holds.pub const PLATES: u64 = 8;impl Pace {    /// A record's band from its owed moment (absent: none), the reader's    /// `now`, whether it is finished, and whether it repeats.    pub fn of(due: Option<Moment>, now: Moment, done: bool, repeats: bool) -> Pace {        if done {            return Pace::Done;        }        let Some(due) = due else {            return if repeats { Pace::Upcoming } else { Pace::Someday };        };        match due.day - now.day {            ahead if ahead < 0 => Pace::Late,            0 if due.timed() && due.minute < now.minute => Pace::Late,            ahead if ahead <= SOON_DAYS => Pace::Soon,            _ => Pace::Upcoming,        }    }}/// The plate a record draws within its band, `0..PLATES`: FNV-1a over the/// record's id, so every device and every build pick the same one.pub fn animal(record_id: &str) -> u8 {    let mut hash: u64 = 0xcbf2_9ce4_8422_2325;    for byte in record_id.as_bytes() {        hash ^= u64::from(*byte);        hash = hash.wrapping_mul(0x0000_0100_0000_01b3);    }    (hash % PLATES) as u8}#[cfg(test)]mod tests {    use super::*;    fn at(day: i64, minute: i64) -> Moment {        Moment { day, minute }    }    #[test]    fn the_bands_follow_the_owed_day() {        let now = at(100, 600);        assert_eq!(Pace::of(Some(at(94, 0)), now, false, false), Pace::Late);        assert_eq!(Pace::of(Some(at(100, 540)), now, false, false), Pace::Late, "an hour passed today");        assert_eq!(Pace::of(Some(at(100, 0)), now, false, false), Pace::Soon, "a bare day today is not late");        assert_eq!(Pace::of(Some(at(100, 700)), now, false, false), Pace::Soon);        assert_eq!(Pace::of(Some(at(102, 0)), now, false, false), Pace::Soon);        assert_eq!(Pace::of(Some(at(103, 0)), now, false, false), Pace::Upcoming);        assert_eq!(Pace::of(None, now, false, false), Pace::Someday);        assert_eq!(Pace::of(None, now, false, true), Pace::Upcoming);        assert_eq!(Pace::of(Some(at(94, 0)), now, true, false), Pace::Done);    }    // RULE: a record's plate is a pure function of its id — stable across    // devices, builds and reads — so a card never changes animal while its    // pace holds.    #[test]    fn the_plate_is_stable_and_spread() {        assert_eq!(animal("6f1c0b52-1f7e-4c55-9d64-3c1b2a9e8f01"), animal("6f1c0b52-1f7e-4c55-9d64-3c1b2a9e8f01"));        let plates: std::collections::BTreeSet<u8> = (0..64).map(|n| animal(&format!("todo-{n}"))).collect();        assert_eq!(plates.len(), PLATES as usize, "64 ids reach every plate");        assert!(plates.iter().all(|plate| u64::from(*plate) < PLATES));    }}