-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
40 lines (35 loc) · 1.51 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
// rust proto generate code
use protobuf_codegen::{Codegen, Customize, CustomizeCallback};
use protoc_bin_vendored;
use protobuf::descriptor::field_descriptor_proto::Type;
use protobuf::reflect::FieldDescriptor;
fn main() {
struct GenSerde;
impl CustomizeCallback for GenSerde {
// fn message(&self, _message: &MessageDescriptor) -> Customize {
// Customize::default().before("#[derive(::serde::Serialize, ::serde::Deserialize)]")
// }
fn field(&self, field: &FieldDescriptor) -> Customize {
if field.proto().type_() == Type::TYPE_ENUM {
// `EnumOrUnknown` is not a part of rust-protobuf, so external serializer is needed.
Customize::default().before(
"#[serde(serialize_with = \"crate::serialize_enum_or_unknown\", deserialize_with = \"crate::deserialize_enum_or_unknown\")]")
} else {
Customize::default()
}
}
}
Codegen::new()
// Use `protoc` parser, optional.
.protoc()
// Use `protoc-bin-vendored` bundled protoc command, optional.
.protoc_path(&protoc_bin_vendored::protoc_bin_path().unwrap())
// All inputs and imports from the inputs must reside in `includes` directories.
.includes(&["src/protos"])
// Inputs must reside in some of include paths.
.input("src/protos/slinky_oracle.proto")
// Specify output directory relative to Cargo output directory.
.out_dir("src/")
.customize_callback(GenSerde)
.run_from_script();
}