~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/atirc-ios-client

0
1
main
54 lines · 1.3 KB Raw History
import Foundation
import AtircGo

/// Swift-facing wrapper around the embedded Go library.
enum GoBridge {
    static var runtimeVersion: String { AtircgoVersion() }

    static func echo(_ name: String) -> String {
        AtircgoEcho(name)
    }

    static func sha256Hex(_ data: Data) -> String {
        AtircgoSHA256Hex(data)
    }

    static func parseCommand(_ line: String) throws -> String {
        var error: NSError?
        let command = AtircgoParseCommand(line, &error)
        if let error {
            throw error
        }
        return command
    }

    static func makeCounter() -> AtircgoCounter? {
        AtircgoNewCounter()
    }

    static func recordAfter(_ message: String) {
        AtircgoRecordAfter(message)
    }

    static func lastRecorded() -> String {
        AtircgoLastRecorded()
    }

    static func emitLater(_ callback: AtircgoCallbackProtocol, message: String) {
        AtircgoEmitLater(callback, message)
    }
}

/// Bridges Go callback invocations to a Swift closure.
nonisolated final class GoCallback: NSObject, AtircgoCallbackProtocol, @unchecked Sendable {
    private let handler: @Sendable (String?) -> Void

    init(handler: @escaping @Sendable (String?) -> Void) {
        self.handler = handler
        super.init()
    }

    func onEvent(_ message: String?) {
        handler(message)
    }
}