Cotton code
179 lines · 7.6 KB · SwiftRaw
// ONE SLIP ANSWER, as the core said it (`shell_slip.rs`): what a record says// wherever it is drawn. Every record read through the core carries it under// `@said`, and a list's row answers the same words (`ListAnswer.Row`), so a// face draws what it holds, whole on its first paint, and decides none of it.// Plain JSON, the same answer a Kotlin shell reads.import CottonValuesimport Foundation/// A record's answer, beside its row's acts.public struct RecordSaid: Equatable, Sendable {    public var slip: SlipWords    /// Whether the slip's check is on; nil on a table with no check.    public var toggled: Bool?    /// What a tap on the check writes, `{column: value}`; nil, nothing.    public var toggle: [String: RecordValue]?    public var dimmed: Bool    /// The stamp the record wears, by its word (done, dropped, a choice's stamp).    public var stamp: String?    /// Whether Delete may take it.    public var deletes: Bool    /// Nothing about the record can be written.    public var readOnly: Bool    /// A typed sentence could change it.    public var typesInto: Bool    public var archived: Bool    /// It stands on its table's own list.    public var listed: Bool    /// The project it is filed under.    public var project: String?    /// The slip its `slip_by` variant draws; nil, the table's own.    public var layout: SlipLayout?    /// The columns whose value its `red_when` holds.    public var red: [String]    /// The row's own words its columns' units name.    public var unitWords: [String: String]    /// What is drawn for a record no core has said anything of.    public static let nothing = RecordSaid(slip: SlipWords([:]), toggled: nil, toggle: nil, dimmed: false, stamp: nil, deletes: false,                                           readOnly: false, typesInto: false, archived: false, listed: true, project: nil)    public init(slip: SlipWords, toggled: Bool?, toggle: [String: RecordValue]?, dimmed: Bool, stamp: String?, deletes: Bool,                readOnly: Bool, typesInto: Bool, archived: Bool, listed: Bool, project: String?,                layout: SlipLayout? = nil, red: [String] = [], unitWords: [String: String] = [:]) {        self.slip = slip        self.toggled = toggled        self.toggle = toggle        self.dimmed = dimmed        self.stamp = stamp        self.deletes = deletes        self.readOnly = readOnly        self.typesInto = typesInto        self.archived = archived        self.listed = listed        self.project = project        self.layout = layout        self.red = red        self.unitWords = unitWords    }    init?(_ value: RecordValue) {        guard case .object(let said) = value, case .object(let slip)? = said["slip"] else { return nil }        var toggle: [String: RecordValue]?        if case .object(let write)? = said["toggle"] { toggle = write }        var layout: SlipLayout?        if let drawn = said["layout"], let data = try? JSONEncoder().encode(drawn) { layout = try? JSONDecoder().decode(SlipLayout.self, from: data) }        var red: [String] = []        if case .list(let columns)? = said["red"] { red = columns.compactMap(\.text) }        var unitWords: [String: String] = [:]        if case .object(let words)? = said["unit_words"] { unitWords = words.compactMapValues(\.text) }        self.init(slip: SlipWords(slip), toggled: said["toggled"]?.bool, toggle: toggle, dimmed: said["dimmed"]?.bool ?? false,                  stamp: said["stamp"]?.text, deletes: said["deletes"]?.bool ?? false, readOnly: said["read_only"]?.bool ?? false,                  typesInto: said["types_into"]?.bool ?? false, archived: said["archived"]?.bool ?? false, listed: said["listed"]?.bool ?? true, project: said["project"]?.text,                  layout: layout, red: red, unitWords: unitWords)    }}/// The words a slip draws.public struct SlipWords: Equatable, Sendable {    public struct Stub: Equatable, Sendable {        public let day: RecordValue?        public let month: RecordValue?    }    public struct Byline: Equatable, Sendable {        public let words: String        public let red: Bool    }    public struct Badge: Equatable, Sendable {        public let symbol: String?        public let spoken: String?    }    public struct Pin: Equatable, Sendable {        public let lat: Double        public let lng: Double    }    public let title: String?    public let subtitle: String?    /// One line where `subtitle` joins several columns.    public let subtitle_one: String?    public let caption: String?    public let caption_late: Bool?    public let figure: RecordValue?    public let foot: RecordValue?    public let foot_late: RecordValue?    public let day_foot: String?    /// The trailing column's words, away from a list.    public let trailing: String?    public let stub: Stub?    public let byline: Byline?    public let badge: Badge?    public let unread: Bool    /// The record's own title alone.    public let name: String?    /// What names it where its title holds nothing.    public let fallback: String?    /// Never empty, on a record the core has said.    public let spoken: String?    /// The day heading the record, where it has no title of its own.    public let weekday: String?    /// What a cover says at its foot, part by part.    public let cover_footer: [String]?    public let stock: String?    public let pin: Pin?    /// The project's name, where the slip does not already say it.    public let project_words: String?    /// What heads a page or a list opened from the record.    public var heading: String? { name ?? fallback }    init(_ said: [String: RecordValue]) {        title = said["title"]?.text        subtitle = said["subtitle"]?.text        subtitle_one = said["subtitle_one"]?.text        caption = said["caption"]?.text        caption_late = said["caption_late"]?.bool        figure = said["figure"]        foot = said["foot"]        foot_late = said["foot_late"]        day_foot = said["day_foot"]?.text        trailing = said["trailing"]?.text        if case .object(let stub)? = said["stub"] { self.stub = Stub(day: stub["day"], month: stub["month"]) } else { stub = nil }        if case .object(let byline)? = said["byline"], let words = byline["words"]?.text {            self.byline = Byline(words: words, red: byline["red"]?.bool ?? false)        } else { byline = nil }        if case .object(let mark)? = said["badge"] {            self.badge = Badge(symbol: mark["symbol"]?.text, spoken: mark["spoken"]?.text)        } else { badge = nil }        unread = said["unread"]?.bool ?? false        name = said["name"]?.text        fallback = said["fallback"]?.text        spoken = said["spoken"]?.text        weekday = said["weekday"]?.text        if case .list(let parts)? = said["cover_footer"] { cover_footer = parts.compactMap(\.text) } else { cover_footer = nil }        stock = said["stock"]?.text        if case .object(let pin)? = said["pin"], let lat = pin["lat"]?.number, let lng = pin["lng"]?.number {            self.pin = Pin(lat: lat, lng: lng)        } else { pin = nil }        project_words = said["project_words"]?.text    }}extension TableRecord {    /// Where the core puts a record's answer.    public static let saidKey = "@said"    /// The record's answer as the core said it; nil on a record no core read.    public var said: RecordSaid? { RecordSaid(self[Self.saidKey]) }    /// The ink a column's words take on this record: red where the core    /// says its value is, else `fallback`.    public func ink(_ key: String?, fallback: PaperInk = .ink) -> PaperInk {        guard let key, said?.red.contains(key) == true else { return fallback }        return .red    }}