Cotton code
90 lines · 3.6 KB · SwiftRaw
// Keys a record that is handed on reads, each drawn once for any table that// names it (Thoughts was the first: a capture that became one todo opens as// that todo, a filed capture names under its slip what it became, a capture's// pictures are added and taken off on its page):////   * `hand_over` — a record whose `links` hold exactly ONE record opens as//     that record: its page replaces itself with that record's page, while//     the record holds what `when` names;//   * slip `links` — the record's `links` as hand links under its slip//     ("Todo ›"), each opening its record;//   * `attachments` — the strip under the page is editable: a picture added//     from where `sources` says, moved, captioned and taken off, the whole//     list written as the record's `attachments` through `update_record`.//// The reading lives here, pure, so what opens is settled by tests; the faces and surfaces only draw them.import CottonKitimport CottonValuesimport Foundation/// A record that opens as the one record it became (`hand_over`).public typealias RecordHandOver = DefinitionRecordHandOverextension DefinitionRecordHandOver {    public init(when: SectionWhen? = nil) {        self.init(_generated: ())        self.when = when    }}/// Where a picture added to a record's strip comes from.public typealias AttachmentSourceKey = DefinitionAttachmentSource/// A record's strip, editable (`attachments`).public typealias AttachmentsLayout = DefinitionAttachmentsLayoutextension DefinitionAttachmentsLayout {    public init(editable: Bool = true, sources: [AttachmentSourceKey] = [.library, .camera]) {        self.init(_generated: ())        self.editable = editable        self.sources = sources    }}extension TableDefinition {    /// The hand links under a slip: the record's links less the tables the    /// slip leaves out, none without slip `links`.    public func slipLinks(of record: TableRecord) -> [LinkedRecord] {        guard let links = slip.links else { return [] }        return linkedRecords(of: record, in: links)    }    /// The lines under a slip from another table (`slip.lines`): the core's    /// words, one string each, never composed here; none without the key.    public func slipRowLines(of record: TableRecord) -> [String] {        guard slip.lines != nil, case .list(let lines) = record["@slip_lines"] else { return [] }        return lines.compactMap(\.text).filter { !$0.isEmpty }    }    /// `update_record`'s patch for a strip: the whole list, in order, as the    /// record's `attachments` (each `{kind, id}`, and its caption).    static func attachmentsPatch(_ list: [RecordAttachment]) -> [String: RecordValue] {        attachmentsPatch(RecordAttachments.argument(list))    }    /// The same patch from the argument as written (a caption's list).    static func attachmentsPatch(_ argument: JSONValue) -> [String: RecordValue] {        ["attachments": RecordValue(argument)]    }}extension RecordValue {    /// A JSON value as a record holds it.    init(_ json: JSONValue) {        switch json {        case .null: self = .null        case .bool(let b): self = .bool(b)        case .number(let n): self = .number(n)        case .string(let s): self = .text(s)        case .array(let items): self = .list(items.map(RecordValue.init))        case .object(let fields): self = .object(fields.mapValues(RecordValue.init))        }    }}extension LinkedRecord {    /// A hand link's words: the kind's word, then the arrow ("Todo ›").    public var handLink: String { "\(BadgeKind.label(forKind: table)) \u{203A}" }}