import Foundation /// A loosely typed JSON value, used for lexicon `unknown` fields and for /// building record bodies we send to a PDS. enum JSONValue: Codable, Hashable, Sendable { case string(String) case number(Double) case bool(Bool) case object([String: JSONValue]) case array([JSONValue]) case null init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() if container.decodeNil() { self = .null } else if let value = try? container.decode(Bool.self) { self = .bool(value) } else if let value = try? container.decode(Double.self) { self = .number(value) } else if let value = try? container.decode(String.self) { self = .string(value) } else if let value = try? container.decode([JSONValue].self) { self = .array(value) } else { self = .object(try container.decode([String: JSONValue].self)) } } func encode(to encoder: Encoder) throws { var container = encoder.singleValueContainer() switch self { case .string(let value): try container.encode(value) case .number(let value): try container.encode(value) case .bool(let value): try container.encode(value) case .object(let value): try container.encode(value) case .array(let value): try container.encode(value) case .null: try container.encodeNil() } } subscript(key: String) -> JSONValue? { if case .object(let dict) = self { return dict[key] } return nil } var stringValue: String? { if case .string(let value) = self { return value } return nil } /// Re-decodes this value as a concrete type. func decode(_ type: T.Type) throws -> T { let data = try JSONEncoder().encode(self) return try JSONDecoder().decode(type, from: data) } } /// Text that may be a plain string (legacy records) or an /// `org.tangled.markup.markdown` object (`{ "text": "..." }`). struct FlexText: Codable, Hashable, Sendable { var text: String init(_ text: String) { self.text = text } private enum CodingKeys: String, CodingKey { case text } init(from decoder: Decoder) throws { if let single = try? decoder.singleValueContainer(), let value = try? single.decode(String.self) { text = value return } let container = try decoder.container(keyedBy: CodingKeys.self) text = try container.decodeIfPresent(String.self, forKey: .text) ?? "" } func encode(to encoder: Encoder) throws { var container = encoder.singleValueContainer() try container.encode(text) } } enum ATDate { private static let fractional: ISO8601DateFormatter = { let formatter = ISO8601DateFormatter() formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds] return formatter }() private static let plain: ISO8601DateFormatter = { let formatter = ISO8601DateFormatter() formatter.formatOptions = [.withInternetDateTime] return formatter }() static func parse(_ string: String?) -> Date? { guard let string else { return nil } if let date = fractional.date(from: string) ?? plain.date(from: string) { return date } // Some records carry more than millisecond precision, which // ISO8601DateFormatter rejects; trim the fraction and try again. if let dot = string.firstIndex(of: ".") { let rest = string[dot...] if let zoneStart = rest.firstIndex(where: { $0 == "Z" || $0 == "+" || $0 == "-" }) { let trimmed = String(string[.. String { fractional.string(from: Date()) } }