Skip to content

Commit face174

Browse files
committed
Refactor MachPortController to use typed throws
Set `swift-tools-version` to `6.0` Move errors into `MachPortController`
1 parent 768932d commit face174

5 files changed

Lines changed: 19 additions & 26 deletions

File tree

Package.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.6
1+
// swift-tools-version:6.0
22
import PackageDescription
33

44
let package = Package(
@@ -8,9 +8,7 @@ let package = Package(
88
.library(name: "MachPort", targets: ["MachPort"]),
99
],
1010
targets: [
11-
.target(
12-
name: "MachPort",
13-
dependencies: [])
11+
.target(name: "MachPort", dependencies: [])
1412
]
1513
)
1614

Sources/MachPort/CFRunLoopSource+Extensions.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import CoreFoundation
22

3-
public enum CFRunLoopSourceError: Error {
4-
case failedToCreateCFRunLoopSource
5-
}
6-
73
internal extension CFRunLoopSource {
8-
static func create(with machPort: CFMachPort) throws -> CFRunLoopSource {
4+
static func create(with machPort: CFMachPort) throws(MachPortError) -> CFRunLoopSource {
95
guard let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, machPort, 0) else {
10-
throw CFRunLoopSourceError.failedToCreateCFRunLoopSource
6+
throw .failedToCreateCFRunLoopSource
117
}
128
return runLoopSource
139
}

Sources/MachPort/CGEventSource+Extensions.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import CoreGraphics
22

3-
public enum CGEventSourceError: Error {
4-
case failedToCreateCGEventSource
5-
}
6-
73
internal extension CGEventSource {
8-
static func create(_ stateID: CGEventSourceStateID) throws -> CGEventSource {
4+
static func create(_ stateID: CGEventSourceStateID) throws(MachPortError) -> CGEventSource {
95
guard let eventSource = CGEventSource(stateID: stateID) else {
10-
throw CGEventSourceError.failedToCreateCGEventSource
6+
throw .failedToCreateCGEventSource
117
}
128
return eventSource
139
}

Sources/MachPort/MachPortError.swift

Lines changed: 0 additions & 5 deletions
This file was deleted.

Sources/MachPort/MachPortEventController.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import Carbon
22
import Cocoa
33
import os
44

5+
public enum MachPortError: Error {
6+
case failedToCreateCGEventSource
7+
case failedToCreateCFRunLoopSource
8+
case failedToCreateMachPort
9+
case failedToCreateKeyCode(Int)
10+
case failedToCreateEvent
11+
}
12+
513
public class MachPortEventPublisher {
614
@Published public internal(set) var flagsChanged: CGEventFlags?
715
@Published public internal(set) var event: MachPortEvent?
@@ -94,15 +102,15 @@ public final class MachPortEventController: MachPortEventPublisher, @unchecked S
94102

95103
public func createEvent(_ key: Int, type: CGEventType, flags: CGEventFlags,
96104
tapLocation: CGEventTapLocation = .cghidEventTap,
97-
configure: (CGEvent) -> Void = { _ in }) throws -> CGEvent {
105+
configure: (CGEvent) -> Void = { _ in }) throws(MachPortError) -> CGEvent {
98106
guard let cgKeyCode = CGKeyCode(exactly: key) else {
99-
throw MachPortError.failedToCreateKeyCode(key)
107+
throw .failedToCreateKeyCode(key)
100108
}
101109

102110
guard let cgEvent = CGEvent(keyboardEventSource: eventSource,
103111
virtualKey: cgKeyCode,
104112
keyDown: type == .keyDown) else {
105-
throw MachPortError.failedToCreateEvent
113+
throw .failedToCreateEvent
106114
}
107115

108116
cgEvent.setIntegerValueField(.eventSourceUserData, value: signature)
@@ -207,7 +215,7 @@ public final class MachPortEventController: MachPortEventPublisher, @unchecked S
207215
return newEvent.result
208216
}
209217

210-
private final func createMachPort(_ currentMode: CFRunLoopMode) throws -> CFMachPort {
218+
private final func createMachPort(_ currentMode: CFRunLoopMode) throws(MachPortError) -> CFMachPort {
211219
let userInfo = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
212220

213221
guard let machPort = CGEvent.tapCreate(
@@ -230,7 +238,7 @@ public final class MachPortEventController: MachPortEventPublisher, @unchecked S
230238
}
231239
return Unmanaged.passUnretained(event)
232240
}, userInfo: userInfo) else {
233-
throw MachPortError.failedToCreateMachPort
241+
throw .failedToCreateMachPort
234242
}
235243
return machPort
236244
}

0 commit comments

Comments
 (0)