-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbuild.rs
64 lines (51 loc) · 1.74 KB
/
build.rs
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
58
59
60
61
62
63
64
use walkdir::WalkDir;
fn main() -> std::io::Result<()> {
let protobufs_dir = "src/protobufs/";
println!("cargo:rerun-if-changed={}", protobufs_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}");
}
}
let mut protos = vec![];
for entry in WalkDir::new(protobufs_dir)
.into_iter()
.map(|e| e.unwrap())
.filter(|e| {
e.path()
.extension()
.map_or(false, |ext| ext.to_str().unwrap() == "proto")
})
{
let path = entry.path();
protos.push(path.to_owned());
}
let mut config = prost_build::Config::new();
let mut derive_string = String::from("#[derive(");
#[cfg(feature = "serde")]
{
derive_string.push_str("serde::Serialize, serde::Deserialize, ");
}
#[cfg(feature = "ts-gen")]
{
derive_string.push_str("specta::Type, ");
}
derive_string.push_str(")]");
config.type_attribute(".", derive_string.as_str());
#[cfg(feature = "serde")]
{
config.type_attribute(".", "#[serde(rename_all = \"camelCase\")]");
config.type_attribute(".", "#[allow(clippy::doc_lazy_continuation)]");
}
config.compile_protos(&protos, &[protobufs_dir]).unwrap();
Ok(())
}