Crate tonic_build_codec

Crate tonic_build_codec 

Source
Expand description

tonic-build compiles proto files via prost and generates service stubs and proto definitiones for use with tonic.

§Features

  • rustfmt: This feature enables the use of rustfmt to format the output code this makes the code readable and the error messages nice. This requires that rustfmt is installed. This is 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(
            &["proto/helloworld/helloworld.proto"],
            &["proto/helloworld"],
        )?;
   Ok(())
}

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§

client
Service code generation for client
server
Service code generation for Server

Structs§

Attributes
Attributes that will be added to mod and struct items.
Builderprost
Service generator builder.

Traits§

Method
Method generation trait.
Service
Service generation trait.

Functions§

compile_protosprost
Simple .proto compiling. Use configure instead if you need more options.
configureprost
Configure tonic-build code generation.
fmtrustfmt
Format files under the out_dir with rustfmt