#protobuf #serialization #prost

bin+lib protoc-gen-prost

Protocol Buffers compiler plugin powered by Prost!

8 releases

Uses new Rust 2021

0.2.1 Nov 14, 2022
0.2.0 Aug 8, 2022
0.1.4 Jun 19, 2022
0.1.3 Mar 31, 2022
0.0.1 Feb 24, 2022

#147 in Encoding

Download history 899/week @ 2022-08-08 3394/week @ 2022-08-15 5154/week @ 2022-08-22 2040/week @ 2022-08-29 2041/week @ 2022-09-05 1852/week @ 2022-09-12 1789/week @ 2022-09-19 1735/week @ 2022-09-26 1680/week @ 2022-10-03 1635/week @ 2022-10-10 1521/week @ 2022-10-17 1396/week @ 2022-10-24 1409/week @ 2022-10-31 1021/week @ 2022-11-07 1128/week @ 2022-11-14 898/week @ 2022-11-21

4,542 downloads per month
Used in 6 crates (5 directly)

Apache-2.0

32KB
658 lines

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:

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:

  • file_descriptor_set(=<boolean>): Includes the encoded FileDescriptorSet in the generated output for each module. Note that this behavior is different from prost-build in that each module only includes the file descriptors for that module. This allows for better scoping of information when passing to a protobuf reflection API, and less exposure of useless or excess information. In addition, this module embeds the raw file descriptors without having first decoded them with Prost!, ensuring that extensions and unexpected tags are preserved.

A note on parameter values:

  • <attribute>: All ,s appearing in the value must be \ escaped (i.e. \,) This is due to the fact that internally, protoc joins all passed parameters with a , before sending it as a single string to the underlying plugin.
  • <proto_path>: Protobuf paths beginning with . will be matched from the global root (prefix matches). All other paths will be matched as suffix matches.
  • (=<boolean>): Boolean values may be specified after a parameter, but if not, the value is assumed to be true by virtue of having listed the parameter.

Usage with buf

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

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)]

The protoc-gen-prost plugin is also published on the Buf Schema Registry as a plugin which you can execute remotely, without needing to explicitly install this tool. See the plugin listing to identify the latest published version for use. The plugin is referenced as follows:

version: v1
plugins:
  - remote: buf.build/prost/plugins/prost:v0.1.4-1
    out: gen

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.

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):

  • module: Appends to the end of the module file

Within the include file:

  • <proto_package>: Appends to the module defined for this package

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

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

Dependencies

~3.5–4.5MB
~102K SLoC