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");
println!("cargo:rerun-if-changed=proto/datafusion.proto");
build()?;
Ok(())
}
fn build() -> Result<(), String> {
use std::io::Write;
let out = std::path::PathBuf::from(
std::env::var("OUT_DIR").expect("Cannot find OUT_DIR environment variable"),
);
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))?;
#[cfg(feature = "json")]
let descriptor_set = std::fs::read(&descriptor_path)
.expect(&*format!("Cannot read {:?}", &descriptor_path));
#[cfg(feature = "json")]
pbjson_build::Builder::new()
.register_descriptors(&descriptor_set)
.expect(&*format!(
"Cannot register descriptors {:?}",
&descriptor_set
))
.build(&[".datafusion"])
.map_err(|e| format!("pbjson compilation failed: {}", e))?;
let proto = std::fs::read_to_string(out.join("datafusion.rs")).unwrap();
#[cfg(feature = "json")]
let json = std::fs::read_to_string(out.join("datafusion.serde.rs")).unwrap();
#[cfg(feature = "docsrs")]
let path = out.join("datafusion.rs");
#[cfg(not(feature = "docsrs"))]
let path = "src/generated/datafusion.rs";
let mut file = std::fs::OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(path)
.unwrap();
file.write_all(proto.as_str().as_ref()).unwrap();
#[cfg(feature = "json")]
file.write_all(json.as_str().as_ref()).unwrap();
Ok(())
}