---
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 Span;
        #[swift_bridge(init)]
        fn new(start_byte: usize, end_byte: usize) -> Span;
        #[swift_bridge(swift_name = "startByte")]
        fn start_byte(&self) -> usize;
        #[swift_bridge(swift_name = "endByte")]
        fn end_byte(&self) -> usize;
    }

    extern "Rust" {
        type Diagnostic;
        #[swift_bridge(init)]
        fn new(message: String, span: Span) -> Diagnostic;
        fn message(&self) -> String;
        fn span(&self) -> Span;
    }

    extern "Rust" {
        type ProcessResult;
        #[swift_bridge(init)]
        fn new(language: String, diagnostics: Vec<Diagnostic>, tags: Vec<String>) -> ProcessResult;
        fn language(&self) -> String;
        fn diagnostics(&self) -> Vec<Diagnostic>;
        fn tags(&self) -> Vec<String>;
    }

    extern "Rust" {
        #[swift_bridge(swift_name = "spanFromJson")]
        fn span_from_json(json: String) -> Result<Span, String>;
        #[swift_bridge(swift_name = "diagnosticFromJson")]
        fn diagnostic_from_json(json: String) -> Result<Diagnostic, String>;
        #[swift_bridge(swift_name = "processResultFromJson")]
        fn process_result_from_json(json: String) -> Result<ProcessResult, String>;
    }
}


pub struct Span(pub demo::Span);
impl Span {
    pub fn new(start_byte: usize, end_byte: usize) -> Span {
        Span(demo::Span {
            start_byte,
            end_byte,
        })
    }
    pub fn start_byte(&self) -> usize {
        ::serde_json::to_value(&self.0.start_byte).ok().and_then(|j| ::serde_json::from_value(j).ok()).unwrap_or_default()
    }
    pub fn end_byte(&self) -> usize {
        ::serde_json::to_value(&self.0.end_byte).ok().and_then(|j| ::serde_json::from_value(j).ok()).unwrap_or_default()
    }
}

pub struct Diagnostic(pub demo::Diagnostic);
impl Diagnostic {
    pub fn new(message: String, span: Span) -> Diagnostic {
        let mut __target: demo::Diagnostic = ::std::default::Default::default();
        {
            // Try JSON parse first (handles enum/object values); on parse failure
            // treat the raw input as a JSON string scalar so plain `String` /
            // string-like enum fields don't end up empty for inputs that aren't
            // valid JSON tokens (e.g. `tts-1`).
            let __v = ::serde_json::from_str::<::serde_json::Value>(&message)
                .unwrap_or(::serde_json::Value::String(message.clone()));
            if let Ok(t) = ::serde_json::from_value(__v) { __target.message = t; }
        }
        __target.span = span.0;
        Diagnostic(__target)
    }
    pub fn message(&self) -> String {
        self.0.message.clone()
    }
    pub fn span(&self) -> Span {
        Span(self.0.span.clone())
    }
}

pub struct ProcessResult(pub demo::ProcessResult);
impl ProcessResult {
    pub fn new(language: String, diagnostics: Vec<Diagnostic>, tags: Vec<String>) -> ProcessResult {
        let mut __target: demo::ProcessResult = ::std::default::Default::default();
        {
            // Try JSON parse first (handles enum/object values); on parse failure
            // treat the raw input as a JSON string scalar so plain `String` /
            // string-like enum fields don't end up empty for inputs that aren't
            // valid JSON tokens (e.g. `tts-1`).
            let __v = ::serde_json::from_str::<::serde_json::Value>(&language)
                .unwrap_or(::serde_json::Value::String(language.clone()));
            if let Ok(t) = ::serde_json::from_value(__v) { __target.language = t; }
        }
        __target.diagnostics = diagnostics.into_iter().map(|w| w.0).collect();
        if let Ok(__v) = ::serde_json::to_value(tags) {
            if let Ok(t) = ::serde_json::from_value(__v) { __target.tags = t; }
        }
        ProcessResult(__target)
    }
    pub fn language(&self) -> String {
        self.0.language.clone()
    }
    pub fn diagnostics(&self) -> Vec<Diagnostic> {
        self.0.diagnostics.iter().map(|elem| Diagnostic(elem.clone())).collect()
    }
    pub fn tags(&self) -> Vec<String> {
        ::serde_json::to_value(&self.0.tags).ok().and_then(|j| ::serde_json::from_value(j).ok()).unwrap_or_default()
    }
}

pub fn span_from_json(json: String) -> Result<Span, String> {
    serde_json::from_str::<demo::Span>(&json)
        .map(Span)
        .map_err(|e| e.to_string())
}
pub fn diagnostic_from_json(json: String) -> Result<Diagnostic, String> {
    serde_json::from_str::<demo::Diagnostic>(&json)
        .map(Diagnostic)
        .map_err(|e| e.to_string())
}
pub fn process_result_from_json(json: String) -> Result<ProcessResult, String> {
    serde_json::from_str::<demo::ProcessResult>(&json)
        .map(ProcessResult)
        .map_err(|e| e.to_string())
}
