feat: use TOML for editable color configuration
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import AppKit
|
||||
import TOMLDecoder
|
||||
|
||||
@MainActor
|
||||
struct ColorScheme {
|
||||
@@ -31,7 +32,7 @@ struct ColorScheme {
|
||||
}
|
||||
|
||||
static func load(from directory: URL) throws -> ColorScheme {
|
||||
let configURL = directory.appendingPathComponent("config.json")
|
||||
let configURL = directory.appendingPathComponent("config.toml")
|
||||
guard let config = try readObject(at: configURL, missingAllowed: true) else { return sunset }
|
||||
try validateKeys(config, allowed: ["theme", "colors"], at: configURL, prefix: "")
|
||||
let theme: String
|
||||
@@ -69,7 +70,7 @@ struct ColorScheme {
|
||||
private mutating func applyColors(from object: [String: Any], at url: URL) throws {
|
||||
guard let value = object["colors"] else { return }
|
||||
guard let colors = value as? [String: Any] else {
|
||||
throw ColorSchemeError(path: url.path, reason: "colors must be an object.")
|
||||
throw ColorSchemeError(path: url.path, reason: "colors must contain named color values.")
|
||||
}
|
||||
try Self.validateKeys(colors, allowed: Set(Self.colorFields.keys), at: url, prefix: "colors.")
|
||||
for (name, value) in colors {
|
||||
@@ -107,10 +108,13 @@ struct ColorScheme {
|
||||
throw ColorSchemeError(path: url.path, reason: error.localizedDescription)
|
||||
}
|
||||
let value: Any
|
||||
let isTOML = url.pathExtension == "toml"
|
||||
do {
|
||||
if isTOML { return try Dictionary(TOMLTable(source: data)) }
|
||||
value = try JSONSerialization.jsonObject(with: data, options: [.fragmentsAllowed])
|
||||
} catch {
|
||||
throw ColorSchemeError(path: url.path, reason: "Invalid JSON: \(error.localizedDescription)")
|
||||
let detail = isTOML ? String(describing: error) : error.localizedDescription
|
||||
throw ColorSchemeError(path: url.path, reason: "Invalid \(isTOML ? "TOML" : "JSON"): \(detail)")
|
||||
}
|
||||
guard let object = value as? [String: Any] else {
|
||||
throw ColorSchemeError(path: url.path, reason: "Expected a JSON object.")
|
||||
|
||||
Reference in New Issue
Block a user