---
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

/// A text chunk.
public struct Chunk: Codable, Sendable, Hashable {
    public let text: String
    public init(text: String) {
        self.text = text
    }
    private enum CodingKeys: String, CodingKey {
        case text = "text"
    }
    public init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.text = try container.decodeIfPresent(String.self, forKey: .text) ?? ""
    }
}

// MARK: - Internal FFI conversions for Chunk
internal extension Chunk {
    init(_ rb: RustBridge.ChunkRef) throws {
        self.text = rb.text().toString()
    }
    func intoRust() throws -> RustBridge.Chunk {
        return RustBridge.Chunk(RustString(self.text))
    }
}

/// Streaming event enum.
public enum StreamEvent: Codable, Sendable, Hashable {
    /// A data event carrying an optional chunk.
    case data(chunk: Chunk?)
    /// An error event with an optional message.
    case error(message: String?)
    /// Stream completed with item count.
    case done(count: UInt32)
}
extension StreamEvent {
    func intoRust() throws -> RustBridge.StreamEvent {
        let data = try JSONEncoder().encode(self)
        let json = String(data: data, encoding: .utf8) ?? "null"
        return try RustBridge.streamEventFromJson(json)
    }
}

// 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 chunkFromJson(_ json: String) throws -> Chunk {
    let data = json.data(using: .utf8) ?? Data()
    return try JSONDecoder().decode(Chunk.self, from: data)
}

public func streamEventFromJson(_ json: String) throws -> StreamEvent {
    let data = json.data(using: .utf8) ?? Data()
    return try JSONDecoder().decode(StreamEvent.self, from: data)
}

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