feat: Breaking out more dependencies.
This commit is contained in:
53
Sources/ConfigurationClient/File.swift
Normal file
53
Sources/ConfigurationClient/File.swift
Normal file
@@ -0,0 +1,53 @@
|
||||
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: "")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user