---
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
/// Keyword extraction configuration.
public struct KeywordConfig: Codable, Sendable, Hashable {
    public let maxKeywords: UInt
    public let ngramRange: [UInt]
    public init(maxKeywords: UInt, ngramRange: [UInt]) {
        self.maxKeywords = maxKeywords
        self.ngramRange = ngramRange
    }
    private enum CodingKeys: String, CodingKey {
        case maxKeywords = "max_keywords"
        case ngramRange = "ngram_range"
    }
    public init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.maxKeywords = try container.decodeIfPresent(UInt.self, forKey: .maxKeywords) ?? 0
        self.ngramRange = try container.decodeIfPresent([UInt].self, forKey: .ngramRange) ?? []
    }
}

// MARK: - Internal FFI conversions for KeywordConfig
internal extension KeywordConfig {
    init(_ rb: RustBridge.KeywordConfigRef) throws {
        self.maxKeywords = rb.maxKeywords()
        self.ngramRange = Array(rb.ngramRange())
    }

    func intoRust() throws -> RustBridge.KeywordConfig {
        let __ngramRange = RustVec<UInt>()
        for __elem in self.ngramRange { __ngramRange.push(value: __elem) }
        return RustBridge.KeywordConfig(self.maxKeywords, __ngramRange)
    }
}

public typealias KeywordConfigRef = RustBridge.KeywordConfigRef

public typealias KeywordConfigRefMut = RustBridge.KeywordConfigRefMut

/// Errors emitted by keyword extraction.
public enum DemoError: Swift.Error {
    /// Extraction encountered an error.
    case extractionFailed
    /// Synthetic case raised when a Rust serde unit-enum raw value cannot
    /// be mapped to a known Swift case during DTO unmarshaling.
    case validation(message: String, source: String)
}

// MARK: - JSON-String Convenience Overloads
// These overloads accept JSON-encoded config parameters and decode them automatically.
// Enables e2e tests to pass JSON strings directly without typed config construction.

/// Resolves a string argument as either a file path or literal UTF-8 content.
/// Searches: current working directory, ALEF_TEST_DOCUMENTS_DIR env var,
/// and ancestor `test_documents/` or `fixtures/` directories (up to 16 levels).
/// If no file is found, treats the string as UTF-8 content and returns its bytes.
private func _loadBytesFromPathOrUtf8(_ pathOrContent: String) throws -> [UInt8] {
    let fm = FileManager.default
    var roots: [String] = [fm.currentDirectoryPath]
    if let envRoot = ProcessInfo.processInfo.environment["ALEF_TEST_DOCUMENTS_DIR"] {
        roots.append(envRoot)
    }
    var walker = URL(fileURLWithPath: fm.currentDirectoryPath)
    for _ in 0..<16 {
        roots.append(walker.appendingPathComponent("test_documents").path)
        roots.append(walker.appendingPathComponent("fixtures").path)
        let parent = walker.deletingLastPathComponent()
        if parent.path == walker.path { break }
        walker = parent
    }
    let candidates = [pathOrContent] + roots.map { ($0 as NSString).appendingPathComponent(pathOrContent) }
    for path in candidates {
        if fm.fileExists(atPath: path), let data = try? Data(contentsOf: URL(fileURLWithPath: path)) {
            return [UInt8](data)
        }
    }
    return [UInt8](pathOrContent.utf8)
}

public func extractKeywords(_ text: String, _ configJson: String) throws -> [String] {
    let config = try keywordConfigFromJson(configJson)
    return try extractKeywords(text: text, config: config)
}
// 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 keywordConfigFromJson(_ json: String) throws -> KeywordConfig {
    let data = json.data(using: .utf8) ?? Data()
    return try JSONDecoder().decode(KeywordConfig.self, from: data)
}
// MARK: - Free-function Forwarders
// Re-export every public free function on the source Rust crate as a
// top-level `public func` on the host module so consumers do not need to
// `import RustBridge` directly. Forwarders take Swift-native parameter
// types and convert to the swift-bridge runtime types internally.

/// Extract keywords from text.
public func extractKeywords(text: String, config: KeywordConfig) throws -> [String] {
        let _rb_text = RustString(text)
        let _rb_config = try config.intoRust()
    return try RustBridge.extractKeywords(_rb_text, _rb_config).map { $0.as_str().toString() }
}
// swift-bridge opaque type used across Task.detached boundaries — Rust type is Send + Sync.
extension RustBridge.KeywordConfig: @unchecked Sendable {}
