Expand description
tonic-build
compiles proto
files via prost
and generates service stubs
and proto definitions for use with tonic
.
§Feature flags
cleanup-markdown
: Enables cleaning up documentation from the generated code. Useful when documentation of the generated code failscargo test --doc
for example.prost
: Enables usage of prost generator (enabled by default).transport
: Enables generation ofconnect
method usingtonic::transport::Channel
(enabled by default).
§Required dependencies
[dependencies]
tonic = <tonic-version>
prost = <prost-version>
[build-dependencies]
tonic-build = <tonic-version>
§Examples
Simple
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::compile_protos("proto/service.proto")?;
Ok(())
}
Configuration
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::configure()
.build_server(false)
.compile_protos(
&["proto/helloworld/helloworld.proto"],
&["proto/helloworld"],
)?;
Ok(())
}
§NixOS related hints
On NixOS, it is better to specify the location of PROTOC
and PROTOC_INCLUDE
explicitly.
$ export PROTOBUF_LOCATION=$(nix-env -q protobuf --out-path --no-name)
$ export PROTOC=$PROTOBUF_LOCATION/bin/protoc
$ export PROTOC_INCLUDE=$PROTOBUF_LOCATION/include
$ cargo build
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.
Modules§
- Service code generation for client
- This module provides utilities for generating
tonic
service stubs and clients purely in Rust without the need ofproto
files. It also enables you to set a customCodec
if you want to use a custom serialization format other thanprotobuf
. - Service code generation for Server
Structs§
- Attributes that will be added to
mod
andstruct
items. - Builder
prost
Service generator builder. - Builder for the generic code generation of server and clients.
- Config
prost
Configuration options for Protobuf code generation. - File
Descriptor Set prost
The protocol compiler can output a FileDescriptorSet containing the .proto files it parses.
Traits§
- Method generation trait.
- Service generation trait.
Functions§
- compile_
fds prost
Simple file descriptor set compiling. Useconfigure
instead if you need more options. - compile_
protos prost
Simple.proto
compiling. Useconfigure
instead if you need more options. - configure
prost
Configuretonic-build
code generation.