Files
swift-hpa/Sources/ConfigurationClient/File.swift

54 lines
1.0 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 {
.toml("~/.config/\(HPAKey.configDirName)/\(HPAKey.defaultFileName)")
}
}
@_spi(Internal)
public extension URL {
var cleanFilePath: String {
absoluteString.replacing("file://", with: "")
}
}