#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use std::env;
use std::fs;
use std::path::Path;
const MODEL_URL: &str = "https://huggingface.co/Milang/captcha-solver/resolve/main/captcha.rten";
fn main() {
if env::var("CARGO_FEATURE_EMBED_MODEL").is_ok() {
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("model.rten");
let local_model_path = Path::new(&manifest_dir).join("assets/captcha.rten");
if !local_model_path.exists() && env::var("CARGO_FEATURE_DOWNLOAD_AT_BUILD").is_ok() {
println!(
"cargo:warning=Local model not found. Downloading from HuggingFace to assets/..."
);
if let Some(parent) = local_model_path.parent() {
fs::create_dir_all(parent).expect("Failed to create assets directory");
}
let client = reqwest::blocking::Client::new();
let mut response = client
.get(MODEL_URL)
.send()
.expect("Failed to send request to download model");
assert!(
response.status().is_success(),
"Failed to download model: HTTP {}",
response.status()
);
let mut file = fs::File::create(&local_model_path)
.expect("Failed to create model file in assets directory");
response
.copy_to(&mut file)
.expect("Failed to write downloaded model to file");
println!(
"cargo:warning=Model downloaded successfully to {}",
local_model_path.display()
);
}
if local_model_path.exists() {
println!(
"cargo:warning=Embedding local model from {}",
local_model_path.display()
);
fs::copy(&local_model_path, &dest_path).expect("Failed to copy local model file");
println!("cargo:rerun-if-changed={}", local_model_path.display());
} else {
println!(
"cargo:warning=Model file not found at {}. Embedded model validation will fail.",
local_model_path.display()
);
panic!(
"Model file not found at {}. Run with --features download-at-build to download automatically, or ensure the file exists.",
local_model_path.display()
);
}
println!("cargo:rerun-if-env-changed=CARGO_FEATURE_EMBED_MODEL");
println!("cargo:rerun-if-env-changed=CARGO_FEATURE_DOWNLOAD_AT_BUILD");
}
}