Caps-Train/Sources/ConfigurationFile.swift

37 lines
817 B
Swift
Raw Normal View History

2023-10-23 14:58:58 +02:00
import Foundation
struct ConfigurationFile {
2023-10-24 11:31:08 +02:00
let folder: String?
2023-10-23 14:58:58 +02:00
2023-10-24 11:31:08 +02:00
let iterations: Int?
2023-10-23 14:58:58 +02:00
2023-10-24 11:31:08 +02:00
let server: String?
2023-10-23 14:58:58 +02:00
2023-10-24 11:31:08 +02:00
let authentication: String?
2023-10-23 14:58:58 +02:00
}
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
}
}
}