-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathbuild.rs
More file actions
57 lines (51 loc) · 1.79 KB
/
build.rs
File metadata and controls
57 lines (51 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! The `build.rs` script for this crate.
#[cfg(not(feature = "gen"))]
fn main() {}
#[cfg(feature = "gen")]
fn main() -> std::io::Result<()> {
let src_dir = "src/protobufs/";
let gen_dir = "src/generated/";
println!("cargo:rerun-if-changed={src_dir}");
println!("cargo:rerun-if-changed={gen_dir}");
// Allows protobuf compilation without installing the `protoc` binary
match protoc_bin_vendored::protoc_bin_path() {
Ok(protoc_path) => {
if std::env::var("PROTOC").ok().is_some() {
println!("Using PROTOC set in environment.");
} else {
println!("Setting PROTOC to protoc-bin-vendored version.");
std::env::set_var("PROTOC", protoc_path);
}
}
Err(err) => {
println!("Install protoc yourself, protoc-bin-vendored failed: {err}");
}
}
// Get sorted list of .proto files inside src_dir
let mut protos: Vec<_> = walkdir::WalkDir::new(src_dir)
.into_iter()
.filter_map(|x| x.ok())
.map(|x| x.into_path())
.filter(|x| x.extension().is_some_and(|x| x == "proto"))
.collect();
protos.sort();
let mut config = prost_build::Config::new();
config.type_attribute(
".",
"#[cfg_attr(feature = \"serde\", derive(serde::Serialize, serde::Deserialize))]",
);
config.type_attribute(
".",
"#[cfg_attr(feature = \"serde\", serde(rename_all = \"camelCase\"))]",
);
config.type_attribute(
".",
"#[cfg_attr(feature = \"ts-gen\", derive(specta::Type))]",
);
config.enum_attribute(
".",
"#[cfg_attr(feature = \"strum\", derive(strum::EnumCount, strum::EnumIter))]",
);
config.out_dir(gen_dir);
config.compile_protos(&protos, &[src_dir])
}