// Generated by alef. Do not edit by hand.
// swift-format-ignore-file
// This file contains generated FFI glue for trait bridge registration.

import Foundation
import RustBridge

/// Protocol for outbound `{{ trait_name }}` implementations.
/// Conform your Swift class or struct to this protocol to implement
/// a Rust trait from the host side.
public protocol {{ protocol }}: SwiftPluginBridge {
{{ protocol_methods }}}

public extension {{ protocol }} {
{{ default_methods }}}

/// Internal adapter wrapping a `{{ protocol }}` conformer.
/// Wraps a user-supplied bridge and forwards calls through marshalling entry points.
final class {{ adapter_class }} {
    private let bridge: any {{ protocol }}

    init(bridge: any {{ protocol }}) {
        self.bridge = bridge
    }

    // MARK: - SwiftPluginBridge lifecycle

    var name: String {
        return bridge.name
    }

    func version() -> String {
        return bridge.version()
    }

    func initialize() throws {
        try bridge.initialize()
    }

    func shutdown() throws {
        try bridge.shutdown()
    }

    // MARK: - FFI marshalling entry points

{{ adapter_methods }}
}

// MARK: - Marshalling helpers

private struct Empty: Codable {}

private func marshal_ok_result<T: Encodable>(_ value: T) -> String {
    let encoder = JSONEncoder()
    if let data = try? encoder.encode(value),
       let jsonString = String(data: data, encoding: .utf8) {
        return "{\"ok\": \(jsonString)}"
    }
    return "{\"ok\": null}"
}

private func marshal_encode_excluded<T: Encodable>(_ value: T) throws -> Data {
    let encoder = JSONEncoder()
    return try encoder.encode(value)
}

private func marshal_error_result(_ error: any Error) -> String {
    let errorString = String(describing: error)
    let encoder = JSONEncoder()
    if let data = try? encoder.encode(errorString),
       let jsonString = String(data: data, encoding: .utf8) {
        return "{\"err\": \(jsonString)}"
    }
    return "{\"err\": \"unknown error\"}"
}
