Cotton code
253 lines · 9.8 KB · SwiftRaw
// HOME'S PAGE, AS THE CORE ANSWERS IT (`shell.today`; the shell boundary,// 7 Nov 2026): the deck in the board's order — this device's card, today's// rows across every database held, the birthdays and money due, the unread// conversations, a card for each band that speaks — each worn in its face// with its tap and acts; the bands' words (Asked of you, Done for you, the// notices); the tools' arrangement; a failed read's line; the// families whose data events mean it is read again. Home draws this and// decides none of it. Plain JSON, the same answer a Kotlin shell reads.import CottonKitimport CottonValuesimport Foundationextension CottonMethod {    /// `shell.today {timezone, today, database?, board?, size?, cursor?, limit?}`    public static let shellToday = "shell.today"    /// `shell.undo {kind, id, database?}` — a Done for you receipt taken back: `{said}`.    public static let shellUndo = "shell.undo"}struct TodayAnswer: Decodable, Equatable, Sendable {    let cards: [Card]    let acts: [String: DeckAnswer.Act]    let count: Int    let parts: Parts    /// "Working on 2 · Researching X · 12 min".    let working_on: String?    /// `phone` or `mac`: which side is behind.    let update: String?    /// The shown database's copy still on its way, in one calm sentence.    let arriving: String?    let failed: Failed?    /// The tools' arrangement (`read.home`'s), for the board's grid.    let arrangement: JSONValue?    let projects: [JSONValue]    let project_facts_missing: Bool    /// The shown database's workspace, once read.    let workspace_id: String?    /// The data families whose events mean the page is read again.    let families: [String]    /// What an empty Home says.    let empty: String    let next: Int?    /// The same answer holding `cards`.    func with(cards: [Card]) -> TodayAnswer {        TodayAnswer(cards: cards, acts: acts, count: count, parts: parts, working_on: working_on, update: update,                    arriving: arriving, failed: failed, arrangement: arrangement, projects: projects,                    project_facts_missing: project_facts_missing, workspace_id: workspace_id, families: families, empty: empty, next: next)    }    /// One card of Home's deck: the deck's card, and what Home knows of it.    struct Card: Decodable, Equatable, Sendable, Identifiable {        /// `device`, `day`, `later`, `thread` or `part`.        let kind: String        let table: String?        /// What a tap on a day's card opens.        let opens: Opens?        /// The paper a day's slip is cut from on Home's list (`PaperDye`'s name).        let dye: String?        /// The database the row is in, when it is not the shown one.        let database: Database?        let deck: DeckAnswer.Card        var id: String { deck.id }        private enum CodingKeys: String, CodingKey { case kind, table, opens, dye, database }        init(from decoder: Decoder) throws {            let container = try decoder.container(keyedBy: CodingKeys.self)            kind = try container.decode(String.self, forKey: .kind)            table = try container.decodeIfPresent(String.self, forKey: .table)            opens = try container.decodeIfPresent(Opens.self, forKey: .opens)            dye = try container.decodeIfPresent(String.self, forKey: .dye)            database = try container.decodeIfPresent(Database.self, forKey: .database)            deck = try DeckAnswer.Card(from: decoder)        }    }    struct Opens: Decodable, Equatable, Sendable {        let table: String        let id: String        /// The id its tool's deck stands on: an occurrence's `<series>@<at>`.        let page: String        /// A repeating event's occurrence: the start it opens at.        let at: String?        let database: String?    }    struct Database: Decodable, Equatable, Sendable {        let id: String        let name: String?        var row: FeedRowDatabase? { FeedRowDatabase(databaseId: id, databaseName: name) }    }    struct Parts: Decodable, Equatable, Sendable {        let asked: Asked        let done: [Done]        let notices: [Notice]        /// The board whole, in the definition's order (`shell.today`'s module doc).        let board: [BoardEntry]    }    struct Asked: Decodable, Equatable, Sendable {        let items: [Item]        let total: Int        /// The page could not be read and has nothing of its own to draw.        let unreadable: Bool        struct Item: Decodable, Equatable, Sendable {            let key: String            let count: Int            let label: String            let path: String            let database: Database?            let tiles: [JSONValue]        }    }    struct Done: Decodable, Equatable, Sendable {        let kind: String        let id: String        let label: String        let text: String        let when: String        let action: String        let path: String    }    struct Notice: Decodable, Equatable, Sendable {        let id: String        let words: String        /// `update`, `quiet`, `amber` or `refusal`.        let kind: String        let title: String?        let label: String?        let side: String?        let path: String?    }    struct Failed: Decodable, Equatable, Sendable {        let why: String        let line: String        /// A read the core cut keeps trying by itself; any other offers Try again.        let keeps_trying: Bool    }}/// One part of the board, as `shell.today`'s `parts.board` answers it: the/// part's own settings, whether it draws and why not, and what its kind draws./// The shell draws these in order and decides none of it.struct BoardEntry: Decodable, Equatable, Sendable, Identifiable {    struct Why: Decodable, Equatable, Sendable {        /// `quiet`, `head`, `personal`, `no_device`, `magnifier`, `unarranged`,        /// `no_table`, `no_record` or `unknown`.        let key: String        let words: String    }    struct Fact: Decodable, Equatable, Sendable {        let key: String        let label: String        let value: String    }    let id: String    let kind: String    let shown: Bool    let why: Why?    let settings: JSONValue    /// `head`: its tags, and the rows those tags count and open.    var tags: [String] = []    var headAsked: [TodayAnswer.Asked.Item] = []    var headAskedTotal = 0    var headDone: [TodayAnswer.Done] = []    /// `notices`: the notices it shows, in its order.    var notices: [TodayAnswer.Notice] = []    /// `asked` and `done`: the rows they show.    var asked: [TodayAnswer.Asked.Item] = []    var done: [TodayAnswer.Done] = []    var heading: String?    /// `device`: this device's card id. `today`: the deck's day, later and thread ids.    var card: String?    var cards: [String] = []    /// `today`: what a day with none says.    var quiet: String?    /// `list`: its table, how many rows it holds, and those rows.    var table: String?    var count: Int?    var records: [TableRecord] = []    /// `figure`: the line the core worked out.    var line: String?    /// `facts`: its table, the record, and each column in words.    var source: String?    var record: String?    var facts: [Fact] = []    /// `tools`: the tiles, whole.    var tools: [FanAnswer.Tile] = []    private struct Rows<Item: Decodable>: Decodable { let items: [Item]; var total: Int? }    private enum CodingKeys: String, CodingKey {        case id, kind, shown, why, settings, tags, asked, done, items, total, heading, card, cards, quiet        case table, count, records, line, source, record, facts    }    init(from decoder: Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        id = try c.decode(String.self, forKey: .id)        kind = try c.decode(String.self, forKey: .kind)        shown = try c.decode(Bool.self, forKey: .shown)        why = try c.decodeIfPresent(Why.self, forKey: .why)        settings = try c.decodeIfPresent(JSONValue.self, forKey: .settings) ?? .null        heading = try c.decodeIfPresent(String.self, forKey: .heading)        switch kind {        case "head":            tags = try c.decodeIfPresent([String].self, forKey: .tags) ?? []            let rows = try c.decode(Rows<TodayAnswer.Asked.Item>.self, forKey: .asked)            headAsked = rows.items            headAskedTotal = rows.total ?? 0            headDone = try c.decode(Rows<TodayAnswer.Done>.self, forKey: .done).items        case "notices": notices = try c.decodeIfPresent([TodayAnswer.Notice].self, forKey: .items) ?? []        case "asked": asked = try c.decodeIfPresent([TodayAnswer.Asked.Item].self, forKey: .items) ?? []        case "done": done = try c.decodeIfPresent([TodayAnswer.Done].self, forKey: .items) ?? []        case "device": card = try c.decodeIfPresent(String.self, forKey: .card)        case "today":            cards = try c.decodeIfPresent([String].self, forKey: .cards) ?? []            quiet = try c.decodeIfPresent(String.self, forKey: .quiet)        case "list":            table = try c.decodeIfPresent(String.self, forKey: .table)            count = try c.decodeIfPresent(Int.self, forKey: .count)            records = (try c.decodeIfPresent([CoreRecords.WireRecord].self, forKey: .records) ?? []).map(\.record)        case "figure": line = try c.decodeIfPresent(String.self, forKey: .line)        case "facts":            source = try c.decodeIfPresent(String.self, forKey: .source)            record = try c.decodeIfPresent(String.self, forKey: .record)            facts = try c.decodeIfPresent([Fact].self, forKey: .facts) ?? []        case "tools": tools = try c.decodeIfPresent([FanAnswer.Tile].self, forKey: .cards) ?? []        default: break        }    }}/// The size of screen this build asks the core's board for: a board's variant.enum BoardScreen {    static var size: String? {        #if os(macOS)        "mac"        #else        nil        #endif    }}