Cotton code
161 lines · 6.9 KB · SwiftRaw
// A whole keeps its parts on the next fan tier; the hub closes one level.import CottonValuesimport Foundation/// One whole the fan stands inside: its record, and its title as its card/// said it when it was opened.public struct WholeLevel: Hashable, Sendable {    public let table: String    public let id: String    public let title: String    public init(table: String, id: String, title: String) {        self.table = table        self.id = id        self.title = title    }}/// One part as the fan deals it: the part's own record (the one its deck/// holds), and the record it stands for — itself, or with `through` the/// record its link names (a playlist's entry stands for its track).public struct WholePart: Hashable, Sendable {    /// The part's own id, in the parts' table.    public let part: String    /// The record the fan shows for it.    public let table: String    public let id: String    public var title: String}/// One word of the crumb line and where its tap goes: the depth of wholes/// left standing (0 is the tool's own deck), nil for where the fan stands.public struct WholeCrumb: Hashable, Sendable {    public let word: String    public let depth: Int?}enum WholePath {    // MARK: The parts    /// The narrowing a whole's parts are read with: the parts whose `by`    /// link names the whole.    static func filter(_ opens: DefinitionCardOpens, whole id: String) -> [String: RecordValue] {        [opens.by: .text(id)]    }    /// The parts in the order `order` deals them: by its first column, a tie    /// by the next, and so on; a part with nothing in a column comes after    /// one with something; a full tie keeps the order they were read in.    static func ordered(_ parts: [TableRecord], by order: [String]) -> [TableRecord] {        guard !order.isEmpty else { return parts }        return parts.enumerated().sorted { a, b in            for key in order {                let (x, y) = (a.element[key], b.element[key])                switch (RecordValue.isBlank(x), RecordValue.isBlank(y)) {                case (false, true): return true                case (true, false): return false                case (true, true): continue                case (false, false):                    if TableDefinition.precedes(x, y) { return true }                    if TableDefinition.precedes(y, x) { return false }                }            }            return a.offset < b.offset        }.map(\.element)    }    /// The order a deck of the parts is drawn in, as a list sort holds it:    /// the first `order` column, a tie by the second. Nil with no order.    static func sort(_ order: [String]) -> ListSort? {        guard let first = order.first else { return nil }        var sort = ListSort(_generated: (), column: first)        sort.tie = order.dropFirst().first        return sort    }    /// The parts' table as the page deals them: in the whole's order, which    /// stands over the table's own (a drag order included).    static func dealt(_ parts: TableDefinition, by opens: DefinitionCardOpens) -> TableDefinition {        guard let sort = sort(opens.order) else { return parts }        var dealt = parts        dealt.order = nil        dealt.sort = sort        return dealt    }    /// What each part stands for, in the parts' order: itself, or with    /// `through` the record its link names in the link's table. A part whose    /// link names nothing is left out — the fan never deals a blank card.    static func standing(_ parts: [TableRecord], opens: DefinitionCardOpens, in definition: TableDefinition?) -> [WholePart] {        guard let through = opens.through else {            return parts.map { WholePart(part: $0.id, table: opens.table, id: $0.id, title: title(of: $0, in: definition)) }        }        guard let linked = definition?.column(through)?.linkTo else { return [] }        return parts.compactMap { part in            guard let id = FaceText.linkIDs(part[through]).first else { return nil }            // Its own title until the linked record is read.            return WholePart(part: part.id, table: linked, id: id, title: title(of: part, in: definition))        }    }    /// Where a part's card on the fan goes: into it, when the record it    /// stands for is a whole of its own (its card `opens`); else that part is    /// brought to the front of the deck. The definitions decide, never whether    /// the part's own parts have been read yet.    static func entered(_ part: WholePart, in definitions: TableDefinitions) -> WholeLevel? {        guard definitions[part.table]?.card?.opens != nil else { return nil }        return WholeLevel(table: part.table, id: part.id, title: part.title)    }    /// A record's title as its card says it, else its title column.    static func title(of record: TableRecord, in definition: TableDefinition?) -> String {        DeckCard(record)?.text("title") ?? record[definition?.title].text ?? ""    }    // MARK: The path    /// The path with `whole` opened inside where it stands. A whole already    /// on the path is gone back to, never opened twice.    static func entering(_ whole: WholeLevel, _ path: [WholeLevel]) -> [WholeLevel] {        if let at = path.firstIndex(where: { $0.table == whole.table && $0.id == whole.id }) {            return Array(path.prefix(at + 1))        }        return path + [whole]    }    /// A step out to `depth` wholes: what stands, and the whole just left,    /// whose card the deck under it opens at.    struct Leaving: Equatable {        let path: [WholeLevel]        let opensAt: WholeLevel    }    /// Out to `depth` wholes (0: the tool's own deck); nil where the fan    /// already stands there or deeper is asked.    static func leaving(_ path: [WholeLevel], to depth: Int) -> Leaving? {        guard depth >= 0, depth < path.count else { return nil }        return Leaving(path: Array(path.prefix(depth)), opensAt: path[depth])    }    /// The back card's step: one level up; from the top level, out of the    /// whole to the tool's own deck.    static func back(_ path: [WholeLevel]) -> Leaving? {        leaving(path, to: path.count - 1)    }    /// The crumb line: the tool, the first whole's table, then each whole    /// by its title, the last where the fan stands. The tool and the table    /// both go back to the tool's own deck; one word the same as the one    /// before it is said once ("Books › Dune", never twice).    static func crumbs(tool: String, path: [WholeLevel], plural: (String) -> String?) -> [WholeCrumb] {        guard let first = path.first else { return [] }        var crumbs = [WholeCrumb(word: tool, depth: 0)]        if let table = plural(first.table), table != tool { crumbs.append(WholeCrumb(word: table, depth: 0)) }        for (at, whole) in path.enumerated() {            crumbs.append(WholeCrumb(word: whole.title, depth: at + 1 == path.count ? nil : at + 1))        }        return crumbs    }}