diff --git a/Sources/phbar/Models/Config/Extensions/PHConfig+Loading.swift b/Sources/phbar/Models/Config/Extensions/PHConfig+Loading.swift new file mode 100644 index 0000000..b61eaab --- /dev/null +++ b/Sources/phbar/Models/Config/Extensions/PHConfig+Loading.swift @@ -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) + } + } +} diff --git a/Sources/phbar/Models/Config/PHConfig.swift b/Sources/phbar/Models/Config/PHConfig.swift index 73d5715..ac24f2f 100644 --- a/Sources/phbar/Models/Config/PHConfig.swift +++ b/Sources/phbar/Models/Config/PHConfig.swift @@ -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) - } - } -} diff --git a/Sources/phbar/Models/Config/PHConfigText.swift b/Sources/phbar/Models/Config/PHConfigText.swift index c7276dd..9a0669b 100644 --- a/Sources/phbar/Models/Config/PHConfigText.swift +++ b/Sources/phbar/Models/Config/PHConfigText.swift @@ -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 }