use std::path::Path;
fn main() -> Result<(), String> {
use std::io::Write;
let out = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
println!("cargo:rerun-if-env-changed=FORCE_REBUILD");
let version = rustc_version::version().unwrap();
println!("cargo:rustc-env=RUSTC_VERSION={version}");
#[cfg(feature = "docsrs")]
let path = out.join("ballista.rs");
#[cfg(not(feature = "docsrs"))]
let path = "src/serde/generated/ballista.rs";
if Path::new("proto/datafusion.proto").exists() {
println!("cargo:rerun-if-changed=proto/datafusion.proto");
println!("cargo:rerun-if-changed=proto/ballista.proto");
tonic_build::configure()
.extern_path(".datafusion", "::datafusion_proto::protobuf")
.compile(&["proto/ballista.proto"], &["proto"])
.map_err(|e| format!("protobuf compilation failed: {e}"))?;
let generated_source_path = out.join("ballista.protobuf.rs");
let code = std::fs::read_to_string(generated_source_path).unwrap();
let mut file = std::fs::OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(path)
.unwrap();
file.write_all(code.as_str().as_ref()).unwrap();
}
Ok(())
}