Cotton code
52 lines · 2.4 KB · SwiftRaw
// A record's card (`CottonValues/DeckCard.swift`) read off the record the// core resolved it on (`@card`), and the card a record stands on when its// table names none. Saying a card the deck cannot read is the app's: the// value module logs nothing.import CottonValuesimport osimport SwiftUIextension DeckCard {    /// The record's card; nil when it carries none, and nil with a logged    /// `DeckCardFailure` when it carries one the deck cannot read — never a    /// silent nil, because the deck holds only rows with a card and an    /// unreadable one empties it while the strip still counts the rows.    public init?(_ record: TableRecord) {        switch Self.read(record[Self.key]) {        case nil: return nil        case .success(let card): self = card        case .failure(let failure):            DeckCardLog.unreadable(record.id, failure)            return nil        }    }    /// The card a record stands on when its table names no card at all (a    /// table the owner made): the plain face, headed by what the record is    /// called — never no card, which would leave it off its own deck. Nil    /// for a table with a card, whose record carries the core's.    static func plain(_ record: TableRecord, in definition: TableDefinition) -> DeckCard? {        definition.card == nil ? named(record, in: definition) : nil    }    /// The plain card headed by what `record` is called (`spokenName`): a    /// record read without the core's card.    static func named(_ record: TableRecord, in definition: TableDefinition) -> DeckCard {        DeckCard(face: DefinitionCardKind.plain.rawValue, corner: nil, pressing: false, pace: nil, plate: nil,                 fields: ["title": .text((record.said?.slip.spoken ?? definition.name))])    }}/// Where an unreadable card is said: once per record and failure, since the/// deck reads its rows' cards on every draw.enum DeckCardLog {    private static let log = Logger(subsystem: "com.cottondb.deck", category: "card")    private static let said = OSAllocatedUnfairLock(initialState: Set<String>())    static func unreadable(_ id: String, _ failure: DeckCardFailure) {        let key = "\(id)|\(failure)"        guard said.withLock({ $0.insert(key).inserted }) else { return }        log.fault("deck.card.unreadable:\(id, privacy: .public):\(String(describing: failure), privacy: .public)")    }}