use prost_build;
use std::{env, fs, path::PathBuf};
fn main() {
generate_protocols();
}
fn generate_protocols() {
let mut config = prost_build::Config::new();
config
.type_attribute("NodeAddress", "#[derive(Eq, Hash)]")
.compile_protos(&protocol_files(), &protocol_includes())
.expect("Error compiling protobuf definitions");
for file in generated_files() {
fs::copy(
&file,
format!(
"src/generated/{}",
file.file_name().unwrap().to_string_lossy()
),
)
.expect(
"Could not copy \
generated code to \
src/generated",
);
}
}
fn generated_files() -> Vec<PathBuf> {
let mut files = vec![];
for entry in fs::read_dir(env::var("OUT_DIR").unwrap()).unwrap() {
let file = entry.unwrap();
if file.file_name().to_str().unwrap().ends_with(".rs") {
if file.metadata().unwrap().is_file() {
files.push(file.path());
}
}
}
files
}
fn protocol_includes() -> Vec<String> {
vec!["proto".to_string()]
}
fn protocol_files() -> Vec<String> {
let mut files = vec![];
for entry in fs::read_dir("proto").unwrap() {
let file = entry.unwrap();
if file.file_name().to_str().unwrap().starts_with(".") {
continue;
}
if file.metadata().unwrap().is_file() {
files.push(file.path().to_string_lossy().into_owned());
}
}
files
}