---
source: tests/backends_swift_snapshot_test.rs
expression: "&file.content"
---
// Generated by alef. Do not edit by hand.
// swift-format-ignore-file

import Foundation
import RustBridge
/// Callback handle type alias.
public typealias CallbackHandle = RustBridge.CallbackHandle

/// Options for conversion.
public struct RenderOptions: Codable, Sendable, Hashable {
    public let timeoutMs: UInt32
    public init(timeoutMs: UInt32) {
        self.timeoutMs = timeoutMs
    }
    private enum CodingKeys: String, CodingKey {
        case timeoutMs = "timeout_ms"
    }
    public init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.timeoutMs = try container.decodeIfPresent(UInt32.self, forKey: .timeoutMs) ?? 0
    }
}

// MARK: - Internal FFI conversions for RenderOptions
internal extension RenderOptions {
    init(_ rb: RustBridge.RenderOptionsRef) throws {
        self.timeoutMs = rb.timeoutMs()
    }

    func intoRust() throws -> RustBridge.RenderOptions {
        return RustBridge.RenderOptions(self.timeoutMs)
    }
}

public typealias RenderOptionsRef = RustBridge.RenderOptionsRef

public typealias RenderOptionsRefMut = RustBridge.RenderOptionsRefMut

/// Decision returned by a markup callback.
public enum FlowDecision: String, Codable, Sendable, Hashable {
    /// Accept the event.
    case accept = "Accept"
    /// Stop processing.
    case stop = "Stop"
}

// MARK: - From-JSON Helpers
// Public helpers that decode JSON into first-class Swift types.
// First-class struct types (Codable) use JSONDecoder directly.
// Opaque RustBridge types forward to RustBridge.

public func renderOptionsFromJson(_ json: String) throws -> RenderOptions {
    let data = json.data(using: .utf8) ?? Data()
    return try JSONDecoder().decode(RenderOptions.self, from: data)
}
public func flowDecisionFromJson(_ json: String) throws -> FlowDecision {
    let data = json.data(using: .utf8) ?? Data()
    return try JSONDecoder().decode(FlowDecision.self, from: data)
}
/// Swift protocol that Swift classes implement to provide visitor callbacks.
/// Conform to this protocol to intercept configured Rust trait bridge events.
public protocol MarkupCallbackProtocol: AnyObject {
    func visitNode(_ tag: String) -> FlowDecision
}

/// Default implementation: every method returns `.accept` so conforming
/// types only need to implement the callbacks they care about.
public extension MarkupCallbackProtocol {
    func visitNode(_ _tag: String) -> FlowDecision { return .accept }
}

/// Internal adapter: wraps a `MarkupCallbackProtocol` conformer as a `_SwiftMarkupCallbackBoxDelegate`.
/// Converts swift-bridge raw types (RustString, UInt, etc.) to user-friendly Swift
/// types before dispatching, then serialises configured return values to JSON.
private final class _MarkupCallbackProtocolAdapter: _SwiftMarkupCallbackBoxDelegate {
    private let inner: any MarkupCallbackProtocol
    init(_ inner: any MarkupCallbackProtocol) { self.inner = inner }
    func visitNode(_ tag: RustString) -> String {
        return flow_decision_toJson(inner.visitNode(tag.toString()))
    }
}

/// Serialise a `FlowDecision` to a JSON string matching Rust serde defaults.
private func flow_decision_toJson(_ result: FlowDecision) -> String {
    switch result {
    case .accept: return "\"Accept\""
    case .stop: return "\"Stop\""
    }
}

/// Escape a Swift String for embedding in a JSON string literal.
private func jsonEscapeStr(_ s: String) -> String {
    s.replacingOccurrences(of: "\\", with: "\\\\")
     .replacingOccurrences(of: "\"", with: "\\\"")
     .replacingOccurrences(of: "\n", with: "\\n")
     .replacingOccurrences(of: "\r", with: "\\r")
     .replacingOccurrences(of: "\t", with: "\\t")
}

/// Wrap a `MarkupCallbackProtocol` conformer in an opaque `CallbackHandle` handle
/// that can be passed to `renderOptionsFromJsonWithCallback(...)` on the Rust side.
public func makeMarkupCallbackHandle(_ visitor: any MarkupCallbackProtocol) -> CallbackHandle {
    return RustBridge.makeMarkupCallbackHandle(SwiftMarkupCallbackBox(_MarkupCallbackProtocolAdapter(visitor)))
}

/// Decode `RenderOptions` JSON and attach a `CallbackHandle` visitor handle.
/// Forwards to the swift-bridge shim emitted in the `RustBridge` module —
/// re-exposed here so callers do not need `import RustBridge`.
public func renderOptionsFromJsonWithCallback(_ json: String, _ callback: CallbackHandle?) throws -> RenderOptions {
    return try RustBridge.renderOptionsFromJsonWithCallback(json, callback)
}

// swift-bridge opaque type used across Task.detached boundaries — Rust type is Send + Sync.
extension RustBridge.CallbackHandle: @unchecked Sendable {}
// swift-bridge opaque type used across Task.detached boundaries — Rust type is Send + Sync.
extension RustBridge.RenderOptions: @unchecked Sendable {}
