Cotton code
300 lines · 14.0 KB · SwiftRaw
// THE DECK'S PAGE, AS THE CORE ANSWERS IT (`shell.deck`; boundary review// move 2, 5 Nov 2026): the cards in the view's order, each its face worn,// its places' words and what the deck read beyond them, its position said,// its tap map and its acts by id; the view's head, the empty deck's words,// the hand's faces and what Plus does (`shell.plus`). The deck draws this and// decides none of it. Plain JSON, the same answer a Kotlin shell reads.import CottonKitimport CottonValuesimport Foundationstruct DeckAnswer: Decodable, Equatable, Sendable {    let table: String    let view: String?    /// The view the core landed a record's or a blank's deck on, as the fan    /// says a view (`lands`); nil on every other read.    let landed: ToolView?    let cards: [Card]    let acts: [String: Act]    let labels: Labels    /// Each table's labels, where letters searched the app's whole kit and    /// its cards are of more than one table; nil on every other read.    var labels_of: [String: Labels]? = nil    let count: Int    let head: String?    /// The empty deck's words: a title always, the message where there is one.    let empty: DefinitionSectionEmptyWords?    let hand: [String]    let worn: String?    /// What the hand's tap wears; nil where the deck has no hand.    let hand_next: HandNext?    let plus: Plus    let visitor: Card?    /// The cursor of the page after this one; nil at the deck's end.    let next: Int?    /// The face after the one worn, what VoiceOver says of it, and what    /// `shell.input` commits to wear it.    struct HandNext: Decodable, Equatable, Sendable {        let face: String        let label: String?        let writes: JSONValue    }    struct Act: Decodable, Equatable, Sendable {        let label: String?        let kind: String?        let red: Bool    }    struct Labels: Decodable, Equatable, Sendable {        let by_slot: [String: String]        let by_column: [String: String]    }    struct Card: Decodable, Equatable, Sendable {        let id: String        let face: String        let corner: String?        let pressing: Bool        let pace: String?        let plate: Int?        let thread: ThreadHistory?        let slots: [String: RecordValue]        let pages: [Page]        let stars: Double?        let stored: [String: String]        let reach: Reach        let file: String?        let delivery: Delivery?        let prints: [String]?        /// "3 / 14"; nil on a visitor.        let position: String?        /// What a tap does, by place: `card` the card's own (`open.<how>`), `check` its check.        let tap: [String: String]        let acts: [String]        /// "From {name}, {day}": the send that brought it (its provenance), said while it is opened.        let from: String?        /// What a tap on its check writes, `{column: value}`; nil where it writes nothing.        let toggle: [String: RecordValue]?        /// A visitor's: whether the view holds it.        let held: Bool?        /// The list this card stands for, on a list's deck whose tool stands        /// its lists in a face of their own (Music's cassette, first on the        /// deck): its `record` is the list's row, never one of the deck's table.        let list: String?        /// Where a group card's tap goes (`open.group`); nil where no tool        /// partitions its table by its people, and it opens at rest.        let group: GroupRoute?        let record: CoreRecords.WireRecord        /// The table the card's record is of, where letters searched the        /// app's whole kit (a highlight among books); nil, the deck's own.        let table: String?        /// A tile of the fan's first tier standing in the results (`view`):        /// drawn by the fan's own tile look, a tap opening its view.        let view: ToolView?        let tool: String?        let tile: FanTileLook?        /// The partitioned view a group card opens, scoped to its person.        struct GroupRoute: Decodable, Equatable, Sendable {            let table: String            let column: String            let value: String            let view: String            let title: String        }        struct Page: Decodable, Equatable, Sendable {            let label: String            let text: String            let markdown: Bool        }        struct Reach: Decodable, Equatable, Sendable {            let phones: [String]            let emails: [String]            let channel: String?            let call: Bool            let message: Bool            let email: Bool        }        struct Delivery: Decodable, Equatable, Sendable {            let delivered: Bool            let paid: Bool            let on_plan: Bool        }        private enum CodingKeys: String, CodingKey {            case id, face, corner, pressing, pace, plate, thread, slots, pages, stars, stored, reach, file, delivery, prints            case position, from, tap, acts, toggle, held, list, group, record, table, view, tool, tile        }        init(from decoder: Decoder) throws {            let container = try decoder.container(keyedBy: CodingKeys.self)            id = try container.decode(String.self, forKey: .id)            face = try container.decode(String.self, forKey: .face)            corner = try container.decodeIfPresent(String.self, forKey: .corner)            pressing = try container.decodeIfPresent(Bool.self, forKey: .pressing) ?? false            pace = try container.decodeIfPresent(String.self, forKey: .pace)            plate = try container.decodeIfPresent(Int.self, forKey: .plate)            thread = try container.decodeIfPresent(ThreadHistory.self, forKey: .thread)            slots = try container.decodeIfPresent([String: RecordValue].self, forKey: .slots) ?? [:]            pages = try container.decodeIfPresent([Page].self, forKey: .pages) ?? []            stars = try container.decodeIfPresent(Double.self, forKey: .stars)            stored = try container.decodeIfPresent([String: String].self, forKey: .stored) ?? [:]            reach = try container.decode(Reach.self, forKey: .reach)            file = try container.decodeIfPresent(String.self, forKey: .file)            delivery = try container.decodeIfPresent(Delivery.self, forKey: .delivery)            prints = try container.decodeIfPresent([String].self, forKey: .prints)            position = try container.decodeIfPresent(String.self, forKey: .position)            from = try container.decodeIfPresent(String.self, forKey: .from)            tap = try container.decodeIfPresent([String: String].self, forKey: .tap) ?? [:]            acts = try container.decodeIfPresent([String].self, forKey: .acts) ?? []            toggle = try container.decodeIfPresent([String: RecordValue].self, forKey: .toggle)            held = try container.decodeIfPresent(Bool.self, forKey: .held)            list = try container.decodeIfPresent(String.self, forKey: .list)            group = try container.decodeIfPresent(GroupRoute.self, forKey: .group)            record = try container.decode(CoreRecords.WireRecord.self, forKey: .record)            table = try container.decodeIfPresent(String.self, forKey: .table)            view = try container.decodeIfPresent(ToolView.self, forKey: .view)            tool = try container.decodeIfPresent(String.self, forKey: .tool)            tile = try container.decodeIfPresent(FanTileLook.self, forKey: .tile)        }        static func == (a: Card, b: Card) -> Bool {            a.id == b.id && a.face == b.face && a.slots == b.slots && a.corner == b.corner && a.position == b.position                && a.pressing == b.pressing && a.pace == b.pace && a.plate == b.plate && a.tap == b.tap && a.acts == b.acts                && a.toggle == b.toggle && a.record.record == b.record.record && a.prints == b.prints && a.pages == b.pages                && a.list == b.list && a.table == b.table && a.view == b.view        }        /// The card the face draws, as answered: nothing is filled on the phone.        func drawn(labels: Labels) -> DeckCard {            var card = DeckCard(face: face, corner: corner, pressing: pressing, pace: pace.flatMap(DeckPace.init(rawValue:)),                                plate: plate, fields: slots)            card.thread = thread            card.answered = true            card.record = id            card.pages = pages.map { CardPage(label: $0.label, text: $0.text, markdown: $0.markdown) }            card.labels = CardLabels(bySlot: labels.by_slot, byColumn: labels.by_column)            card.stars = stars            card.stored = stored            card.reach = CardReach(phones: reach.phones, emails: reach.emails, channel: reach.channel,                                   call: reach.call, message: reach.message, email: reach.email)            card.file = file            card.delivery = delivery.map { KitchenDeliveryState(delivered: $0.delivered, paid: $0.paid, onPlan: $0.on_plan) }            card.prints = prints            return card        }        /// The fan's view this card stands for, by its tool: a tap opens it.        var opensView: (tool: String, view: ToolView)? {            guard let tool, let view else { return nil }            return (tool, view)        }        /// How a tap on the card opens it (`open.<how>`).        var opens: DeckOpens { tap["card"].flatMap { DeckOpens(rawValue: String($0.dropFirst("open.".count))) } ?? .opened }    }    /// What a table's "+" does (`shell.plus`): its doors' choice tier, its act    /// at the object, its route, or a new record written by hand.    struct Plus: Decodable, Equatable, Sendable {        let does: String        let label: String?        let doors: [Door]        let act: Ask?        let route: String?        /// A phone method called at once, nothing asked (`does: call`): its        /// answer's platform action is followed (Story's New scene opens the        /// writer on the scene it made).        var method: String? = nil        /// The fan tile of the table's tool (`FanTileLook`), which its doors are drawn on.        var tile: FanTileLook? = nil        struct Door: Decodable, Equatable, Sendable {            let id: String            let label: String            let picture: String?            /// Its kit pictogram, by reference, where the tool's tile names one.            var pictogram: String? = nil            let accepts: Accepts        }        struct Accepts: Decodable, Equatable, Sendable {            let words: Ask?            let files: DefinitionCreateFiles?            /// Pictures from the photo library or the camera (Photos' Choose            /// photos and Take a photo), the system's own picker for each.            var pick: DefinitionCreatePick? = nil            /// What this device keeps, read in (Read Apple Health): the            /// app's attached reader, the system asked at this tap.            var reads: DefinitionDeviceReads? = nil            /// A new record begun by the core and each of its parts asked in            /// the shell's one input (Money's Add a spend: the amount, then            /// its words), as Android's `askDraftDoor` asks it.            var draft: JSONValue? = nil        }        struct Ask: Decodable, Equatable, Sendable {            let act: String            let argument: String            let lands: String?            let signed_out: String?            var create: DefinitionCreateAct {                var act = DefinitionCreateAct(name: self.act, argument: argument, lands: lands)                act.signedOut = signed_out                return act            }        }    }}extension DeckAnswer.Plus {    /// `shell.plus {table}`: what `table`'s "+" does, where no deck has read it.    @MainActor static func read(_ table: String, core: (any CottonCalling)?) async throws -> DeckAnswer.Plus? {        guard let core else { return nil }        return try await core.call(CottonMethod.shellPlus, PlusAsk(table: table), as: DeckAnswer.Plus.self)    }    private struct PlusAsk: Encodable, Sendable { let table: String }    /// The doors as the fan's choice tier: a door's words asked in the    /// shell's one input and sent to its act, a door's files chosen in the    /// system's picker (`choose`, `doorFiles`), a door's pictures chosen in    /// the photo library or taken with the camera (`pick`, `doorPictures`).    @MainActor func offers(_ editor: DeckEditor?, table: String, core: (any CottonCalling)?, toasts: ToastCenter?,                           landed: @escaping @MainActor (String) -> Void,                           pick: @escaping @MainActor (DefinitionCreatePick) -> Void = { _ in },                           choose: @escaping @MainActor (DefinitionCreateFiles) -> Void) -> [DeckOffer] {        doors.map { door in            DeckOffer(id: "door-\(door.id)", word: door.label, picture: door.picture, pictogram: door.pictogram, tile: tile) { _ in                if let act = door.accepts.words {                    editor?.askDoor(act.create, door: door.id, table: table, core: core, toasts: toasts, landed: landed)                } else if let files = door.accepts.files {                    choose(files)                } else if let pictures = door.accepts.pick {                    pick(pictures)                } else if door.accepts.draft != nil {                    editor?.askDraftDoor(table: table, door: door.id, core: core)                } else if let reads = door.accepts.reads {                    Task { await DeviceReadings.read(reads, core: core, toasts: toasts) }                }            }        }    }}/// Where a tap on a card goes, as the core says (`shell.deck`'s `tap.card`):/// a book into the reader, a whole into its parts, a group card into its/// person's list, a thread into its chat, a thing the viewer shows into the/// viewer, a card of its own words into its reading page, a list's own card/// (its cassette) plays the list, a scene into the writer (Cotton Story);/// else it opens at rest.enum DeckOpens: String, Equatable, Sendable {    case writer, reader, whole, group, conversation, viewer, reading, opened, play}