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

#![allow(unused_variables, unreachable_code, unreachable_patterns)]
#![allow(
    clippy::collapsible_if,
    clippy::collapsible_match,
    clippy::needless_borrows_for_generic_args,
    clippy::field_reassign_with_default,
    clippy::too_many_arguments,
    clippy::clone_on_copy,
    clippy::unnecessary_cast,
    clippy::manual_flatten,
    clippy::match_single_binding,
    clippy::redundant_closure,
    clippy::useless_conversion,
    clippy::inherent_to_string,
)]


/// Process-wide tokio runtime shared across every swift-bridge async wrapper.
///
/// alef-emitted; see shims.rs for the rationale (orphaned reqwest connection
/// pools when each call creates and drops its own current-thread runtime).
fn __alef_tokio_runtime() -> &'static ::tokio::runtime::Runtime {
    use std::sync::OnceLock;
    static RT: OnceLock<::tokio::runtime::Runtime> = OnceLock::new();
    RT.get_or_init(|| {
        ::tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .build()
            .expect("build process-wide alef tokio runtime")
    })
}

#[swift_bridge::bridge]
mod ffi {
    extern "Rust" {
        type KeywordConfig;
        #[swift_bridge(init)]
        fn new(max_keywords: usize, ngram_range: Vec<usize>) -> KeywordConfig;
        #[swift_bridge(swift_name = "maxKeywords")]
        fn max_keywords(&self) -> usize;
        #[swift_bridge(swift_name = "ngramRange")]
        fn ngram_range(&self) -> Vec<usize>;
    }

    extern "Rust" {
        #[swift_bridge(swift_name = "extractKeywords")]
        fn extract_keywords(text: String, config: KeywordConfig) -> Result<Vec<String>, String>;
    }

    extern "Rust" {
        #[swift_bridge(swift_name = "keywordConfigFromJson")]
        fn keyword_config_from_json(json: String) -> Result<KeywordConfig, String>;
    }
}


pub struct KeywordConfig(pub demo::KeywordConfig);
impl KeywordConfig {
    pub fn new(max_keywords: usize, ngram_range: Vec<usize>) -> KeywordConfig {
        let mut __target: demo::KeywordConfig = ::std::default::Default::default();
        __target.max_keywords = max_keywords;
        if let Ok(__v) = ::serde_json::to_value(ngram_range) {
            if let Ok(t) = ::serde_json::from_value(__v) { __target.ngram_range = t; }
        }
        KeywordConfig(__target)
    }
    pub fn max_keywords(&self) -> usize {
        ::serde_json::to_value(&self.0.max_keywords).ok().and_then(|j| ::serde_json::from_value(j).ok()).unwrap_or_default()
    }
    pub fn ngram_range(&self) -> Vec<usize> {
        ::serde_json::to_value(&self.0.ngram_range).ok().and_then(|j| ::serde_json::from_value(j).ok()).unwrap_or_default()
    }
}

pub fn extract_keywords(text: String, config: KeywordConfig) -> Result<Vec<String>, String> {
    demo::extract_keywords(text, config.0).map_err(|e| e.to_string()).map(|v| v.into_iter().map(|s| s.to_string()).collect::<Vec<_>>())
}

pub fn keyword_config_from_json(json: String) -> Result<KeywordConfig, String> {
    serde_json::from_str::<demo::KeywordConfig>(&json)
        .map(KeywordConfig)
        .map_err(|e| e.to_string())
}
