60 lines
1.2 KiB
Swift
60 lines
1.2 KiB
Swift
import Foundation
|
|
|
|
/// Represents a file location and type on disk for a configuration file.
|
|
public enum File: Equatable, Sendable {
|
|
|
|
case json(URL)
|
|
case toml(URL)
|
|
|
|
public init?(_ url: URL) {
|
|
if url.cleanFilePath.hasSuffix("json") {
|
|
self = .json(url)
|
|
} else if url.cleanFilePath.hasSuffix("toml") {
|
|
self = .toml(url)
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
public init?(_ path: String) {
|
|
self.init(URL(filePath: path))
|
|
}
|
|
|
|
public static func json(_ path: String) -> Self {
|
|
.json(URL(filePath: path))
|
|
}
|
|
|
|
public static func toml(_ path: String) -> Self {
|
|
.toml(URL(filePath: path))
|
|
}
|
|
|
|
public var url: URL {
|
|
switch self {
|
|
case let .json(url): return url
|
|
case let .toml(url): return url
|
|
}
|
|
}
|
|
|
|
public var path: String {
|
|
url.cleanFilePath
|
|
}
|
|
|
|
public static var `default`: Self {
|
|
let fileUrl = FileManager.default
|
|
.homeDirectoryForCurrentUser
|
|
.appending(path: ".config")
|
|
.appending(path: HPAKey.configDirName)
|
|
.appending(path: HPAKey.defaultFileName)
|
|
|
|
return .toml(fileUrl)
|
|
}
|
|
}
|
|
|
|
@_spi(Internal)
|
|
public extension URL {
|
|
|
|
var cleanFilePath: String {
|
|
absoluteString.replacing("file://", with: "")
|
|
}
|
|
}
|