Cotton code
48 lines · 2.1 KB · SwiftRaw
// The synthetic household previews and the package's tests draw: the kit's// tables as a database that imported them answers them// (`Resources/Fixtures/catalogue.json`, decoded by the core), and its records// (`Resources/Fixtures/<id>.json`). A database never draws these.//// Read through `CottonUIResources.bundle`, never `Bundle.module` directly: the Mac// finds the bundle by path (`Resources.swift`).import CottonKitimport CottonValuesimport Foundationpublic enum FixtureDefinitions {    /// The synthetic household's tables, sorted by id, as the core decoded    /// them; `failures` names any this build cannot draw.    public static func load() -> (definitions: [TableDefinition], failures: [String]) {        load(from: CottonUIResources.bundle)    }    /// The synthetic household's tables as `bundle` holds them. Empty when the    /// bundle carries none; a fixture this build cannot read is named in    /// `failures`.    static func load(from bundle: Bundle?) -> (definitions: [TableDefinition], failures: [String]) {        do {            guard let fixture = try DefinitionCatalogue.fixture(in: bundle) else { return ([], []) }            return (fixture.tables, fixture.failures)        } catch {            return ([], ["\(error)"])        }    }    /// The synthetic records for one table, or none.    public static func fixtureRecords(for table: String) throws -> [TableRecord] {        try fixtureRecords(for: table, bundle: CottonUIResources.bundle)    }    static func fixtureRecords(for table: String, bundle: Bundle?) throws -> [TableRecord] {        guard let url = bundle?.url(forResource: table, withExtension: "json", subdirectory: "Fixtures") else { return [] }        return try JSONDecoder().decode([TableRecord].self, from: readBundledJSON(url))    }    /// Bundled JSON is bounded; refuse a packaging error before buffering a large file.    /// The fixture catalogue — every kit table in one answer — is the one    /// bigger file and passes its own bound.    static func readBundledJSON(_ url: URL, limit: Int = 1_048_576) throws -> Data {        try BundledJSON.read(url, limit: limit)    }}