|
| 1 | +import Foundation |
| 2 | + |
| 3 | +private enum IdentifierCoding { |
| 4 | + static func decodeUUID(from decoder: any Decoder, typeName: String) throws -> UUID { |
| 5 | + let container = try decoder.singleValueContainer() |
| 6 | + let string = try container.decode(String.self) |
| 7 | + guard let uuid = UUID(uuidString: string) else { |
| 8 | + throw DecodingError.dataCorruptedError( |
| 9 | + in: container, |
| 10 | + debugDescription: "Invalid \(typeName) UUID string: \(string)" |
| 11 | + ) |
| 12 | + } |
| 13 | + return uuid |
| 14 | + } |
| 15 | + |
| 16 | + static func encodeUUID(_ uuid: UUID, to encoder: any Encoder) throws { |
| 17 | + var container = encoder.singleValueContainer() |
| 18 | + try container.encode(uuid.uuidString) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +/// Uniquely identifies a streamed event. |
| 23 | +public struct EventID: Sendable, Hashable, Codable, CustomStringConvertible { |
| 24 | + public let rawValue: UUID |
| 25 | + |
| 26 | + public init() { |
| 27 | + rawValue = UUID() |
| 28 | + } |
| 29 | + |
| 30 | + public init(rawValue: UUID) { |
| 31 | + self.rawValue = rawValue |
| 32 | + } |
| 33 | + |
| 34 | + public var description: String { |
| 35 | + rawValue.uuidString |
| 36 | + } |
| 37 | + |
| 38 | + public init(from decoder: any Decoder) throws { |
| 39 | + rawValue = try IdentifierCoding.decodeUUID(from: decoder, typeName: "EventID") |
| 40 | + } |
| 41 | + |
| 42 | + public func encode(to encoder: any Encoder) throws { |
| 43 | + try IdentifierCoding.encodeUUID(rawValue, to: encoder) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/// Uniquely identifies an agent session. |
| 48 | +public struct SessionID: Sendable, Hashable, Codable, CustomStringConvertible { |
| 49 | + public let rawValue: UUID |
| 50 | + |
| 51 | + public init() { |
| 52 | + rawValue = UUID() |
| 53 | + } |
| 54 | + |
| 55 | + public init(rawValue: UUID) { |
| 56 | + self.rawValue = rawValue |
| 57 | + } |
| 58 | + |
| 59 | + public var description: String { |
| 60 | + rawValue.uuidString |
| 61 | + } |
| 62 | + |
| 63 | + public init(from decoder: any Decoder) throws { |
| 64 | + rawValue = try IdentifierCoding.decodeUUID(from: decoder, typeName: "SessionID") |
| 65 | + } |
| 66 | + |
| 67 | + public func encode(to encoder: any Encoder) throws { |
| 68 | + try IdentifierCoding.encodeUUID(rawValue, to: encoder) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/// Uniquely identifies an agent run within a session. |
| 73 | +public struct RunID: Sendable, Hashable, Codable, CustomStringConvertible { |
| 74 | + public let rawValue: UUID |
| 75 | + |
| 76 | + public init() { |
| 77 | + rawValue = UUID() |
| 78 | + } |
| 79 | + |
| 80 | + public init(rawValue: UUID) { |
| 81 | + self.rawValue = rawValue |
| 82 | + } |
| 83 | + |
| 84 | + public var description: String { |
| 85 | + rawValue.uuidString |
| 86 | + } |
| 87 | + |
| 88 | + public init(from decoder: any Decoder) throws { |
| 89 | + rawValue = try IdentifierCoding.decodeUUID(from: decoder, typeName: "RunID") |
| 90 | + } |
| 91 | + |
| 92 | + public func encode(to encoder: any Encoder) throws { |
| 93 | + try IdentifierCoding.encodeUUID(rawValue, to: encoder) |
| 94 | + } |
| 95 | +} |
0 commit comments