// One record of a table, as the interpreter holds it: an id and its column// values as typed JSON. The shared format stores a user table's columns as JSON// (`records.data`); a built-in table answers the same shape through its engine.// With the words, numbers and days the core resolved beside them. Plain// values: what a write answers (`ChangeRecord`) stays the app's.importFoundation/// A column's value as the core resolved it for a face: the words, or nil/// when the core read it and it says nothing. A face draws these as given.publicstructResolvedWords: Hashable, Sendable {publicvar said: String?publicinit(said: String?) { self.said = said }}/// A counted number as a card draws it (`page::number_parts`): the figure/// short enough to stand on the card ("4.2K") and the word under it.publicstructNumberParts: Hashable, Sendable {publicvar figure: Stringpublicvar unit: String}/// A stored date as the core resolved it (`page::DayParts`): every surface/// draws these parts and never reads a calendar for words.publicstructDayParts: Hashable, Sendable {/// The day as a key ("2026-10-15"), for comparing and grouping days.publicvar day: String/// "Today", "Friday": within a week either side, else nil.publicvar near: String?/// "11 Oct", the year outside the reader's.publicvar dated: String/// A page headed by its day: "Wednesday" over "1 October".publicvar weekdayName: Stringpublicvar dayLong: String/// "Tue 6 Oct", the year outside the reader's.publicvar weekdayDate: String/// The heading over a day's records: "Today", else the weekday date.publicvar heading: String/// Where no heading names the day: the clock today, "Yesterday", else the weekday date.publicvar recent: String/// Under a heading naming the day: the clock, or "All day".publicvar underDay: Stringpublicvar clock: String?/// "2 days late" or "late"; nil when not behind.publicvar late: String?/// The month as a key ("2026-09") and its heading ("September").publicvar month: Stringpublicvar monthTitle: Stringpublicvar leafDay: Intpublicvar leafMonth: String/// A date with an `until`, as one range ("1 Jul – 31 Jul 2026").publicvar range: String?/// A yearly date by its next time round: "Turns 34 on Mon 17 Aug", and/// on a card, whose leaf says the day, "turns 34 tomorrow".publicvar yearly: String?publicvar yearlyCard: String?/// The weekday date and the clock ("Mon 5 Oct · 3:24 am"), or the day alone.publicvar full: String/// "5 min ago", "3 h ago", "yesterday" within two days of a timed moment.publicvar ago: String?publicinit?(_ value: RecordValue) {guardcase .object(let o) = value, let day = o["day"]?.text, let dated = o["dated"]?.text,let weekdayDate = o["weekday_date"]?.text, let weekdayName = o["weekday_name"]?.text,let dayLong = o["day_long"]?.text, let heading = o["heading"]?.text,let recent = o["recent"]?.text, let underDay = o["under_day"]?.text,let month = o["month"]?.text, let monthTitle = o["month_title"]?.text,let leafDay = o["leaf_day"]?.number, let leafMonth = o["leaf_month"]?.text else { returnnil } (self.day, self.near, self.dated, self.weekdayDate, self.heading) = (day, o["near"]?.text, dated, weekdayDate, heading) (self.recent, self.underDay, self.clock, self.late) = (recent, underDay, o["clock"]?.text, o["late"]?.text) (self.weekdayName, self.dayLong, self.range) = (weekdayName, dayLong, o["range"]?.text) (self.yearly, self.yearlyCard) = (o["yearly"]?.text, o["yearly_card"]?.text) (self.full, self.ago) = (o["full"]?.text ?? weekdayDate, o["ago"]?.text) (self.month, self.monthTitle, self.leafDay, self.leafMonth) = (month, monthTitle, Int(leafDay), leafMonth) }}publicstructTableRecord: Hashable, Sendable, Identifiable, Codable {publicvar id: Stringpublicvar values: [String: RecordValue]publicinit(id: String, values: [String: RecordValue]) {self.id = idself.values = values }/// The byline a shared database's record carries from `records.get`: the/// name of the person whose device last wrote it. Not a column of any/// definition; the page's Edited line reads it.publicstaticlet editedByKey = "edited_by"/// Set false by the core on a record it will not delete alone (a later/// occurrence of a repeating event: deleting it would take the series)./// Not a column of any definition; absent means deletable.publicstaticlet deletableKey = "deletable"/// Whether the core lets this record be deleted on its own.publicvar deletable: Bool { self[Self.deletableKey].bool != false }/// The rollup columns whose source tool is not installed in this/// database (`records.get`'s `computed_unavailable`), each as the core/// sent it: `{table, words}`. Not a column of any definition.publicstaticlet unavailableKey = "computed_unavailable"/// Where the core puts each typed column's face words/// (`cotton_core::records::page::column_words`).publicstaticlet columnWordsKey = "@column_words"/// Where the core puts each date column's parts (`page::day_parts`).publicstaticlet dayPartsKey = "@day_parts"/// A date column's parts as the core said them, or nil.publicfunc dayParts(_ key: String?) -> DayParts? {guardlet key, case .object(let days) = self[Self.dayPartsKey], let day = days[key] else { returnnil }returnDayParts(day) }/// A counted number column's figure and unit as the core said them, or nil.publicfunc numberParts(_ key: String?) -> NumberParts? {guardlet key, case .object(let numbers) = self["@number_parts"], case .object(let parts)? = numbers[key],let figure = parts["figure"]?.text else { returnnil }returnNumberParts(figure: figure, unit: parts["unit"]?.text ?? "") }/// The day an upcoming list files this record under (`@slip_day`).publicvar upcomingDay: DayParts? { DayParts(self["@slip_day"]) }/// The core's words for a column, or nil when the core did not read it.publicfunc columnWords(_ key: String?) -> ResolvedWords? {guardlet key else { returnnil }ifcase .object(let words) = self[Self.columnWordsKey], let said = words[key] { returnResolvedWords(said: said.text) }// A yearly day is said by its next time round (`DayParts.yearly`).iflet yearly = dayParts(key)?.yearly { returnResolvedWords(said: yearly) }returnnil }publicsubscript(_ key: String?) -> RecordValue {guardlet key else { return .null }return values[key] ?? .null }/// A fixture or FFI record is one flat JSON object: `id` plus the columns.publicinit(from decoder: Decoder) throws {var all = try decoder.singleValueContainer().decode([String: RecordValue].self)guardcase .text(let id)? = all.removeValue(forKey: "id") else {throwDecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "a record needs a text id")) }self.id = idself.values = all }publicfunc encode(to encoder: Encoder) throws {var all = values all["id"] = .text(id)var c = encoder.singleValueContainer()try c.encode(all) }}