diff --git a/Sources/ElixxirDAppsSDK/Callbacks/UdLookupCallback.swift b/Sources/ElixxirDAppsSDK/Callbacks/UdLookupCallback.swift new file mode 100644 index 0000000000000000000000000000000000000000..772933fce417dc45e5beec96d514430e152c12b6 --- /dev/null +++ b/Sources/ElixxirDAppsSDK/Callbacks/UdLookupCallback.swift @@ -0,0 +1,40 @@ +import Bindings +import XCTestDynamicOverlay + +public struct UdLookupCallback { + public init(handle: @escaping (Result<Data, NSError>) -> Void) { + self.handle = handle + } + + public var handle: (Result<Data, NSError>) -> Void +} + +extension UdLookupCallback { + public static let unimplemented = UdLookupCallback( + handle: XCTUnimplemented("\(Self.self)") + ) +} + +extension UdLookupCallback { + func makeBindingsUdLookupCallback() -> BindingsUdLookupCallbackProtocol { + class CallbackObject: NSObject, BindingsUdLookupCallbackProtocol { + init(_ callback: UdLookupCallback) { + self.callback = callback + } + + let callback: UdLookupCallback + + func callback(_ contactBytes: Data?, err: Error?) { + if let error = err { + callback.handle(.failure(error as NSError)) + } else if let data = contactBytes { + callback.handle(.success(data)) + } else { + fatalError("BindingsUdLookupCallback received `nil` data and `nil` error") + } + } + } + + return CallbackObject(self) + } +}