forked from tafia/quick-protobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
75 lines (67 loc) · 2.15 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
65
66
67
68
69
70
71
72
73
74
75
extern crate pb_rs;
extern crate prost_build;
extern crate protobuf_codegen_pure;
use pb_rs::types::{Config, FileDescriptor, RpcService};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::{env, fs};
fn generate_rpc_test<W: Write + ?Sized>(
rpc: &RpcService,
w: &mut W,
) -> Result<(), pb_rs::errors::Error> {
/* Example:
trait <service> {
fn <func>(&self, arg: &<arg>) -> Result<<ret>, failure::Error>;
}
*/
writeln!(w, "\npub trait {SERVICE} {{", SERVICE = rpc.service_name)?;
for func in rpc.functions.iter() {
writeln!(
w,
" fn {FUNC}(&self, arg: &{ARG}) -> std::result::Result<{RET}, quick_protobuf::Error>;",
FUNC = func.name,
ARG = func.arg,
RET = func.ret
)?;
}
writeln!(w, "}}\n")?;
Ok(())
}
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
// protobuf
protobuf_codegen_pure::Codegen::new()
.out_dir("src")
.inputs(&["src/perftest_data.proto"])
.include("src")
.run()
.expect("protoc");
// quick-protobuf
let quick_dest = Path::new(&out_dir).join("perftest_data_quick.rs");
let config = Config {
in_file: PathBuf::from("src/perftest_data.proto"),
out_file: quick_dest,
single_module: true,
import_search_path: vec![PathBuf::from("src")],
no_output: false,
error_cycle: false,
headers: false,
dont_use_cow: false,
custom_struct_derive: vec![],
custom_repr: None,
custom_rpc_generator: Box::new(|rpc, writer| generate_rpc_test(rpc, writer)),
custom_includes: Vec::new(),
owned: false,
hashbrown: false,
nostd: false,
gen_info: false,
add_deprecated_fields: false,
};
FileDescriptor::write_proto(&config).unwrap();
// prost
let old = ::std::env::var("OUT_DIR");
env::set_var("OUT_DIR", ".");
prost_build::compile_protos(&["src/perftest_data.proto"], &["src"]).unwrap();
let _ = old.map(|val| env::set_var("OUT_DIR", &val));
fs::rename("perftest_data.rs", "src/perftest_data_prost.rs").unwrap();
}