Cotton code
115 lines · 4.7 KB · RustRaw
//! The order a deck's "All" deals its cards in when it is ordered by what//! calls for attention now rather than by name (programme cardtrial, the//! `relevance` sort): first a yearly day coming within the week, then a//! contact gone quiet, then a record with open work linked to it, then the//! ones recently in touch, then the rest. A transform (contract 2): it reads//! typed day counts a definition's columns gave it, and names no table — the//! People deck feeds it a birthday, a last-reached day and its open todos and//! events, but any table with a yearly date or a last-contact date can./// One record's facts, as day counts against today.#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]pub struct AttentionFacts {    /// Days until a yearly date next comes round (0 is today).    pub yearly_ahead: Option<i64>,    /// Days since the record was last in touch; none when it never was.    pub since_touch: Option<i64>,    /// Open records linked to it (todos not done, events still to come).    pub open: u32,}/// Where the windows fall.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub struct AttentionWindows {    /// A yearly date at most this many days ahead calls.    pub soon: i64,    /// Quiet this many days or more, after having been in touch, calls.    pub quiet: i64,    /// But not past this: a contact quiet for years is the rest, not news.    pub quiet_until: i64,    /// In touch within this many days is recent.    pub recent: i64,}impl Default for AttentionWindows {    fn default() -> Self {        AttentionWindows { soon: 7, quiet: 42, quiet_until: 365, recent: 30 }    }}/// Why a record stands where it does, most pressing first.#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]pub enum Attention {    /// Its yearly day comes within the window; nearer first.    YearlySoon { ahead: i64 },    /// Gone quiet; the most recently lapsed first.    Quiet { since: i64 },    /// Open work linked to it; more first.    Open { open: std::cmp::Reverse<u32> },    /// Recently in touch; the most recent first.    Recent { since: i64 },    Rest,}impl Attention {    pub fn of(facts: AttentionFacts, windows: AttentionWindows) -> Attention {        if let Some(ahead) = facts.yearly_ahead.filter(|ahead| (0..=windows.soon).contains(ahead)) {            return Attention::YearlySoon { ahead };        }        if let Some(since) = facts.since_touch.filter(|since| (windows.quiet..=windows.quiet_until).contains(since)) {            return Attention::Quiet { since };        }        if facts.open > 0 {            return Attention::Open { open: std::cmp::Reverse(facts.open) };        }        if let Some(since) = facts.since_touch.filter(|since| *since <= windows.recent) {            return Attention::Recent { since };        }        Attention::Rest    }}/// Orders records by attention; ties keep the order they came in (the/// caller's name order), so the sort is stable.pub fn order<T>(records: &mut [T], facts: impl Fn(&T) -> AttentionFacts, windows: AttentionWindows) {    records.sort_by(|a, b| Attention::of(facts(a), windows).cmp(&Attention::of(facts(b), windows)));}#[cfg(test)]mod tests {    use super::*;    // The deck's "All" opens on whoever matters now: a birthday this week    // before a friend gone quiet, a friend gone quiet before someone with an    // open todo, those before someone you spoke to yesterday, and the many    // never reached (or lost for years) last, in their name order.    #[test]    fn all_is_dealt_by_what_calls_now() {        let person = |name: &'static str, yearly_ahead, since_touch, open| (name, AttentionFacts { yearly_ahead, since_touch, open });        let mut people = vec![            person("Aarav (never reached)", None, None, 0),            person("Bela (spoke yesterday)", Some(200), Some(1), 0),            person("Chitra (open todo)", None, Some(10), 1),            person("Dev (quiet 6 weeks)", None, Some(44), 0),            person("Esha (birthday in 5 days)", Some(5), Some(400), 0),            person("Farid (lost for years)", None, Some(900), 0),            person("Gita (birthday today)", Some(0), None, 0),            person("Hari (quiet 10 weeks)", None, Some(70), 2),        ];        order(&mut people, |(_, facts)| *facts, AttentionWindows::default());        let names: Vec<&str> = people.iter().map(|(name, _)| *name).collect();        assert_eq!(            names,            [                "Gita (birthday today)",                "Esha (birthday in 5 days)",                "Dev (quiet 6 weeks)",                "Hari (quiet 10 weeks)",                "Chitra (open todo)",                "Bela (spoke yesterday)",                "Aarav (never reached)",                "Farid (lost for years)",            ]        );    }}