use std::{env, fs, path::PathBuf};
use prost_wkt_build::{FileDescriptorSet, Message as _};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let spark_descriptor_file = out_dir.clone().join("spark_descriptor.bin");
tonic_build::configure()
.server_mod_attribute(".", "#[cfg(feature = \"server\")]")
.client_mod_attribute(".", "#[cfg(feature = \"client\")]")
.type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]")
.type_attribute(".", "#[derive(utoipa::ToSchema, utoipa::ToResponse)]")
.type_attribute(".", "#[serde(rename_all = \"camelCase\")]")
.extern_path(".google.protobuf.Any", "::prost_wkt_types::Any")
.extern_path(".google.protobuf.Timestamp", "::prost_wkt_types::Timestamp")
.extern_path(".google.protobuf.Value", "::prost_wkt_types::Value")
.protoc_arg("--experimental_allow_proto3_optional")
.file_descriptor_set_path(&spark_descriptor_file)
.compile_protos(
&[
"v1/common/types.proto",
"v1/frost/service.proto",
"v1/spark/service.proto",
"v1/spark_authn/service.proto",
"v1/spark_tree/service.proto",
"v1/validate/api.proto",
],
&["protos/spark"],
)
.unwrap();
let spark_descriptor_bytes = std::fs::read(&spark_descriptor_file).unwrap();
let spark_descriptor = FileDescriptorSet::decode(&spark_descriptor_bytes[..]).unwrap();
prost_wkt_build::add_serde(out_dir.clone(), spark_descriptor);
let mut lib_rs_path = out_dir.clone();
lib_rs_path.push("lib.rs");
fs::write(
lib_rs_path,
"#![allow(non_camel_case_types)]\n\ninclude!(concat!(env!(\"OUT_DIR\"), \"/spark.rs\"));\n",
)?;
println!("cargo:rerun-if-changed=protos");
Ok(())
}