Skip to content

Commit 8d5dc7b

Browse files
committed
Json, cleanup: get rid of different explicit numeric types, leave only one (Int64)
64 bits will be enough for everyone ©
1 parent 1b6ce66 commit 8d5dc7b

File tree

6 files changed

+19
-20
lines changed

6 files changed

+19
-20
lines changed

Sources/AppBundle/command/impl/DebugWindowsCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private func dumpWindowDebugInfo(_ window: Window) async throws -> String {
8080
let windowLevel = getWindowLevel(for: window.windowId)
8181
let windowLevelJson = windowLevel?.toJson() ?? .null
8282
result["Aero.windowLevel"] = windowLevelJson
83-
result["Aero.axWindowId"] = .uint32(window.windowId)
83+
result["Aero.axWindowId"] = .int(window.windowId)
8484
result["Aero.workspace"] = .stringOrNull(window.nodeWorkspace?.name)
8585
result["Aero.treeNodeParent"] = .string(String(describing: window.parent))
8686
result["Aero.macOS.version"] = .string(ProcessInfo().operatingSystemVersionString) // because built-in apps might behave differently depending on the OS version

Sources/AppBundle/model/Json.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ enum Json: Encodable, Equatable {
99
// scalar
1010
case null
1111
case string(String)
12-
case int(Int)
13-
case uint32(UInt32)
12+
case int(Int64)
1413
case bool(Bool)
1514

1615
typealias JsonDict = [String: Json]
@@ -22,7 +21,6 @@ enum Json: Encodable, Equatable {
2221
case .dict(let value): try value.encode(to: encoder)
2322
case .string(let value): try value.encode(to: encoder)
2423
case .int(let value): try value.encode(to: encoder)
25-
case .uint32(let value): try value.encode(to: encoder)
2624
case .bool(let value): try value.encode(to: encoder)
2725
case .null: try (nil as String?).encode(to: encoder)
2826
}
@@ -40,9 +38,10 @@ enum Json: Encodable, Equatable {
4038

4139
static func newScalarOrNil(_ value: Any?) -> Json? {
4240
switch value {
43-
case let value as Int: .int(value)
44-
case let value as Int64: Int(exactly: value).map(Json.int)
45-
case let value as UInt32: .uint32(value)
41+
case let value as Int64: .int(value)
42+
case let value as Int: .int(Int64(value))
43+
case let value as UInt32: .int(Int64(value))
44+
case let value as UInt: .int(Int64(value))
4645
case let value as Bool: .bool(value)
4746
case let value as String: .string(value)
4847
case nil, is NSNull: .null
@@ -52,6 +51,9 @@ enum Json: Encodable, Equatable {
5251

5352
static func stringOrNull(_ str: String?) -> Json { str.map(Json.string) ?? .null }
5453

54+
static func int(_ int: Int) -> Json { .int(Int64(exactly: int).orDie()) }
55+
static func int(_ int: UInt32) -> Json { .int(Int64(exactly: int).orDie()) }
56+
5557
var rawValue: Any? {
5658
switch self {
5759
case .null: nil
@@ -62,16 +64,19 @@ enum Json: Encodable, Equatable {
6264
case .bool(let x): x
6365
case .int(let x): x
6466
case .string(let x): x
65-
case .uint32(let x): x
6667
}
6768
}
6869

6970
var asDictOrDie: [String: Json] { asDictOrNil.orDie("\(self) is not a dict") }
7071

71-
var asIntOrNil: Int? {
72+
var asInt64OrNil: Int64? {
7273
if case .int(let value) = self { value } else { nil }
7374
}
7475

76+
var asIntOrNil: Int? {
77+
asInt64OrNil.flatMap { Int.init(exactly: $0) }
78+
}
79+
7580
var asStringOrNil: String? {
7681
if case .string(let value) = self { value } else { nil }
7782
}
@@ -95,7 +100,6 @@ enum Json: Encodable, Equatable {
95100
case .null: return .null
96101
case .string: return .string
97102
case .int: return .int
98-
case .uint32: return .uint32
99103
case .bool: return .bool
100104
}
101105
}
@@ -108,6 +112,5 @@ enum TomlType: String {
108112
case null
109113
case string
110114
case int
111-
case uint32
112115
case bool
113116
}

Sources/AppBundle/util/accessibility.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ private func castToAxUiElementMock(_ a: AnyObject) -> AxUiElementMock {
301301
let windowId = UInt32.init(String(str.prefix(upTo: commaIndex)).removePrefix("AXUIElement(AxWindowId="))
302302
if let windowId {
303303
return castToAxUiElementMock([
304-
"Aero.axWindowId": Json.uint32(windowId),
304+
"Aero.axWindowId": Json.int(windowId),
305305
kAXAeroSynthetic: Json.bool(true),
306306
] as AnyObject)
307307
}

Sources/AppBundle/util/dumpAxRecursive.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private func prettyValue(_ value: Any?, recursionDepth: Int) -> Json {
5050
return .int(value)
5151
}
5252
if let value = value as? UInt32 {
53-
return .uint32(value)
53+
return .int(value)
5454
}
5555
if let value = value as? Bool {
5656
return .bool(value)

Sources/AppBundle/windowLevelCache.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ enum MacOsWindowLevel: Sendable, Equatable {
4343
switch json {
4444
case .string("normalWindow"): .normalWindow
4545
case .string("alwaysOnTopWindow"): .alwaysOnTopWindow
46-
case .int(let int): .new(windowLevel: int)
46+
case .int(let int): .new(windowLevel: Int(exactly: int).orDie())
4747
default: nil
4848
}
4949
}

Sources/AppBundleTests/AxUiElementWindowTypeTest.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,8 @@ extension [String: Json]: AxUiElementMock {
5353
public func containingWindowId() -> CGWindowID? { _containingWindowId() }
5454

5555
private func _containingWindowId() -> CGWindowID {
56-
let windowId = self["Aero.axWindowId"]?.rawValue ?? dieT()
57-
if let windowId = windowId as? Int {
58-
return UInt32.init(windowId)
59-
} else {
60-
return windowId as? UInt32 ?? dieT()
61-
}
56+
let windowId = self["Aero.axWindowId"]?.asInt64OrNil ?? dieT()
57+
return UInt32.init(exactly: windowId).orDie()
6258
}
6359
}
6460

0 commit comments

Comments
 (0)