import CottonKit import CottonValues import Foundation /// Engine tables keep app-only acts while their records come from the core's /// declared view. Native read failures propagate rather than rebuilding rows. enum EngineSettingsRows { private struct Page: Decodable { let records: [CoreRecords.WireRecord] let next_page: Int? } static func list(_ table: String, database: String? = nil, core: any CottonCalling) async throws -> [TableRecord] { var rows: [TableRecord] = [] var page = 0 repeat { var request: [String: RecordValue] = ["table": .text(table), "page": .number(Double(page)), "limit": .number(200)] if let database { request["database"] = .text(database) } let answer = try await core.call(CottonMethod.recordsList, request, as: Page.self) rows += answer.records.map(\.record) guard let next = answer.next_page else { return rows } guard next > page else { throw ActionRefused(ShellLines.line("chrome", "engine.the_next_page_could")) } page = next } while true } static func get(_ table: String, id: String, core: any CottonCalling) async throws -> TableRecord? { do { let request: [String: RecordValue] = ["table": .text(table), "id": .text(id)] return try await core.call(CottonMethod.recordsGet, request, as: CoreRecords.WireRecord.self).record } catch let error as CottonCallError where error.code == "record_not_found" { return nil } } } /// App capabilities may keep local state after a successful native command. protocol SettingsCommandFinishing: SettingsTableSource { func afterCommand(_ command: RecordCommand, record: TableRecord?, change: ChangeRecord, core: any CottonCalling) async -> ChangeRecord } /// Creation pages read required authority before offering their app capability. protocol SettingsCreationPreparing: SettingsTableSource { func prepareCreation(where filter: [String: RecordValue], core: any CottonCalling) async throws } protocol CreationRecords: Records { func prepareCreation(table: String, where filter: [String: RecordValue]) async throws }