//! The ONE place a failure becomes words for the person (architect ruling R6, //! 5 Oct 2026). A failure never prints the machinery under it — the fold, the //! write clock, a CHECK constraint, a table or column name, a record id: it //! says what did not happen and what to do, and the raw text goes to the log //! only. Every surface that hands a failure to a person (the phone's and the //! Mac's methods, the tools' commit) asks [`for_person`]; a sentence already //! written for the person passes through unchanged. /// What the person reads when a change could not be kept. pub const NOT_KEPT: &str = "That wasn't kept. Try again."; /// What the person reads when something could not be read. pub const NOT_READ: &str = "That couldn't be opened. Try again."; /// What the person reads when anything else failed. pub const NOT_DONE: &str = "That didn't work. Try again."; /// What a reader reads when it asked for any change: no retry will keep it. pub const READS_ONLY: &str = "This device reads only."; /// What a failure did not do, which picks the sentence said for it. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Act { /// A write: a save, a command, an undo. Keep, /// A read. Read, /// Anything else. Other, } impl Act { pub fn sentence(self) -> &'static str { match self { Act::Keep => NOT_KEPT, Act::Read => NOT_READ, Act::Other => NOT_DONE, } } } /// A failure as the person reads it, and the raw text the log keeps when the /// raw text was not fit to show. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Said { pub sentence: String, /// The text that was replaced — for the log, never for a screen. pub logged: Option, } /// The words a person reads for `raw`: `raw` itself when it is already a /// sentence for them, else the plain sentence for `act`, with `raw` handed /// back for the log. pub fn for_person(raw: &str, act: Act) -> Said { let trimmed = raw.trim(); // A READER'S WRITE, refused at the store with its typed refusal (shape // 139), is not a "try again": the page says what this device is. if trimmed.contains(crate::device::scope::READER_WRITE_REFUSAL) { return Said { sentence: READS_ONLY.to_string(), logged: Some(raw.to_string()) }; } if trimmed.is_empty() || shows_internals(trimmed) { return Said { sentence: act.sentence().to_string(), logged: Some(raw.to_string()), }; } Said { sentence: trimmed.to_string(), logged: None, } } /// Words that only the machinery says. Matched on whole words, lowercased. const INTERNAL_WORDS: [&str; 13] = [ "fold", "folded", "hlc", "sqlite", "sql", "constraint", "op(s)", "rowid", "panicked", "unwrap", "errno", "deserialize", "serde", ]; /// Phrases that only the machinery says. const INTERNAL_PHRASES: [&str; 12] = [ "write clock", "check constraint", "is null", "is not null", "no such table", "no such column", "foreign key", "unique constraint", "os error", "database is locked", "invalid type:", "expected value at line", ]; /// Whether `text` prints anything a person should never read: a code span, a /// snake_case or dotted identifier (a table, a column, a method), a record id /// (a uuid or a long hex run), an SQL fragment, or the fold's own words. pub fn shows_internals(text: &str) -> bool { if text.contains('`') || text.contains("{") || text.contains("::") || text.contains(" = ") { return true; } let lower = text.to_lowercase(); if INTERNAL_PHRASES.iter().any(|phrase| lower.contains(phrase)) { return true; } lower .split(|c: char| { c.is_whitespace() || matches!(c, ',' | ';' | ':' | '"' | '\'' | '(' | ')' | '[' | ']') }) .filter(|word| !word.is_empty()) .any(|word| { let word = word.trim_end_matches(['.', '!', '?', '—']); INTERNAL_WORDS.contains(&word) || is_identifier(word) || is_record_id(word) }) || text.split_whitespace().any(|word| word.contains("op(s)")) } /// `cotton_media_albums`, `decided_at`, `records.update`, `_group`: a name the /// code uses. A web address or a file name is not one of them, so a word with /// `/` or `@` is left alone. fn is_identifier(word: &str) -> bool { if word.contains('/') || word.contains('@') { return false; } let underscored = word.contains('_') && word.chars().any(|c| c.is_ascii_alphabetic()); // `read.members`, `records.list.everything`: every part a lowercase word. let parts: Vec<&str> = word.split('.').collect(); let dotted = parts.len() > 1 && parts.iter().all(|part| !part.is_empty() && part.chars().all(|c| c.is_ascii_lowercase())) && !matches!( parts.last().copied().unwrap_or_default(), "com" | "org" | "net" | "io" | "app" | "md" | "txt" | "pdf" ); underscored || dotted } /// A uuid (8-4-4-4-12), or an unbroken run of twelve or more hex digits with /// letters and digits in it: a record's id. A phone number is neither. fn is_record_id(word: &str) -> bool { let hex = |part: &str| !part.is_empty() && part.chars().all(|c| c.is_ascii_hexdigit()); let parts: Vec<&str> = word.split('-').collect(); if parts.len() == 5 { return parts.iter().map(|part| part.len()).eq([8, 4, 4, 4, 12]) && parts.iter().all(|part| hex(part)); } hex(word) && word.len() >= 12 && word.chars().any(|c| c.is_ascii_digit()) && word.chars().any(|c| c.is_ascii_alphabetic()) } #[cfg(test)] #[path = "failure_tests.rs"] mod tests;