// The pure words said about devices — by the Devices table's source, the join // request screen and Home's device line — and the ladder that decides WHICH // refusal they say. // // Three pure pieces, all of them tested as pure here: // // • the platform's word and the actor's; // • one sentence per refusal code the pairing server names; // • the ladder that picks between the STANDING device refusal, the list's own // refusal, the code's sentence, and the last resort. // // **A refusal CODE is never smoothed.** A code nothing here names has no // sentence of its own and falls through to the refusal's own message, exactly // as `toastError` → `formatErrorForUser` does on the phone. import CottonKit import Foundation /// The pairing digits: six of them, the server's own grammar. public let pairingDigits = 6 /// The six, grouped the way a person reads them aloud: `482913` → `482 913`. /// /// A PURE function so the grouping is tested without a renderer, and the ONLY /// arithmetic done on the number. Anything that is not six ASCII numerals is /// drawn exactly as it arrived rather than reshaped — the server's answer is /// the authority, and a screen that tidied it could show a different number /// from the one it is asking about. public func pairingDigitsGrouped(_ digits: String) -> String { guard digits.count == pairingDigits, digits.allSatisfy({ $0.isASCII && $0.isNumber }) else { return digits } let middle = digits.index(digits.startIndex, offsetBy: pairingDigits / 2) return "\(digits[digits.startIndex.. [String] { guard digits.count == pairingDigits, digits.allSatisfy({ $0.isASCII && $0.isNumber }) else { return [] } return digits.map(String.init) } public enum DeviceWords { /// The platform's word, from the wire's enum. `nil` for anything else, and /// the row then draws no platform at all rather than the raw enum value. public static func platform(_ platform: String?) -> String? { switch platform { case "ios": return "iPhone" case "android": return "Android" case "mac": return "Mac" default: return nil } } /// "by " — who approved or removed a row, read off the list itself. /// /// `"self"` is the root row a server writes for itself at boot: nobody's /// phone. `"recovery-kit"` is the row a restore wrote for the Mac it made /// the server. static func actor(_ deviceId: String?, in devices: [ApprovedDevice]) -> String { guard let deviceId, !deviceId.isEmpty, deviceId != "self" else { return ShellLines.line("devices", "the_server") } if deviceId == "recovery-kit" { return ShellLines.line("devices", "the_recovery_kit") } if let named = devices.first(where: { $0.deviceId == deviceId }) { return named.name } return ShellLines.line("devices", "another_device") } /// The detail line under a row's name: the platform and the version, joined /// by the middle dot, with either half absent when it is unknown. /// /// `[platform, appVersion].filter(Boolean).join(" · ")` — an EMPTY string is /// no line at all, which is what `detail ? … : null` means. public static func detail(platform: String?, appVersion: String?) -> String? { let parts = [DeviceWords.platform(platform), appVersion] .compactMap { $0 } .filter { !$0.isEmpty } return parts.isEmpty ? nil : parts.joined(separator: " \u{00B7} ") } } /// One sentence per refusal, and the ladder above them. public enum DeviceRefusalWords { public static var digitsMoved: String { ShellLines.line("devices", "that_request_changed_look") } /// What a pending row with no number is told, rather than approved. /// /// A server that predates the digits answers `null` on this field, and /// there is no question to put: the second factor IS seeing the same six on /// both screens, so an approve with nothing to compare would be an approve /// with no second factor at all. public static let noDigits = ShellLines.line("devices", "this_request_came_from") /// The pairing server's error-codes table, said in the person's words. /// /// `nil` for a code not named here — see the file header. public static func sentence(forCode code: String?) -> String? { switch code { case "pairing_refused": return ShellLines.line("devices", "that_number_didn_t") case "pairing_not_found": return ShellLines.line("devices", "that_pairing_has_gone") case "pairing_ambiguous": return ShellLines.line("devices", "two_phones_are_waiting") case "rekey_needs_another_device": return ShellLines.line("devices", "approve_a_re_key") case "rekey_not_approved": return ShellLines.line("devices", "this_device_is_no") case "serving_twin": return ShellLines.line("devices", "this_mac_already_serves") case "device_last_full": return ShellLines.line("devices", "approve_another_device_first") case "bad_request": return ShellLines.line("devices", "that_device_s_code") default: return nil } } /// The same, off whatever a `call` threw. public static func sentence(for error: (any Error)?) -> String? { guard let error else { return nil } return sentence(forCode: OwnerRefusal.from(error)?.code) } /// What the devices card says when it cannot draw the list. /// /// **A read that failed is a sentence, not a skeleton** — a skeleton is /// only ever "loading" and it must have an end (2026-09-06: a 401 on every /// call left this card a skeleton for good). The list is the FILE's now /// (`read.devices`), so what can fail is the file, and the server's /// standing refusal of this phone is no longer an arm here: it is the /// banner over the whole app, and the rows stay readable under it. The /// order: /// /// 1. a refusal code the gate knows (`ServerRefusal.of`) — its title; /// 2. the code's own sentence; /// 3. the last resort. /// /// `nil` when there is no error — which is the card drawing rows or a /// skeleton. public static func cardSentence(error: (any Error)?) -> String? { guard let error else { return nil } if let code = OwnerRefusal.from(error)?.code, let refusal = ServerRefusal.of(code: code) { return refusal.title } if let named = sentence(for: error) { return named } return ShellLines.line("devices", "couldn_t_read_your") } /// What a failed VERB says in the toast. /// /// The code's own sentence when there is one, else the refusal's own /// message (which is what `formatErrorForUser` surfaces), else the generic /// line. /// /// PORT GAP: [devices-refusal-not-reported] a refusal was once REPORTED as /// well as said (ruling R62). This app has no observability client and no /// HTTP client to report with, so /// only the SAYING half survives — every refusal a person reads on these two /// screens is invisible afterwards. public static func toastSentence(for error: any Error) -> String { if let named = sentence(for: error) { return named } return (error as? CottonCallError)?.message ?? CottonStrings.somethingWentWrong } } /// When a pairing asked, handwritten on its key tag: "asked 9:58 am" today, /// "asked Yesterday", then "asked Fri 2 Oct" — the core's `recent` part. A /// pairing with no stamp says nothing rather than "asked " alone. func pendingPairingAge(_ said: String?) -> String? { guard let said, !said.isEmpty else { return nil } return "asked \(said)" }