Cotton code
77 lines · 3.4 KB · SwiftRaw
// What the core attaches to every record it reads, for fixtures that have no// core. Since the swiftdraw commits (e7dfdb915 … 80bf5dc71, 22 Oct 2026)// Swift spells no date, clock, count, size or duration: a read carries the// core's words — each date column's parts (`@day_parts`,// `cotton_core::records::page_dates`), each typed column's words// (`@column_words`) and a counted number's figure and unit (`@number_parts`)// — and the faces draw them. A test of how Swift files and places those// words gives its record what the core would have said; the words themselves// are the core's tests' to pin (`page_dates_tests.rs`).@testable import CottonUIimport CottonValuesenum CoreSaid {    /// One date column's parts as `page_dates::day_parts` says them. Only    /// what a test reads need be given; the rest default to the plain day.    static func day(        _ day: String,        heading: String? = nil,        near: String? = nil,        dated: String? = nil,        weekdayName: String? = nil,        dayLong: String? = nil,        weekdayDate: String? = nil,        recent: String? = nil,        underDay: String = "All day",        clock: String? = nil,        late: String? = nil,        monthTitle: String? = nil,        range: String? = nil,        full: String? = nil,        ago: String? = nil    ) -> RecordValue {        let weekdayDate = weekdayDate ?? dated ?? day        var parts: [String: RecordValue] = [            "day": .text(day),            "dated": .text(dated ?? weekdayDate),            "weekday_name": .text(weekdayName ?? weekdayDate),            "day_long": .text(dayLong ?? dated ?? weekdayDate),            "weekday_date": .text(weekdayDate),            "heading": .text(heading ?? near ?? weekdayDate),            "recent": .text(recent ?? clock ?? weekdayDate),            "under_day": .text(clock ?? underDay),            "month": .text(String(day.prefix(7))),            "month_title": .text(monthTitle ?? String(day.prefix(7))),            "leaf_day": .number(Double(day.suffix(2)) ?? 1),            "leaf_month": .text(String(day.prefix(7))),            "full": .text(full ?? [weekdayDate, clock].compactMap { $0 }.joined(separator: " \u{00B7} ")),        ]        for (key, value) in ["near": near, "clock": clock, "late": late, "range": range, "ago": ago] {            if let value { parts[key] = .text(value) }        }        return .object(parts)    }    /// `record` as a read hands it over: its date columns' parts, its typed    /// columns' words and its counted numbers' figures, as the core said them.    static func read(        _ record: TableRecord,        days: [String: RecordValue] = [:],        words: [String: String] = [:],        numbers: [String: (figure: String, unit: String)] = [:]    ) -> TableRecord {        var record = record        func merge(_ key: String, _ more: [String: RecordValue]) {            guard !more.isEmpty else { return }            var all: [String: RecordValue] = [:]            if case .object(let had) = record.values[key] ?? .null { all = had }            all.merge(more) { $1 }            record.values[key] = .object(all)        }        merge(TableRecord.dayPartsKey, days)        merge(TableRecord.columnWordsKey, words.mapValues { .text($0) })        merge("@number_parts", numbers.mapValues { .object(["figure": .text($0.figure), "unit": .text($0.unit)]) })        return record    }}