//! A Mac's off-Mac backup as ONE typed state and ONE line, read through the//! board's words, without its diagnostics.//!//! Every reader says the same fact the same way (walker pass 4, row 10: Me said//! "Nothing has been backed up off your Mac yet", the Feedback list "mac-c-dev//! has stopped backing up", the Feedback page the Mac's script line): Home's//! notice and the device page read a backup receipt, the Feedback list and page//! read the notice the Mac filed; all of them come here for the state and its//! line. The Mac's own reason is never in the line — it is on the Mac's page.use chrono::{DateTime, Utc};use serde_json::{Map, Value};/// A backup that is not getting through.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pubenumBackupState {/// The copy stopped getting through; `last_copy` is the last one that did/// (or when the Mac says it fell behind), `None` when nobody knows.Stopped { last_copy: Option<DateTime<Utc>> },/// A bucket is named but its settings do not add up.HalfSetUp,}implBackupState {/// A backup receipt's state (`methods/host_settings.rs` `backup_section`):/// failing, whole or in any one database, is stopped; never with a reason/// is half set up; anything else is getting through (or off) and says nothing.pubfn of_receipt(backup: &Value) -> Option<BackupState> {let failing = backup["state"] == "failing" || backup["databases"].as_array().is_some_and(|rows| rows.iter().any(|row| row["state"] == "failing"));if !failing {let half = backup["state"] == "never" && backup["lastError"].as_str().is_some_and(|why| !why.is_empty());return half.then_some(BackupState::HalfSetUp); }Some(BackupState::Stopped { last_copy: instant([backup["lastAt"].as_str(), backup["since"].as_str()]) }) }/// The notice a Mac filed in Feedback (`cotton_client::backup::owner_notice`,/// context kind `backup-failed`): stopped since the episode began.pubfn of_notice(context: &Value) -> Option<BackupState> { (context["kind"].as_str() == Some("backup-failed")) .then(|| BackupState::Stopped { last_copy: instant([context["since"].as_str()]) }) }/// The state's one line in the board's words, naming the Mac:/// `backup_behind` (`{device}`, `{behind}` as "3 days"), `backup_stopped`/// when no time is known, `backup_unset` for half a configuration.pubfn line(self, words: &Map<String, Value>, device: &str, now: DateTime<Utc>) -> Option<String> {let word = |key: &str| words.get(key)?.as_str().map(str::to_string);let device = Some(device.trim()).filter(|name| !name.is_empty()).unwrap_or("Your Mac");let device = crate::transforms::note_title::plain_preview(device);let line = matchself {BackupState::HalfSetUp => word("backup_unset")?,BackupState::Stopped { last_copy: None } => word("backup_stopped")?,BackupState::Stopped { last_copy: Some(from) } => word("backup_behind")?.replace("{behind}", &lag(now, from)), };Some(line.replace("{device}", &device)) }}/// A notices part's words — the one place the backup line's words are set/// (`definitions::board::default_of`).pubfn words() -> Map<String, Value> {crate::definitions::board::notice_words()}/// A receipt's line, or none when its backup is getting through.pubfn receipt_line(backup: &Value, device: &str, now: DateTime<Utc>) -> Option<String> {BackupState::of_receipt(backup)?.line(&words(), device, now)}/// A filed notice's line, or none when the capture is not a backup notice.pubfn notice_line(context: &Value, now: DateTime<Utc>) -> Option<String> {BackupState::of_notice(context)?.line(&words(), context["machine"].as_str().unwrap_or_default(), now)}fn instant<'a>(stamps: implIntoIterator<Item = Option<&'a str>>) -> Option<DateTime<Utc>> { stamps.into_iter().flatten().find_map(|at| DateTime::parse_from_rfc3339(at).ok()).map(|at| at.to_utc())}/// A lag in its largest whole unit: "3 days", "1 hour", "20 minutes".fn lag(now: DateTime<Utc>, from: DateTime<Utc>) -> String {let minutes = (now.signed_duration_since(from).num_seconds() / 60).max(1);let (count, unit) = if minutes >= 1440 { (minutes / 1440, "day") }elseif minutes >= 60 { (minutes / 60, "hour") } else { (minutes, "minute") }; format!("{count} {unit}{}", if count == 1 { "" } else { "s" })}#[cfg(test)]mod tests {usesuper::*;use serde_json::json;/// RULE: Backup receipts choose the board's status words from measured times; diagnostics never enter the line and a missing time never invents lag. #[test]fn backup_receipts_read_data_words_without_diagnostics_or_invented_lag() {let words = json!({"backup_behind":"{device} behind: {behind}", "backup_stopped":"{device} stopped", "backup_unset":"{device} half"}).as_object().unwrap().clone();let now = DateTime::parse_from_rfc3339("2026-10-16T06:00:00Z").unwrap().to_utc();let line = |backup: Value| BackupState::of_receipt(&backup).and_then(|state| state.line(&words, "Desk", now));for (backup, expected) in [ (json!({"state":"failing","lastAt":"2026-10-13T06:00:00Z","lastError":"run `private-command`"}), "Desk behind: 3 days"), (json!({"state":"failing","since":"2026-10-16T04:00:00Z"}), "Desk behind: 2 hours"), (json!({"state":"failing","lastAt":"invalid"}), "Desk stopped"), (json!({"state":"never","lastError":"missing `configuration`"}), "Desk half"), ] { assert_eq!(line(backup).as_deref(), Some(expected)); } assert_eq!(line(json!({"state":"ok"})), None); }/// RULE: A backup that stopped reads as ONE line wherever it is said — the Mac's receipt on Home and its page, and the notice it filed in Feedback — because three wordings of one fact read as three facts (walker pass 4, row 10). #[test]fn the_receipt_and_the_filed_notice_say_one_line() {let now = DateTime::parse_from_rfc3339("2026-10-16T06:00:00Z").unwrap().to_utc();let receipt = json!({"state":"failing","since":"2026-10-14T06:00:00Z","lastError":"litestream: exit 1"});let notice = json!({"kind":"backup-failed","machine":"Desk","since":"2026-10-14T06:00:00Z","databases":[{"databaseId":"db","reason":"litestream: exit 1"}]});let said = receipt_line(&receipt, "Desk", now).expect("a stopped backup has a line"); assert_eq!(notice_line(¬ice, now).as_deref(), Some(said.as_str())); assert!(said.contains("Desk") && !said.contains("litestream")); assert_eq!(notice_line(&json!({"kind":"report"}), now), None); }}