use std::path::{Path, PathBuf};
type Error = Box<dyn std::error::Error>;
type Result<T, E = Error> = std::result::Result<T, E>;
fn main() -> Result<(), String> {
println!("cargo:rerun-if-env-changed=FORCE_REBUILD");
if Path::new("proto/datafusion.proto").exists() {
println!("cargo:rerun-if-changed=proto/datafusion.proto");
build()?
}
Ok(())
}
fn build() -> Result<(), String> {
let out: PathBuf = std::env::var("OUT_DIR")
.expect("Cannot find OUT_DIR environment variable")
.into();
let descriptor_path = out.join("proto_descriptor.bin");
prost_build::Config::new()
.file_descriptor_set_path(&descriptor_path)
.compile_well_known_types()
.extern_path(".google.protobuf", "::pbjson_types")
.compile_protos(&["proto/datafusion.proto"], &["proto"])
.map_err(|e| format!("protobuf compilation failed: {e}"))?;
let descriptor_set = std::fs::read(&descriptor_path)
.unwrap_or_else(|e| panic!("Cannot read {:?}: {}", &descriptor_path, e));
pbjson_build::Builder::new()
.register_descriptors(&descriptor_set)
.unwrap_or_else(|e| {
panic!("Cannot register descriptors {:?}: {}", &descriptor_set, e)
})
.build(&[".datafusion"])
.map_err(|e| format!("pbjson compilation failed: {e}"))?;
let prost = out.join("datafusion.rs");
let pbjson = out.join("datafusion.serde.rs");
std::fs::copy(prost, "src/generated/prost.rs").unwrap();
std::fs::copy(pbjson, "src/generated/pbjson.rs").unwrap();
Ok(())
}