feature/xcodegen-modernize-runner
swift 137 lines 3.52 KB
Raw
1 import Foundation
2
3 /// The Apple platform represented by a ControlKit runner.
4 public enum XCRSControlKitPlatform: String, Codable, CaseIterable, Sendable {
5 case iOS
6 case iPadOS
7 case tvOS
8 case watchOS
9 case visionOS
10 case macOS
11 }
12
13 /// An operation supported by a ControlKit runner.
14 public enum XCRSControlKitCapability: String, Codable, CaseIterable, Sendable {
15 case accessibility
16 case screenshot
17 case appLifecycle
18 case touch
19 case textInput
20 case swipe
21 case orientation
22 case remoteButtons
23 case keyboard
24 case pointer
25 case spatialInteraction
26 case pairedDevice
27 }
28
29 /// A simulator or device controlled by a runner.
30 public struct XCRSControlKitTarget: Codable, Equatable, Sendable {
31 public let platform: XCRSControlKitPlatform
32 public let identifier: String
33 public let name: String?
34
35 public init(
36 platform: XCRSControlKitPlatform,
37 identifier: String,
38 name: String? = nil
39 ) {
40 self.platform = platform
41 self.identifier = identifier
42 self.name = name
43 }
44 }
45
46 /// The capabilities advertised by a runner.
47 public struct XCRSControlKitCapabilities: Codable, Equatable, Sendable {
48 public let platform: XCRSControlKitPlatform
49 public let values: Set<XCRSControlKitCapability>
50
51 public init(
52 platform: XCRSControlKitPlatform,
53 values: Set<XCRSControlKitCapability>
54 ) {
55 self.platform = platform
56 self.values = values
57 }
58
59 public func supports(_ capability: XCRSControlKitCapability) -> Bool {
60 values.contains(capability)
61 }
62
63 public static func standard(
64 for platform: XCRSControlKitPlatform
65 ) -> Self {
66 let values: Set<XCRSControlKitCapability>
67
68 switch platform {
69 case .iOS, .iPadOS:
70 values = [
71 .accessibility,
72 .screenshot,
73 .appLifecycle,
74 .touch,
75 .textInput,
76 .swipe,
77 .orientation
78 ]
79 case .tvOS:
80 values = [
81 .accessibility,
82 .screenshot,
83 .appLifecycle,
84 .remoteButtons
85 ]
86 case .watchOS:
87 values = [
88 .accessibility,
89 .screenshot,
90 .appLifecycle,
91 .pairedDevice
92 ]
93 case .visionOS:
94 values = [
95 .accessibility,
96 .screenshot,
97 .appLifecycle,
98 .spatialInteraction
99 ]
100 case .macOS:
101 values = [
102 .accessibility,
103 .screenshot,
104 .appLifecycle,
105 .keyboard,
106 .pointer
107 ]
108 }
109
110 return Self(platform: platform, values: values)
111 }
112 }
113
114 /// A transport-neutral command sent to a runner.
115 public struct XCRSControlKitRequest: Codable, Equatable, Sendable {
116 public let id: String
117 public let method: String
118 public let parameters: [String: String]
119
120 public init(
121 id: String = UUID().uuidString,
122 method: String,
123 parameters: [String: String] = [:]
124 ) {
125 self.id = id
126 self.method = method
127 self.parameters = parameters
128 }
129 }
130
131 /// Errors shared by package consumers and platform runners.
132 public enum XCRSControlKitError: Error, Equatable, Sendable {
133 case unsupportedPlatform(XCRSControlKitPlatform)
134 case unsupportedCapability(XCRSControlKitCapability)
135 case invalidRequest(String)
136 case transport(String)
137 }