xref: /tonic/examples/build.rs (revision e4c71b8e)
1 use std::{env, path::PathBuf};
2 
main()3 fn main() {
4     tonic_build::configure()
5         .type_attribute("routeguide.Point", "#[derive(Hash)]")
6         .compile_protos(&["proto/routeguide/route_guide.proto"], &["proto"])
7         .unwrap();
8 
9     let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
10     tonic_build::configure()
11         .file_descriptor_set_path(out_dir.join("helloworld_descriptor.bin"))
12         .compile_protos(&["proto/helloworld/helloworld.proto"], &["proto"])
13         .unwrap();
14 
15     tonic_build::compile_protos("proto/echo/echo.proto").unwrap();
16 
17     tonic_build::compile_protos("proto/unaryecho/echo.proto").unwrap();
18 
19     tonic_build::configure()
20         .server_mod_attribute("attrs", "#[cfg(feature = \"server\")]")
21         .server_attribute("Echo", "#[derive(PartialEq)]")
22         .client_mod_attribute("attrs", "#[cfg(feature = \"client\")]")
23         .client_attribute("Echo", "#[derive(PartialEq)]")
24         .compile_protos(&["proto/attrs/attrs.proto"], &["proto"])
25         .unwrap();
26 
27     tonic_build::configure()
28         .build_server(false)
29         .compile_protos(
30             &["proto/googleapis/google/pubsub/v1/pubsub.proto"],
31             &["proto/googleapis"],
32         )
33         .unwrap();
34 
35     build_json_codec_service();
36 
37     let smallbuff_copy = out_dir.join("smallbuf");
38     let _ = std::fs::create_dir(smallbuff_copy.clone()); // This will panic below if the directory failed to create
39     tonic_build::configure()
40         .out_dir(smallbuff_copy)
41         .codec_path("crate::common::SmallBufferCodec")
42         .compile_protos(&["proto/helloworld/helloworld.proto"], &["proto"])
43         .unwrap();
44 }
45 
46 // Manually define the json.helloworld.Greeter service which used a custom JsonCodec to use json
47 // serialization instead of protobuf for sending messages on the wire.
48 // This will result in generated client and server code which relies on its request, response and
49 // codec types being defined in a module `crate::common`.
50 //
51 // See the client/server examples defined in `src/json-codec` for more information.
build_json_codec_service()52 fn build_json_codec_service() {
53     let greeter_service = tonic_build::manual::Service::builder()
54         .name("Greeter")
55         .package("json.helloworld")
56         .method(
57             tonic_build::manual::Method::builder()
58                 .name("say_hello")
59                 .route_name("SayHello")
60                 .input_type("crate::common::HelloRequest")
61                 .output_type("crate::common::HelloResponse")
62                 .codec_path("crate::common::JsonCodec")
63                 .build(),
64         )
65         .build();
66 
67     tonic_build::manual::Builder::new().compile(&[greeter_service]);
68 }
69