Name Date Size #Lines LOC

..12-Mar-2025-

src/H12-Mar-2025-2,7461,956

Cargo.tomlH A D12-Mar-20251.1 KiB4740

LICENSEH A D12-Mar-20251 KiB2016

README.mdH A D12-Mar-20252.5 KiB10587

README.md

1# tonic-build
2
3Compiles proto files via prost and generates service stubs and proto definitions for use with tonic.
4
5## Features
6
7Required dependencies
8
9```toml
10[dependencies]
11tonic = "<tonic-version>"
12prost = "<prost-version>"
13
14[build-dependencies]
15tonic-build = "<tonic-version>"
16```
17
18## Examples
19
20### Simple
21
22In `build.rs`:
23```rust
24fn main() -> Result<(), Box<dyn std::error::Error>> {
25    tonic_build::compile_protos("proto/service.proto")?;
26    Ok(())
27}
28```
29
30### Configuration
31
32```rust
33fn main() -> Result<(), Box<dyn std::error::Error>> {
34   tonic_build::configure()
35        .build_server(false)
36        .compile_protos(
37            &["proto/helloworld/helloworld.proto"],
38            &["proto/helloworld"],
39        )?;
40   Ok(())
41}
42```
43See [more examples here](https://github.com/hyperium/tonic/tree/master/examples)
44
45### Google APIs example
46A good way to use Google API is probably using git submodules.
47
48So suppose in our `proto` folder we do:
49```
50git submodule add https://github.com/googleapis/googleapis
51
52git submodule update --remote
53```
54
55And a bunch of Google proto files in structure will be like this:
56```
57├── googleapis
58│   └── google
59│       ├── api
60│       │   ├── annotations.proto
61│       │   ├── client.proto
62│       │   ├── field_behavior.proto
63│       │   ├── http.proto
64│       │   └── resource.proto
65│       └── pubsub
66│           └── v1
67│               ├── pubsub.proto
68│               └── schema.proto
69```
70
71Then we can generate Rust code via this setup in our `build.rs`
72```rust
73fn main() {
74    tonic_build::configure()
75        .build_server(false)
76        //.out_dir("src/google")  // you can change the generated code's location
77        .compile_protos(
78            &["proto/googleapis/google/pubsub/v1/pubsub.proto"],
79            &["proto/googleapis"], // specify the root location to search proto dependencies
80        ).unwrap();
81}
82```
83
84Then you can reference the generated Rust like this this in your code:
85```rust
86pub mod api {
87    tonic::include_proto!("google.pubsub.v1");
88}
89use api::{publisher_client::PublisherClient, ListTopicsRequest};
90```
91
92Or if you want to save the generated code in your own code base,
93you can uncomment the line `.out_dir(...)` above, and in your lib file
94config a mod like this:
95```rust
96pub mod google {
97    #[path = ""]
98    pub mod pubsub {
99        #[path = "google.pubsub.v1.rs"]
100        pub mod v1;
101    }
102}
103```
104See [the example here](https://github.com/hyperium/tonic/tree/master/examples/src/gcp)
105