Cotton code
44 lines · 1.9 KB · SwiftRaw
// A tile figure as the core answers it (`summarize_records`): the op and// column it was asked for, the value over the rows its where kept, and the// currency an amount sum carries. The arithmetic and the words live in the// crate; this side only draws them.import CottonValuesimport Foundationpublic struct SummaryFigure: Hashable, Sendable, Decodable {    public var op: SummaryOp    public var column: String?    public var label: String?    public var value: RecordValue    public var currency: String?    /// The figure as the core wrote it, label and all    /// (`cotton_core::records::summary_words`): what the face draws.    public var words: String?    public init(op: SummaryOp, column: String? = nil, label: String? = nil, value: RecordValue, currency: String? = nil, words: String? = nil) {        self.op = op        self.column = column        self.label = label        self.value = value        self.currency = currency        self.words = words    }    enum CodingKeys: String, CodingKey { case op, column, label, value, currency, words }    public init(from decoder: Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        op = try c.decode(SummaryOp.self, forKey: .op)        column = try c.decodeIfPresent(String.self, forKey: .column)        label = try c.decodeIfPresent(String.self, forKey: .label)        value = try c.decodeIfPresent(RecordValue.self, forKey: .value) ?? .null        currency = try c.decodeIfPresent(String.self, forKey: .currency)        words = try c.decodeIfPresent(String.self, forKey: .words)    }    /// Every figure on one line: "£45.98 a month · 9 Oct next renewal".    public static func line(_ figures: [SummaryFigure], in definition: TableDefinition) -> String? {        let words = figures.compactMap(\.words).filter { !$0.isEmpty }        return words.isEmpty ? nil : words.joined(separator: " \u{00B7} ")    }}