30 lines
656 B
Swift
30 lines
656 B
Swift
import Foundation
|
|
|
|
protocol Metadata: Codable {
|
|
|
|
static var fileName: String { get }
|
|
|
|
static var initial: Self { get }
|
|
}
|
|
|
|
extension Metadata {
|
|
|
|
static func url(in folder: URL) -> URL {
|
|
folder.appendingPathComponent(fileName)
|
|
}
|
|
|
|
static func exists(in folder: URL) -> Bool {
|
|
url(in: folder).exists
|
|
}
|
|
|
|
init?(in folder: URL) throws {
|
|
let metadataUrl = Self.url(in: folder)
|
|
guard metadataUrl.exists else {
|
|
try Self.initial.writeJSON(to: metadataUrl)
|
|
print("Created metadata in \(folder)")
|
|
return nil
|
|
}
|
|
try self.init(decodeFrom: metadataUrl)
|
|
}
|
|
}
|