fn main() -> Result<(), Box<dyn std::error::Error>> {
    const PROTO_FILES: &[&str] = &["proto/multi_file.proto"];
    const GENERATED_DIR: &str = "generated";
    const GENERATED_PROTO_RS: &str = "generated/storage_backend.multi_file.rs";
    const GENERATED_RS: &str = "generated/multi_file.rs";

    std::fs::create_dir_all(GENERATED_DIR)?;
    let mut config = prost_build::Config::new();
    config.out_dir(GENERATED_DIR);
    config.type_attribute(".", "#[derive(Eq)]");
    config.compile_protos(PROTO_FILES, &["proto"])?;
    if std::path::Path::new(GENERATED_RS).exists() {
        std::fs::remove_file(GENERATED_RS)?;
    }
    std::fs::rename(GENERATED_PROTO_RS, GENERATED_RS)?;

    for proto in PROTO_FILES {
        println!("cargo:rerun-if-changed={proto}");
    }
    println!("cargo:rerun-if-changed=build.rs");

    Ok(())
}
