Skip to content

Commit 86b5218

Browse files
committed
Minimize usage of TOMLKit
1 parent 058ec8a commit 86b5218

File tree

12 files changed

+191
-112
lines changed

12 files changed

+191
-112
lines changed

Sources/AppBundle/command/parseCommand.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Common
2-
import TOMLKit
32

43
func parseCommand(_ raw: String) -> ParsedCmd<any Command> {
54
if raw.starts(with: "exec-and-forget") {
@@ -15,14 +14,14 @@ func parseCommand(_ args: [String]) -> ParsedCmd<any Command> {
1514
parseCmdArgs(args.slice).map { $0.toCommand() }
1615
}
1716

18-
func expectedActualTypeError(expected: TOMLType, actual: TOMLType) -> String {
17+
func expectedActualTypeError(expected: TomlType, actual: TomlType) -> String {
1918
"Expected type is '\(expected)'. But actual type is '\(actual)'"
2019
}
2120

22-
func expectedActualTypeError(expected: [TOMLType], actual: TOMLType) -> String {
21+
func expectedActualTypeError(expected: [TomlType], actual: TomlType) -> String {
2322
if let single = expected.singleOrNil() {
2423
return expectedActualTypeError(expected: single, actual: actual)
2524
} else {
26-
return "Expected types are \(expected.map { "'\($0.description)'" }.joined(separator: " or ")). But actual type is '\(actual)'"
25+
return "Expected types are \(expected.map { "'\($0)'" }.joined(separator: " or ")). But actual type is '\(actual)'"
2726
}
2827
}

Sources/AppBundle/config/DynamicConfigValue.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Common
2-
import TOMLKit
32

43
struct PerMonitorValue<Value: Equatable>: Equatable {
54
let description: MonitorDescription
@@ -33,15 +32,15 @@ extension DynamicConfigValue {
3332
}
3433

3534
func parseDynamicValue<T>(
36-
_ raw: TOMLValueConvertible,
35+
_ raw: Json,
3736
_ valueType: T.Type,
3837
_ fallback: T,
3938
_ backtrace: ConfigBacktrace,
4039
_ errors: inout [ConfigParseError],
4140
) -> DynamicConfigValue<T> {
4241
if let simpleValue = parseSimpleType(raw) as T? {
4342
return .constant(simpleValue)
44-
} else if let array = raw.array {
43+
} else if let array = raw.asArrayOrNil {
4544
if array.isEmpty {
4645
errors.append(.semantic(backtrace, "The array must not be empty"))
4746
return .constant(fallback)
@@ -57,17 +56,17 @@ func parseDynamicValue<T>(
5756
return .constant(fallback)
5857
}
5958

60-
let rules: [PerMonitorValue<T>] = parsePerMonitorValues(TOMLArray(array.dropLast()), backtrace, &errors)
59+
let rules: [PerMonitorValue<T>] = parsePerMonitorValues(array.dropLast(), backtrace, &errors)
6160

6261
return .perMonitor(rules, default: defaultValue)
6362
} else {
64-
errors.append(.semantic(backtrace, "Unsupported type: \(raw.type), expected: \(valueType) or array"))
63+
errors.append(.semantic(backtrace, "Unsupported type: \(raw.tomlType), expected: \(valueType) or array"))
6564
return .constant(fallback)
6665
}
6766
}
6867

69-
func parsePerMonitorValues<T>(_ array: TOMLArray, _ backtrace: ConfigBacktrace, _ errors: inout [ConfigParseError]) -> [PerMonitorValue<T>] {
70-
array.enumerated().compactMap { (index: Int, raw: TOMLValueConvertible) -> PerMonitorValue<T>? in
68+
func parsePerMonitorValues<T>(_ array: Json.JsonArray, _ backtrace: ConfigBacktrace, _ errors: inout [ConfigParseError]) -> [PerMonitorValue<T>] {
69+
array.enumerated().compactMap { (index: Int, raw: Json) -> PerMonitorValue<T>? in
7170
var backtrace = backtrace + .index(index)
7271

7372
guard let (key, value) = raw.unwrapTableWithSingleKey(expectedKey: "monitor", &backtrace)
@@ -77,12 +76,12 @@ func parsePerMonitorValues<T>(_ array: TOMLArray, _ backtrace: ConfigBacktrace,
7776
return nil
7877
}
7978

80-
let monitorDescriptionResult = parseMonitorDescription(key, backtrace)
79+
let monitorDescriptionResult = parseMonitorDescription(.string(key), backtrace)
8180

8281
guard let monitorDescription = monitorDescriptionResult.getOrNil(appendErrorTo: &errors) else { return nil }
8382

8483
guard let value = parseSimpleType(value) as T? else {
85-
errors.append(.semantic(backtrace, "Expected type is '\(T.self)'. But actual type is '\(value.type)'"))
84+
errors.append(.semantic(backtrace, "Expected type is '\(T.self)'. But actual type is '\(value.tomlType)'"))
8685
return nil
8786
}
8887

Sources/AppBundle/config/HotkeyBinding.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import AppKit
22
import Common
33
import Foundation
44
import HotKey
5-
import TOMLKit
65

76
@MainActor private var hotkeys: [String: HotKey] = [:]
87

@@ -90,13 +89,13 @@ struct HotkeyBinding: Equatable, Sendable {
9089
}
9190
}
9291

93-
func parseBindings(_ raw: TOMLValueConvertible, _ backtrace: ConfigBacktrace, _ errors: inout [ConfigParseError], _ mapping: [String: Key]) -> [String: HotkeyBinding] {
94-
guard let rawTable = raw.table else {
95-
errors += [expectedActualTypeError(expected: .table, actual: raw.type, backtrace)]
92+
func parseBindings(_ raw: Json, _ backtrace: ConfigBacktrace, _ errors: inout [ConfigParseError], _ mapping: [String: Key]) -> [String: HotkeyBinding] {
93+
guard let rawTable = raw.asDictOrNil else {
94+
errors += [expectedActualTypeError(expected: .table, actual: raw.tomlType, backtrace)]
9695
return [:]
9796
}
9897
var result: [String: HotkeyBinding] = [:]
99-
for (binding, rawCommand): (String, TOMLValueConvertible) in rawTable {
98+
for (binding, rawCommand): (String, Json) in rawTable {
10099
let backtrace = backtrace + .key(binding)
101100
let binding = parseBinding(binding, backtrace, mapping)
102101
.flatMap { modifiers, key -> ParsedConfig<HotkeyBinding> in

Sources/AppBundle/config/Mode.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import Common
22
import HotKey
3-
import TOMLKit
43

54
struct Mode: ConvenienceCopyable, Equatable, Sendable {
65
var bindings: [String: HotkeyBinding]
76

87
static let zero = Mode(bindings: [:])
98
}
109

11-
func parseModes(_ raw: TOMLValueConvertible, _ backtrace: ConfigBacktrace, _ errors: inout [ConfigParseError], _ mapping: [String: Key]) -> [String: Mode] {
12-
guard let rawTable = raw.table else {
13-
errors += [expectedActualTypeError(expected: .table, actual: raw.type, backtrace)]
10+
func parseModes(_ raw: Json, _ backtrace: ConfigBacktrace, _ errors: inout [ConfigParseError], _ mapping: [String: Key]) -> [String: Mode] {
11+
guard let rawTable = raw.asDictOrNil else {
12+
errors += [expectedActualTypeError(expected: .table, actual: raw.tomlType, backtrace)]
1413
return [:]
1514
}
1615
var result: [String: Mode] = [:]
@@ -23,9 +22,9 @@ func parseModes(_ raw: TOMLValueConvertible, _ backtrace: ConfigBacktrace, _ err
2322
return result
2423
}
2524

26-
func parseMode(_ raw: TOMLValueConvertible, _ backtrace: ConfigBacktrace, _ errors: inout [ConfigParseError], _ mapping: [String: Key]) -> Mode {
27-
guard let rawTable: TOMLTable = raw.table else {
28-
errors += [expectedActualTypeError(expected: .table, actual: raw.type, backtrace)]
25+
func parseMode(_ raw: Json, _ backtrace: ConfigBacktrace, _ errors: inout [ConfigParseError], _ mapping: [String: Key]) -> Mode {
26+
guard let rawTable: Json.JsonDict = raw.asDictOrNil else {
27+
errors += [expectedActualTypeError(expected: .table, actual: raw.tomlType, backtrace)]
2928
return .zero
3029
}
3130

0 commit comments

Comments
 (0)