Cotton code
138 lines · 5.4 KB · SwiftRaw
// AN ACT'S PICKS ON THE FAN (door, 23 Oct 2026). An act that picks before it// runs — Take an order (a customer, then how many), Renew (a plan), File (a// database) — asks on the fan itself, never on a sheet (door row 7): the// card's acts turn into the first pick's answers, a tap answers it and the// fan turns into the next, and the last answer runs the act. The close// cancels and runs nothing.//// THE CORE ANSWERS EVERY PICK (`shell.opened {step: pick}`, 7 Nov 2026): the// rows of the pick now due — a record pick's rows narrowed by the letters// written over it, a number's stepper, a moment's days and then its half// hours, words from a column, the person's other databases, the tables —// and a row's value is committed through `shell.input`, which answers the// next pick, or runs the act once it was the last. Free words are asked in// the shell's writer as the core says (`PickStep.words`).//// A customer's act (Take an order on a business's row, `CustomerDeck`) asks// the same step of its business's cache (`business` on the ask,// `shell_input_business`): the core holds no credential, so the last answer// comes back as `answers` for the caller to post (`DeckPicking.perform`).import CottonKitimport CottonValuesimport Foundation/// One answer a pick offers, as one card of the fan.struct DeckPickOption: Identifiable, Equatable {    enum Does: Equatable {        /// The value the act is sent for this pick.        case answer(String)        /// A number's stepper: the count stands at this instead.        case count(Double)        /// A moment's day: the times of that day follow.        case day(String)        /// A record pick's search, asked in the shell's writer.        case search(InputAsk?)    }    let id: String    let word: String    var detail: String? = nil    var picture: String? = nil    var initials: String? = nil    let does: Does    /// A row of the pick the core answered.    init(_ row: PickStep.Row, index: Int) {        id = "\(index)-\(row.does)-\(row.value)"        word = row.words        detail = row.detail        picture = row.picture        initials = row.initials        does = switch row.does {        case "count": .count(row.count ?? 0)        case "day": .day(row.day ?? row.value)        case "search": .search(row.search)        default: .answer(row.value)        }    }    init(id: String, word: String, detail: String? = nil, picture: String? = nil, initials: String? = nil, does: Does) {        self.id = id        self.word = word        self.detail = detail        self.picture = picture        self.initials = initials        self.does = does    }}/// Where a pick reads its answers: the store the fan stands over, the core,/// the tables — or, with no store, the rows a business served a customer.struct DeckPickPlace {    var store: (any Records)?    var core: (any CottonCalling)?    var definitions: TableDefinitions    /// The business whose cache the pick is read from (`{server, slug}`),    /// on a customer's phone.    var business: JSONValue? = nil}/// An act's picks being asked on the fan, one after another, as the core/// answers them.struct DeckPicking {    /// The act that asks them.    let act: String    let target: DeckEditTarget    /// The draft the card is, while it is not made: its act is staged there.    let draft: String?    let place: DeckPickPlace    /// A customer's act: handed every answer once the last is given.    var perform: (@MainActor ([String]) async -> Void)? = nil    /// The pick now due, as the core said it.    var step: PickStep? = nil    /// The step's answers, as the fan's cards.    var options: [DeckPickOption] = []    /// A number's count as the stepper stands.    var count: Double? = nil    /// A moment's day, chosen: its times are on the fan.    var day: String? = nil    var query = ""    /// The letters written on the ground (`InkGround`): a record pick's    /// rows narrowed to those a word of whose name starts with them, as the    /// core holds them on `inkSurface`, a fresh one for each step.    var ink = InkLetters()    var inkSurface = "pick:\(UUID().uuidString)"    /// The card the arc rests on: a number's count, else the first answer.    var rest: Int { step?.rest ?? 0 }    /// Whether the step on the fan is a moment's (drawn on the days' row).    var asksMoment: Bool { step?.moment == true }    /// What the core is asked for the pick now due.    var asked: JSONValue {        var ask: [String: JSONValue] = [            "table": .string(target.table), "step": "pick", "act": .string(act),            "answers": step?.writes["answers"] ?? .array([]), "timezone": .string(TimeZone.current.identifier)]        if let draft { ask["draft"] = .string(draft) } else { ask["id"] = .string(target.record) }        if let business = place.business { ask["business"] = business }        if !query.isEmpty { ask["query"] = .string(query) }        if !ink.isEmpty { ask["ink"] = .string(ink.letters) }        if let day { ask["day"] = .string(day) }        if let count { ask["count"] = .number(count) }        return .object(ask)    }}/// What an act tapped on the fan came to.enum DeckActOutcome: Equatable {    /// The fan asks a pick.    case asking    /// The act ran: the core's change.    case ran(ChangeRecord)    /// The act was refused, or a pick had nothing to offer: the sentence.    case refused(String)    /// Nothing ran: the fan was closed, or the act runs nowhere here.    case none}