// 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. import CottonKit import CottonValues import Foundation enum FieldValue: 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 unread case 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 { switch self { case .empty, .unread: return false default: return true } } /// 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. static func read( _ value: RecordValue, column: ColumnDefinition, currency: String? = nil, calendar: Calendar = .current ) -> FieldValue { if case .null = value { return .empty } if case .text(let text) = value, text.isEmpty { return .empty } if case .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) } guard case .text(let text) = value else { return .unread } return .text(text) case .date: guard let 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. if case .text(let raw) = value, FaceText.month(raw, calendar: calendar) != nil { return .unread } return .when(when) case .time: return ClockTime.read(value).map(FieldValue.clock) ?? .unread case .bytes: guard case .number(let number) = value, BytesReading.read(number) != nil else { return .unread } return .number(number) case .number: guard case .number(let number) = value else { return .unread } return .number(number) case .amount: return AmountValue.read(value, currency: currency).map(FieldValue.amount) ?? .unread case .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: guard let file = AttachmentValue.read(value), file.stored(for: column.type) == value else { return .unread } return .attachment(file) case .bool: guard case .bool(let on) = value else { return .unread } return .flag(on) } } private static func many( _ value: RecordValue, column: ColumnDefinition, one: (String) -> FieldValue, several: ([String]) -> FieldValue ) -> FieldValue { if column.multiple { guard case .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. guard case .text(let text) = item else { return .unread } texts.append(text) } return several(texts) } guard case .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? { switch self { case .empty: return .null case .unread: return nil case .when(let when): return when.stored(calendar: calendar) case .clock(let time): return time.stored case .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.stored case .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) } } }