import FoldFormat import Foundation public enum ModelLibraryError: Error, Equatable, Sendable { case noModel(named: String) } /// The shipped models: `Resources/.fold`, each generated by `foldc` from /// `Models/.foldscript` and `Models/-cp.fold`. public enum ModelLibrary { /// Every model, in the order of `names()`. public static func all() throws -> [FoldFile] { try names().map(model(named:)) } /// The models' names, sorted. public static func names() throws -> [String] { try FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil) .filter { $0.pathExtension == "fold" } .map { $0.deletingPathExtension().lastPathComponent } .sorted() } public static func model(named name: String) throws -> FoldFile { let url = directory.appendingPathComponent("\(name).fold") guard FileManager.default.fileExists(atPath: url.path) else { throw ModelLibraryError.noModel(named: name) } return try FoldFile(contentsOf: url) } /// The resource bundle's directory, where the package places `Resources/`'s files. A bundle /// holding a directory named `Resources` fails code signing on iOS. private static var directory: URL { Bundle.module.resourceURL! } }