Cotton code
146 lines · 5.9 KB · SwiftRaw
// A record's card as the core resolved it (`cotton_core::records::card`,// under `@card`): the face kind, the corner's words, the pace band and plate,// and each place's value. The deck draws these and composes no words. The one// value a face takes; reading it off a record, and saying a card the deck// cannot read, is the app's (`CottonUI/Deck/DeckCard+Record.swift`).import Foundation/// The pace bands (`transforms::pace::Pace`), most pressing first.public enum DeckPace: String, Sendable, CaseIterable {    case late, soon, upcoming, someday, done}public struct DeckCard: Equatable, Sendable {    public static let key = "@card"    public var face: String    public var corner: String?    /// The corner's date is behind: the late ink.    public var pressing: Bool    public var pace: DeckPace?    /// The band's plate, 0..<8, when the card carries the pace art.    public var plate: Int?    /// The thread's recent messages, filled by the core before presentation.    public var thread: ThreadHistory?    public var fields: [String: RecordValue]    // What the deck read for the card beyond the core's words    // (`CardReadings.swift`; CottonUI's `DeckCardFill`). A card read off    // `@card` holds none; a face draws the ones it is given.    /// Answered whole by the core (`shell.deck`): its readings are the    /// core's, and nothing on the phone fills it again.    public var answered = false    /// The id of the record it is the card of: whose track it plays.    public var record: String?    /// The pages it turns to after its face (`card.pages`).    public var pages: [CardPage] = []    public var related: [CardRelatedCount] = []    public var relatedReadFailed = false    /// Its places and its table's columns under their labels.    public var labels = CardLabels()    /// The rating its facts name in the rating column, out of five.    public var stars: Double?    /// The value stored in the column under a place, by slot, as kept (a    /// state's "running", a dish's kind), never its label.    public var stored: [String: String] = [:]    /// A delivery's states, as its columns hold them.    public var delivery: KitchenDeliveryState?    /// How a person is reached.    public var reach = CardReach()    /// The record's own file (`storage_path`): a document's first page.    public var file: String?    /// An album's print keys, in its order; nil until read.    public var prints: [String]?    /// `@card` as the core resolved it (`ResolvedCard`'s JSON): nil when    /// absent, the typed failure when it is there and not in that shape.    public static func read(_ value: RecordValue) -> Result<DeckCard, DeckCardFailure>? {        if value == .null { return nil }        guard case .object(let card) = value else { return .failure(.notAnObject("card")) }        guard case .text(let face)? = card["face"], !face.isEmpty else { return .failure(.noFace) }        var corner: String?        switch card["corner"] {        case nil, .null?: corner = nil        case .text(let words)?: corner = words        default: return .failure(.notWords("corner"))        }        var pressing = false        switch card["pressing"] {        case nil, .null?: break        case .bool(let value)?: pressing = value        default: return .failure(.notABool("pressing"))        }        var pace: DeckPace?        switch card["pace"] {        case nil, .null?: pace = nil        case .text(let band)?:            guard let known = DeckPace(rawValue: band) else { return .failure(.unknownPace(band)) }            pace = known        default: return .failure(.notWords("pace"))        }        var plate: Int?        switch card["plate"] {        case nil, .null?: plate = nil        case .number(let number)?: plate = Int(number)        default: return .failure(.notANumber("plate"))        }        var fields: [String: RecordValue] = [:]        switch card["fields"] {        case nil, .null?: break        case .object(let given)?: fields = given        default: return .failure(.notAnObject("fields"))        }        var resolved = DeckCard(face: face, corner: corner, pressing: pressing, pace: pace, plate: plate, fields: fields)        if let thread = card["thread"], thread != .null {            do { resolved.thread = try JSONDecoder().decode(ThreadHistory.self, from: JSONEncoder().encode(thread)) }            catch { return .failure(.invalidThread) }        }        return .success(resolved)    }    public init(face: String, corner: String?, pressing: Bool, pace: DeckPace?, plate: Int?, fields: [String: RecordValue]) {        self.face = face        self.corner = corner        self.pressing = pressing        self.pace = pace        self.plate = plate        self.fields = fields    }    /// A place's words, nil when the definition fills none or it is empty.    public func text(_ slot: String) -> String? {        guard let text = fields[slot]?.text, !text.isEmpty else { return nil }        return text    }    /// The check's state; nil when the face has no check.    public var checked: Bool? {        guard case .bool(let checked)? = fields["check"] else { return nil }        return checked    }}/// Why a record's `@card` could not be read: the core's card JSON/// (`cotton_core::records::card::ResolvedCard`) and this decoder disagree.public enum DeckCardFailure: Error, Equatable, Sendable {    case notAnObject(String)    case invalidThread    case noFace    case notWords(String)    case notABool(String)    case notANumber(String)    case unknownPace(String)}public struct CardRelatedCount: Equatable, Sendable, Identifiable {    public var id: String    /// The count as the core says it, in the related table's own words and    /// the plural form its number calls for ("1 photo", "12 photos").    public var said: String    public var count: Int    public init(id: String, said: String, count: Int) {        self.id = id; self.said = said; self.count = count    }}