use std::{
fs::OpenOptions,
io::{Read, Write},
path::Path,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let path = Path::new("../format/Flight.proto");
if path.exists() {
println!("cargo:rerun-if-changed=../format/Flight.proto");
let proto_dir = Path::new("../format");
let proto_path = Path::new("../format/Flight.proto");
tonic_build::configure()
.protoc_arg("--experimental_allow_proto3_optional")
.out_dir("src")
.compile_with_config(prost_config(), &[proto_path], &[proto_dir])?;
let mut file = OpenOptions::new()
.read(true)
.open("src/arrow.flight.protocol.rs")?;
let mut buffer = String::new();
file.read_to_string(&mut buffer)?;
let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.open("src/arrow.flight.protocol.rs")?;
file.write_all("// This file was automatically generated through the build.rs script, and should not be edited.\n\n".as_bytes())?;
file.write_all(buffer.as_bytes())?;
}
let path = Path::new("../format/FlightSql.proto");
if path.exists() {
println!("cargo:rerun-if-changed=../format/FlightSql.proto");
let proto_dir = Path::new("../format");
let proto_path = Path::new("../format/FlightSql.proto");
tonic_build::configure()
.protoc_arg("--experimental_allow_proto3_optional")
.out_dir("src/sql")
.compile_with_config(prost_config(), &[proto_path], &[proto_dir])?;
let mut file = OpenOptions::new()
.read(true)
.open("src/sql/arrow.flight.protocol.sql.rs")?;
let mut buffer = String::new();
file.read_to_string(&mut buffer)?;
let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.open("src/sql/arrow.flight.protocol.sql.rs")?;
file.write_all("// This file was automatically generated through the build.rs script, and should not be edited.\n\n".as_bytes())?;
file.write_all(buffer.as_bytes())?;
}
let google_protobuf_rs = Path::new("src/sql/google.protobuf.rs");
if google_protobuf_rs.exists() && google_protobuf_rs.metadata().unwrap().len() == 0 {
std::fs::remove_file(google_protobuf_rs).unwrap();
}
Ok(())
}
fn prost_config() -> prost_build::Config {
let mut config = prost_build::Config::new();
config.bytes([".arrow"]);
config
}