cleanup Config

This commit is contained in:
2026-07-12 00:37:51 +02:00
parent d8d5e38d33
commit 289cc6eb61
3 changed files with 57 additions and 60 deletions
@@ -0,0 +1,56 @@
import Foundation
import TOML
extension PHConfig {
/// Load configuration from the default path (~/.config/phbar/config.toml).
/// If the file doesn't exist or fails to parse, defaults are used (non-fatal).
static func load() throws -> PHConfig {
let url = FileManager.default.homeDirectoryForCurrentUser.appending(
path: ".config/phbar/config.toml"
)
if FileManager.default.fileExists(atPath: url.relativePath) {
return try load(from: url)
} else {
return try loadFromBundle()
}
}
/// Load the bundled configuration.
/// If the file doesn't exist or fails to parse, the execution is interrupted.
static private func loadFromBundle() throws -> PHConfig {
guard
let defaultConfig = Bundle.module.url(
forResource: "config",
withExtension: "toml"
)?.resolvingSymlinksInPath()
else {
throw phbar.Error("Failed to load config file")
}
return try load(from: defaultConfig)
}
/// Decode configuration from a file at URL.
/// If the file doesn't exist or fails to parse, the execution is interrupted.
static private func load(from url: URL) throws -> PHConfig {
guard let data = try? Data(contentsOf: url),
let contents = String(data: data, encoding: .utf8)
else {
throw phbar.Error("Failed to load config file")
}
return try load(from: contents)
}
/// Decode configuration from a TOML string.
/// If the content fails to parse, the execution is interrupted.
static private func load(from contents: String) throws -> PHConfig {
do {
let decoder = TOMLDecoder()
let configFile = try decoder.decode(PHConfig.self, from: contents)
return configFile
} catch {
throw phbar.Error("Failed to parse config file", underlyingError: error)
}
}
}
@@ -1,62 +1,3 @@
import Foundation
import TOML
struct PHConfig: Decodable {
let text: PHConfigText
}
// Loading
extension PHConfig {
/// Load configuration from the default path (~/.config/phbar/config.toml).
/// If the file doesn't exist or fails to parse, defaults are used (non-fatal).
static func load() throws -> PHConfig {
let url = FileManager.default.homeDirectoryForCurrentUser.appending(
path: ".config/phbar/config.toml"
)
if FileManager.default.fileExists(atPath: url.relativePath) {
return try load(from: url)
} else {
return try loadFromBundle()
}
}
/// Load the bundled configuration.
/// If the file doesn't exist or fails to parse, the execution is interrupted.
static private func loadFromBundle() throws -> PHConfig {
guard
let defaultConfig = Bundle.module.url(
forResource: "config",
withExtension: "toml"
)?.resolvingSymlinksInPath()
else {
throw phbar.Error("Failed to load config file")
}
return try load(from: defaultConfig)
}
/// Decode configuration from a file at URL.
/// If the file doesn't exist or fails to parse, the execution is interrupted.
static private func load(from url: URL) throws -> PHConfig {
guard let data = try? Data(contentsOf: url),
let contents = String(data: data, encoding: .utf8)
else {
throw phbar.Error("Failed to load config file")
}
return try load(from: contents)
}
/// Decode configuration from a TOML string.
/// If the content fails to parse, the execution is interrupted.
static private func load(from contents: String) throws -> PHConfig {
do {
let decoder = TOMLDecoder()
let configFile = try decoder.decode(PHConfig.self, from: contents)
return configFile
} catch {
throw phbar.Error("Failed to parse config file", underlyingError: error)
}
}
}
@@ -8,7 +8,7 @@ struct PHConfigText: Decodable {
let style: PHConfigTextStyle
let offset: Double?
enum CodingKeys: String, CodingKey {
private enum CodingKeys: String, CodingKey {
case fontFamily = "font"
case size, weight, style, offset
}