Cotton code
50 lines · 2.1 KB · SwiftRaw
// What the faces read off a card beyond its words: a place holding several// entries (an event's people), a place holding a list of words (a mood's// emotions), cut where the core joined them, and a card made from its parts —// the shape a specimen or a test hands a face, exactly as the core would have// resolved it under `@card`.import CottonValuesextension DeckCard {    public init(face: DefinitionCardKind, corner: String? = nil, pressing: Bool = false, fields: [String: RecordValue]) {        self.init(face: face.rawValue, corner: corner, pressing: pressing, pace: nil, plate: nil, fields: fields)    }    /// The card with what the deck reads for it beyond the core's words (its    /// pages, labels, history…): the shape a specimen hands a face, exactly    /// as the deck would have filled it.    public func reading(_ read: (inout DeckCard) -> Void) -> DeckCard {        var card = self        read(&card)        return card    }    /// A place that holds several entries (an event's people): each entry's    /// own places.    public func entries(_ slot: String) -> [[String: RecordValue]] {        guard case .list(let items)? = fields[slot] else { return [] }        return items.compactMap { item in            if case .object(let entry) = item { return entry }            return nil        }    }    /// A place that holds a list of words (a mood's emotions, a meeting's    /// speakers): each as the core said it, in its order; words the core    /// joined into one (`cards` `join`) are cut where it joined them.    public func words(_ slot: String) -> [String] {        switch fields[slot] {        case .list(let items)?: return items.compactMap(\.text)        case .text(let joined)?: return Self.chips(joined)        default: return []        }    }    /// The parts the core joined into a place (`join`), each its own chip.    public static func chips(_ words: String?) -> [String] {        guard let words else { return [] }        let join = ShellLines.line("cards", "join")        return words.components(separatedBy: join).map { $0.trimmingCharacters(in: .whitespaces) }.filter { !$0.isEmpty }    }}