// A column's value as the TYPED thing a field card holds, and back.//// A field card is the place a value is read AND the place it is changed: tap// the card, the tray deals the answers, the pick is written. So a card may only// show what it can write back. `FieldValue` is that promise as a type — the// card draws from a `FieldValue`, never from the stored `RecordValue`, and// every case here has one stored form (`stored(for:)`) that reads back to// itself. A value this build cannot read into a case is `unread`: the card// says it holds something and takes no tap, because a card that drew such a// value as empty would invite the person to overwrite it.//// Pure over values and a calendar; `FieldValueTests` holds the round trip for// every column type.importCottonKitimportCottonValuesimportFoundationenumFieldValue: Equatable, Sendable {/// Nothing is stored: the card is its outline.case empty/// Something is stored that no case below reads. Shown as held, never edited.case unreadcase when(WhenValue)case clock(ClockTime)case text(String)/// Several words in one column (`multiple`): a person's phones.case texts([String])case number(Double)case amount(AmountValue)case choice(String)case choices([String])case link(String)case links([String])case attachment(AttachmentValue)case flag(Bool)/// Whether the card has a value to draw.var isSet: Bool {switchself {case .empty, .unread: returnfalsedefault: returntrue } }/// Reads a column's stored value. `currency` is the code the column's/// currency column holds on this record, when the column is an amount.staticfunc read( _ value: RecordValue, column: ColumnDefinition, currency: String? = nil, calendar: Calendar = .current ) -> FieldValue {ifcase .null = value { return .empty }ifcase .text(let text) = value, text.isEmpty { return .empty }ifcase .list(let items) = value, items.isEmpty { return .empty }switch column.type {// A place reads as its words.case .text, .accountNumber, .longtext, .url, .place:if column.multiple { return many(value, column: column, one: FieldValue.text, several: FieldValue.texts) }guardcase .text(let text) = value else { return .unread }return .text(text)case .date:guardlet when = WhenValue.read(value, calendar: calendar) else { return .unread }// A month on its own (a card's expiry) reads as its first day and// would be written back as a day: held, not edited here.ifcase .text(let raw) = value, FaceText.month(raw, calendar: calendar) != nil { return .unread }return .when(when)case .time:returnClockTime.read(value).map(FieldValue.clock) ?? .unreadcase .bytes:guardcase .number(let number) = value, BytesReading.read(number) != nilelse { return .unread }return .number(number)case .number:guardcase .number(let number) = value else { return .unread }return .number(number)case .amount:returnAmountValue.read(value, currency: currency).map(FieldValue.amount) ?? .unreadcase .choice:return many(value, column: column, one: FieldValue.choice, several: FieldValue.choices)case .link:return many(value, column: column, one: FieldValue.link, several: FieldValue.links)case .photo, .file:guardlet file = AttachmentValue.read(value), file.stored(for: column.type) == value else { return .unread }return .attachment(file)case .bool:guardcase .bool(let on) = value else { return .unread }return .flag(on) } }privatestaticfunc many( _ value: RecordValue, column: ColumnDefinition, one: (String) -> FieldValue, several: ([String]) -> FieldValue ) -> FieldValue {if column.multiple {guardcase .list(let items) = value else { return .unread }var texts: [String] = []for item in items {// Only words: a number in the list is not a choice or an id.guardcase .text(let text) = item else { return .unread } texts.append(text) }return several(texts) }guardcase .text(let text) = value else { return .unread }return one(text) }/// The stored form of this value, or nil for `unread`, which has none — the/// card that holds one never writes.func stored(for column: ColumnDefinition, calendar: Calendar = .current) -> RecordValue? {switchself {case .empty: return .nullcase .unread: returnnilcase .when(let when): return when.stored(calendar: calendar)case .clock(let time): return time.storedcase .text(let text): return .text(text)case .texts(let texts): return .list(texts.map(RecordValue.text))case .number(let number): return .number(number)case .amount(let amount): return amount.storedcase .choice(let choice): return .text(choice)case .choices(let choices): return .list(choices.map(RecordValue.text))case .link(let id): return .text(id)case .links(let ids): return .list(ids.map(RecordValue.text))case .attachment(let file): return file.stored(for: column.type)case .flag(let on): return .bool(on) } }}