import Foundation // The person API's pairing, device and error shapes, from // docs/api/equipo-v1.openapi.json. The conversation is in Thin.swift. Field // names follow the wire (snake_case is converted by `EquipoJSON.decoder`). // Optional numbers mean unknown, never zero. public struct Person: Codable, Sendable, Equatable { public var address: String public var displayIdentity: String public var active: Bool public var role: String } public struct Me: Codable, Sendable, Equatable { public var person: Person public var teamId: String public var teamName: String public var revision: Int64 } public struct Page: Codable, Sendable { public var items: [Item] public var nextCursor: String public var revision: Int64 } public struct Ack: Codable, Sendable, Equatable { public var revision: Int64 } public struct DeviceChallenge: Codable, Sendable, Equatable { public var id: String public var deviceSecret: String public var userCode: String public var challenge: String public var verificationUri: String public var expiresAt: Date public var pollIntervalSeconds: Int } public struct DeviceCredential: Codable, Sendable, Equatable { public var id: String public var token: String public var audience: String public var instanceId: String public var expiresAt: Date } public struct Device: Codable, Sendable, Identifiable, Equatable { public var id: String public var label: String public var createdAt: Date public var expiresAt: Date public var revokedAt: Date? public var revision: Int64 public var lastUsedAt: Date? } /// The person API's error body. public struct APIErrorBody: Codable, Sendable, Equatable { public var code: String public var copyKey: String public var requestId: String public var currentRevision: Int64? } public enum EquipoJSON { /// Go's time.Time encodes RFC 3339 with optional fractional seconds. public static var decoder: JSONDecoder { let d = JSONDecoder() d.keyDecodingStrategy = .convertFromSnakeCase d.dateDecodingStrategy = .custom { decoder in let c = try decoder.singleValueContainer() let raw = try c.decode(String.self) if let date = parseDate(raw) { return date } throw DecodingError.dataCorruptedError(in: c, debugDescription: "not an RFC 3339 time: \(raw)") } return d } public static var encoder: JSONEncoder { let e = JSONEncoder() e.keyEncodingStrategy = .convertToSnakeCase e.outputFormatting = [.sortedKeys, .withoutEscapingSlashes] return e } static func parseDate(_ raw: String) -> Date? { let fractional = Date.ISO8601FormatStyle(includingFractionalSeconds: true) let whole = Date.ISO8601FormatStyle() return (try? fractional.parse(raw)) ?? (try? whole.parse(raw)) } }