//! The days a person can give a date in one tap, as typed choices. //! //! A transform over a day number (contract 2: it names no table and no //! column). The card's date corner opens the fan as one row of these: //! Today, Tomorrow, the seven weekdays after tomorrow, Next week, In 2 weeks, //! the first of each of the next two months, and Pick a date. The reading //! edge says each one in the database's `dates` and `editing` lines; Swift //! draws what it is handed and saves the day it carries. use crate::transforms::zoned_clock::{civil_from_days, day_key, days_from_civil, weekday_of_day_number}; use crate::records::page::DateWords; use crate::rules::shell_lines::{LineRow, LineSet}; use serde::Serialize; /// How many weekdays follow Tomorrow: a whole week of names, so the row /// runs from the day after tomorrow to tomorrow's weekday a week on. const WEEKDAYS_AFTER_TOMORROW: i64 = 7; /// How many months the row offers after the weeks. const MONTHS_AHEAD: i64 = 2; /// Monday, as `weekday_of_day_number` numbers it (0 is Sunday): "Next week" /// is the Monday after tomorrow, the start the When picker already uses. const MONDAY: i64 = 1; /// One choice and the day it sets. `Pick` carries no day: it opens a /// calendar. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum DateChoice { Today { day: i64 }, Tomorrow { day: i64 }, Weekday { day: i64 }, NextWeek { day: i64 }, InWeeks { weeks: i64, day: i64 }, Month { day: i64 }, Pick, } impl DateChoice { /// The day it sets, as a day number; none for Pick. pub fn day(self) -> Option { match self { Self::Today { day } | Self::Tomorrow { day } | Self::Weekday { day } | Self::NextWeek { day } | Self::InWeeks { day, .. } | Self::Month { day } => Some(day), Self::Pick => None, } } /// A stable key for the choice, the same across days ("today", /// "weekday-2" for the second weekday after tomorrow, "month-1"). fn key(self, index_in_kind: usize) -> String { match self { Self::Today { .. } => "today".into(), Self::Tomorrow { .. } => "tomorrow".into(), Self::Weekday { .. } => format!("weekday-{}", index_in_kind + 1), Self::NextWeek { .. } => "next-week".into(), Self::InWeeks { weeks, .. } => format!("in-{weeks}-weeks"), Self::Month { .. } => format!("month-{}", index_in_kind + 1), Self::Pick => "pick".into(), } } } /// The row of choices from `today` (a day number in the reader's zone). pub fn date_choices(today: i64) -> Vec { let tomorrow = today + 1; let mut choices = vec![DateChoice::Today { day: today }, DateChoice::Tomorrow { day: tomorrow }]; choices.extend((1..=WEEKDAYS_AFTER_TOMORROW).map(|n| DateChoice::Weekday { day: tomorrow + n })); let to_monday = (MONDAY - weekday_of_day_number(tomorrow)).rem_euclid(7); let next_monday = tomorrow + if to_monday == 0 { 7 } else { to_monday }; choices.push(DateChoice::NextWeek { day: next_monday }); choices.push(DateChoice::InWeeks { weeks: 2, day: today + 14 }); let (year, month, _) = civil_from_days(today); choices.extend((1..=MONTHS_AHEAD).map(|ahead| { let index = month - 1 + ahead; DateChoice::Month { day: days_from_civil(year + index.div_euclid(12), index.rem_euclid(12) + 1, 1) } })); choices.push(DateChoice::Pick); choices } /// A choice as the fan draws it: its words, the smaller line under them /// when the words alone do not say the day, and the day it sets. #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct SaidChoice { pub key: String, /// What the choice is, for the card's picture: `today`, `day`, `week`, /// `month` or `pick`. pub kind: &'static str, pub label: String, #[serde(skip_serializing_if = "Option::is_none")] pub detail: Option, /// The day it sets, `YYYY-MM-DD`; none for Pick. #[serde(skip_serializing_if = "Option::is_none")] pub day: Option, } /// The row said in a database's words: `dates` names the days and months, /// `editing` the rest. pub fn said_choices(today: i64, rows: &[LineRow]) -> Vec { let dates = DateWords::of_rows(rows); let editing = LineSet::of_rows("editing", rows).expect("the kit ships the editing set"); let say = |line: &str, fills: &[(&str, &str)]| editing.say(line, fills).unwrap_or_default(); let this_year = civil_from_days(today).0; let (mut weekdays, mut months) = (0, 0); date_choices(today) .into_iter() .map(|choice| { let index = match choice { DateChoice::Weekday { .. } => { weekdays += 1; weekdays - 1 } DateChoice::Month { .. } => { months += 1; months - 1 } _ => 0, }; let (kind, label, detail) = match choice { DateChoice::Today { day } => ("today", dates.heading(0).unwrap_or_default(), Some(dates.weekday_date(day, this_year))), DateChoice::Tomorrow { day } => ("day", dates.heading(1).unwrap_or_default(), Some(dates.weekday_date(day, this_year))), DateChoice::Weekday { day } => ("day", dates.weekday(weekday_of_day_number(day)), Some(dates.day_month(day, this_year))), DateChoice::NextWeek { .. } => ("week", say("when.next_week", &[]), None), DateChoice::InWeeks { weeks, .. } => ("week", say("when.in_weeks", &[("n", &weeks.to_string())]), None), DateChoice::Month { day } => { let (year, month, _) = civil_from_days(day); ("month", dates.month_title(year, month, this_year), None) } DateChoice::Pick => ("pick", say("when.pick", &[]), None), }; SaidChoice { key: choice.key(index), kind, label, detail, day: choice.day().map(day_key) } }) .collect() } #[cfg(test)] #[path = "date_choices_tests.rs"] mod tests;