1use std::env;
 2use std::fs;
 3use std::path::PathBuf;
 4
 5fn main() -> Result<(), Box<dyn std::error::Error>> {
 6    copy_extension_api_rust_files()
 7}
 8
 9/// rust-analyzer doesn't support include! for files from outside the crate.
10/// Copy them to the OUT_DIR, so we can include them from there, which is supported.
11fn copy_extension_api_rust_files() -> Result<(), Box<dyn std::error::Error>> {
12    let out_dir = env::var("OUT_DIR")?;
13    let input_dir = PathBuf::from("../extension_api/wit");
14    let output_dir = PathBuf::from(out_dir);
15
16    println!("cargo:rerun-if-changed={}", input_dir.display());
17
18    for entry in fs::read_dir(&input_dir)? {
19        let entry = entry?;
20        let path = entry.path();
21        if path.is_dir() {
22            println!("cargo:rerun-if-changed={}", path.display());
23
24            for subentry in fs::read_dir(&path)? {
25                let subentry = subentry?;
26                let subpath = subentry.path();
27                if subpath.extension() == Some(std::ffi::OsStr::new("rs")) {
28                    let relative_path = subpath.strip_prefix(&input_dir)?;
29                    let destination = output_dir.join(relative_path);
30
31                    fs::create_dir_all(destination.parent().unwrap())?;
32                    fs::copy(&subpath, &destination)?;
33                }
34            }
35        } else if path.extension() == Some(std::ffi::OsStr::new("rs")) {
36            let relative_path = path.strip_prefix(&input_dir)?;
37            let destination = output_dir.join(relative_path);
38
39            fs::create_dir_all(destination.parent().unwrap())?;
40            fs::copy(&path, &destination)?;
41            println!("cargo:rerun-if-changed={}", path.display());
42        }
43    }
44
45    Ok(())
46}