use tokio::runtime::Runtime;

mod network;

#[cxx::bridge]
mod ffi {
    extern "Rust" {
        fn download_file(url: &str, path: &str) -> Result<()>;

        fn http_get(url: &str) -> Result<String>;
    }
}

fn get_runtime() -> Runtime {
    Runtime::new().expect("创建Tokio运行时失败")
}

fn download_file(url: &str, path: &str) -> Result<(), Box<dyn std::error::Error>> {
    let rt = get_runtime();
    rt.block_on(network::download_file(url, path))?;
    Ok(())
}

fn http_get(url: &str) -> Result<String, Box<dyn std::error::Error>> {
    let rt = get_runtime();
    rt.block_on(network::http_get(url))
}
