Caps-Train/Sources/ConfigurationFile.swift

37 lines
841 B
Swift
Raw Normal View History

2023-10-23 14:58:58 +02:00
import Foundation
struct ConfigurationFile {
let contentFolder: String?
let trainingIterations: Int?
let serverPath: String?
let authenticationToken: String?
}
extension ConfigurationFile: Decodable {
}
extension ConfigurationFile {
init(at url: URL) throws {
guard FileManager.default.fileExists(atPath: url.path) else {
2023-10-23 16:49:56 +02:00
throw TrainingError.configurationFileMissing(url)
2023-10-23 14:58:58 +02:00
}
let data: Data
do {
data = try Data(contentsOf: url)
} catch {
2023-10-23 16:49:56 +02:00
throw TrainingError.configurationFileUnreadable(url, error)
2023-10-23 14:58:58 +02:00
}
do {
self = try JSONDecoder().decode(ConfigurationFile.self, from: data)
} catch {
2023-10-23 16:49:56 +02:00
throw TrainingError.configurationFileDecodingFailed(url, error)
2023-10-23 14:58:58 +02:00
}
}
}