Cotton code
58 lines · 2.1 KB · SwiftRaw
// Where a face, a list, a page or an editor finds the verbs and the definitions:// the environment, set once at the root of the interpreter's surfaces.import SwiftUI/// Every table definition the app knows, by id.public struct TableDefinitions: Sendable {    public var all: [TableDefinition]    private var byID: [String: TableDefinition]    /// False for the bundled set a window holds until the shown database's    /// own catalogue is read (`AppTables.core`, `DatabaseTableCatalog`): a    /// board read through it read tables the database may not have.    public let isRead: Bool    /// The definitions as read, as one number (this process's hash of them):    /// what a picture of a page was drawn under (`PagePictures`), so one drawn    /// under other definitions is drawn again. Nil before the read.    public let stamp: Int?    public init(_ all: [TableDefinition], read: Bool = true) {        self.all = all        self.byID = Dictionary(all.map { ($0.id, $0) }, uniquingKeysWith: { first, _ in first })        isRead = read        stamp = read ? all.hashValue : nil    }    public subscript(_ id: String?) -> TableDefinition? {        guard let id else { return nil }        return byID[id]    }    /// The table playing `role` (`role`), the first of them; nil where none    /// does.    public func playing(_ role: TableRole) -> TableDefinition? {        all.first { $0.role == role }    }}private struct RecordsKey: EnvironmentKey {    static let defaultValue: (any Records)? = nil}private struct TableDefinitionsKey: EnvironmentKey {    /// No definitions handed down yet is no answer: unread, so nothing    /// refuses a table on it (`UnknownTable`).    static let defaultValue = TableDefinitions([], read: false)}extension EnvironmentValues {    public var records: (any Records)? {        get { self[RecordsKey.self] }        set { self[RecordsKey.self] = newValue }    }    public var tableDefinitions: TableDefinitions {        get { self[TableDefinitionsKey.self] }        set { self[TableDefinitionsKey.self] = newValue }    }}