Cotton code
163 lines · 6.0 KB · SwiftRaw
// What the deck reads for a card beyond the core's words (`DeckCard`'s// readings, filled by CottonUI's `DeckCardFill`): the pages it turns to, the// labels its places stand under, the songs a playlist pins, a delivery's states, the ways a person is reached. Plain// values, including the core's thread history, so a face draws from its card// alone and Loom from its specimens.import Foundation/// One page past a card's face: a column's long words under its label.public struct CardPage: Equatable, Sendable {    public let label: String    public let text: String    /// The column keeps Markdown: its page is set, never typed with its marks.    public var markdown = false    public init(label: String, text: String, markdown: Bool = false) {        self.label = label        self.text = text        self.markdown = markdown    }}/// Each place of a card under its column's label, as the package wrote it/// ("Address", "Due", "Price a plate"): the words a fact stands under.public struct CardLabels: Equatable, Sendable {    public var bySlot: [String: String] = [:]    public var byColumn: [String: String] = [:]    public init(bySlot: [String: String] = [:], byColumn: [String: String] = [:]) {        self.bySlot = bySlot        self.byColumn = byColumn    }    public subscript(slot slot: String) -> String? { bySlot[slot] }    public subscript(column column: String) -> String? { byColumn[column] }}/// The numbers and addresses a person's card is reached by, as the record/// holds them, and the channel their thread runs on.public struct CardReach: Equatable, Sendable {    public var phones: [String] = []    public var emails: [String] = []    public var channel: String?    /// The ways the app can reach them from these, as it worked them out    /// (CottonUI's `ContactHandoff`): the discs the face draws.    public var call = false    public var message = false    public var email = false    public init(phones: [String] = [], emails: [String] = [], channel: String? = nil,                call: Bool = false, message: Bool = false, email: Bool = false) {        self.phones = phones        self.emails = emails        self.channel = channel        self.call = call        self.message = message        self.email = email    }}/// What a thread's card says past its last message: how many messages there/// are since the first, in the core's words and the first one's year, and/// the few before the last.public struct ThreadHistory: Codable, Equatable, Sendable {    public struct Line: Codable, Equatable, Identifiable, Sendable {        public let id: String        public let words: String        public let mine: Bool        /// Present only when the card reader supplied these facts.        public let time: String?        public let sender: String?        public init(id: String, words: String, mine: Bool, time: String? = nil, sender: String? = nil) {            self.id = id            self.words = words            self.mine = mine            self.time = time            self.sender = sender        }    }    public var count: String?    public var earlier: [Line] = []    /// The last message with its direction and metadata, when supplied.    /// A card without local history may carry only its body preview.    public var last: Line?    public init(count: String? = nil, earlier: [Line] = [], last: Line? = nil) {        self.count = count        self.earlier = earlier        self.last = last    }}/// One song pinned on a playlist's J-card: its own cover, or, while it/// keeps none, its sleeve printed with its title and artist.public enum MusicTile: Hashable, Sendable {    case art(String)    case sleeve(title: String?, artist: String?)}/// The words a music whole's faces join (an album's, a playlist's, a/// cassette's facts).public enum MusicWhole {    /// Words joined as the cards join them; nil when there are none.    public static func joined(_ words: [String?]) -> String? {        let said = words.compactMap { $0 }.filter { !$0.isEmpty }        return said.isEmpty ? nil : said.joined(separator: ShellLines.line("cards", "join"))    }}/// The package's columns the kitchen's faces read state from.public enum KitchenColumns {    public static let delivered = "delivered"    public static let paid = "paid"    /// A delivery's plan: the customer's subscription it is a meal of.    public static let plan = "subscription"    public static let rating = "rating"}/// A delivery as its columns hold it: gone out or not, paid or not, and/// whether it is a meal on a plan.public struct KitchenDeliveryState: Equatable, Sendable {    public var delivered: Bool    public var paid: Bool    public var onPlan: Bool    public init(delivered: Bool, paid: Bool, onPlan: Bool) {        self.delivered = delivered        self.paid = paid        self.onPlan = onPlan    }    public static func of(_ record: TableRecord) -> KitchenDeliveryState {        KitchenDeliveryState(            delivered: record[KitchenColumns.delivered].bool == true,            paid: record[KitchenColumns.paid].bool == true,            onPlan: holds(record[KitchenColumns.plan]))    }    /// The plan tag: the plan column's own label, while the delivery is a    /// meal on one; nil off a plan.    public func planTag(_ labels: CardLabels) -> String? {        onPlan ? labels[column: KitchenColumns.plan] : nil    }    /// The states stamped on the card, each its column's label, in order:    /// Delivered, then Paid — only those its columns hold.    public func stamps(_ labels: CardLabels) -> [String] {        [(delivered, KitchenColumns.delivered), (paid, KitchenColumns.paid)]            .compactMap { held, column in held ? labels[column: column] : nil }    }    private static func holds(_ value: RecordValue) -> Bool {        switch value {        case .null: false        case .text(let text): !text.trimmingCharacters(in: .whitespaces).isEmpty        case .list(let items): !items.isEmpty        case .object(let fields): !fields.isEmpty        case .number, .bool: true        }    }}