protoc-gen-prost

A protoc plugin that generates code using the [Prost!] code generation engine.

When used in projects that use only Rust code, the preferred mechanism for generating protobuf definitions with Prost! is to use [prost-build] from within a build.rs file. However, when working in polyglot environments, it can be advantageous to utilize common tooling in the Protocol Buffers ecosystem. One common tool used for this purpose is [buf], which simplifies the code generation process and includes several useful features, including linting, package management, and breaking change detection.

Usage

Ensure that protoc-gen-prost has been installed within a directory on your $PATH. Then invoke protoc from the command line as follows:

shell protoc --prost_out=proto/gen -I proto proto/greeter/v1/greeter.proto

Options

This tool supports all the same options from prost-build. For more information on the effects of these settings, see the related documentation from that crate:

In addition, the following options can also be specified:

A note on parameter values:

Usage with buf

When used with buf, options can be specified in the buf.gen.yaml file:

yaml version: v1 plugins: - name: prost out: gen opt: - bytes=. - compile_well_known_types - extern_path=.google.protobuf=::pbjson_types - file_descriptor_set - type_attribute=.helloworld.v1.HelloWorld=#[derive(Eq\, Hash)]

If an include file or generated crate is desired, then that should be run as a distinct step, as in the following example. For more information, see the protoc-gen-prost-crate plugin.

yaml version: v1 plugins: - name: prost out: gen/src opt: - bytes=. - file_descriptor_set - name: prost-crate out: gen strategy: all opt: - gen_crate=Cargo.toml.tpl

Extensions

When building output, protoc-gen-prost adds insertion points inside modules to make it easy to add more trait implementations. These insertion points are placed in each module and in the include file, if one was generated. Output module files are named based on the untransformed protobuf package name. Thus a package named helloworld.abstract.v1 will have an output filename of helloworld.abstract.v1.rs.

Within module files (<proto_package>.rs):

Within the include file:

Here is an example for buf using the protoc-gen-prost-serde plugin:

yaml version: v1 plugins: - name: prost out: gen/src opt: - bytes=. - file_descriptor_set - name: prost-serde out: gen/src - name: prost-crate out: gen strategy: all opt: - gen_crate=Cargo.toml.tpl