tonic-build
Provides code generation for service stubs to use with tonic. For protobuf compilation via prost, use the tonic-prost-build
crate.
Feature flags
cleanup-markdown
: Enables cleaning up documentation from the generated code. Useful when documentation of the generated code failscargo test --doc
for example. Theprost
feature must be enabled to use this feature.prost
: Enables usage of prost generator (enabled by default).transport
: Enables generation ofconnect
method usingtonic::transport::Channel
(enabled by default).
Features
Required dependencies
[]
= "<tonic-version>"
= "<prost-version>"
[]
= "<tonic-version>"
Getting Started
For protobuf compilation, use tonic-prost-build
in your build.rs
file at the root of the binary/library.
You can rely on the defaults via
Or configure the generated code deeper via
For further details how to use the generated client/server, see the examples here or the Google APIs example below.
NixOS related hints
On NixOS, it is better to specify the location of PROTOC
and PROTOC_INCLUDE
explicitly.
The reason being that if prost_build::compile_protos
fails to generate the resultant package,
the failure is not obvious until the include!(concat!(env!("OUT_DIR"), "/resultant.rs"));
fails with No such file or directory
error.
Google APIs example
A good way to use Google API is probably using git submodules.
So suppose in our proto
folder we do:
And a bunch of Google proto files in structure will be like this:
├── googleapis
│ └── google
│ ├── api
│ │ ├── annotations.proto
│ │ ├── client.proto
│ │ ├── field_behavior.proto
│ │ ├── http.proto
│ │ └── resource.proto
│ └── pubsub
│ └── v1
│ ├── pubsub.proto
│ └── schema.proto
Then we can generate Rust code via this setup in our build.rs
:
Then you can reference the generated Rust like this this in your code:
use ;
Or if you want to save the generated code in your own code base,
you can uncomment the line .out_dir(...)
above, and in your lib file
config a mod like this:
See the example here