Crate protobuf_codegen
source · [−]Expand description
Protobuf code generator for protobuf crate
This crate is useful mostly from build.rs scripts to generate .rs files during the build.
How to generate code
There are three main ways to generate .rs files from .proto files:
- using
protoccommand line tool andprotoc-gen-rustplugin - using this crate
Codegenwith pure rust parser - using this crate
Codegenwithprotocparser
Which one should you use depends on your needs.
If you are using non-cargo build system (like Bazel), you might prefer
using protoc-gen-rust plugin for protoc.
If you build with cargo, you probably want to use Codegen from this crate.
Protoc parser vs pure rust parser
There are two protobuf parsers which can be plugged into this crate:
protoc-based parser (protocis a command like utility from Google protobuf)- pure rust parser (
protobuf-parsecrate)
protoc-based parser is expected to parse .proto files very correctly:
all Google’s protobuf implementations rely on it.
Where there are no known bugs in protobuf-parse, it is not tested very well.
Also protobuf-parse does not implement certain rarely used features of .proto parser,
mostly complex message options specified in .proto files.
I never saw anyone using them, but you have been warned.
Note protoc command can be obtained from
protoc-bin-vendored crate.
Version 3
Note this is documentation for protobuf-codegen version 3 (which is currently in development).
In version 3 this crate encapsulates both protoc-based codegen and pure rust codegen.
In version 2 protobuf-codegen contains protoc-based codegen,
and protobuf-codegen-pure is pure rust codegen.
Example
// Use this in build.rs
protobuf_codegen::Codegen::new()
.includes(&["src/protos"])
// Inputs must reside in some of include paths.
.input("src/protos/apple.proto")
.input("src/protos/banana.proto")
.out_dir("src/protos")
.run_from_script();How to use protoc-gen-rust
If you have to.
(Note protoc can be invoked programmatically with
protoc crate)
- Install protobuf for
protocbinary.
On OS X Homebrew can be used:
brew install protobufOn Ubuntu, protobuf-compiler package can be installed:
apt-get install protobuf-compilerProtobuf is needed only for code generation, rust-protobuf runtime
does not use C++ protobuf library.
- Install
protoc-gen-rustprogram (which isprotocplugin)
It can be installed either from source or with cargo install protobuf-codegen command.
- Add
protoc-gen-rustto $PATH
If you installed it with cargo, it should be
PATH="$HOME/.cargo/bin:$PATH"- Generate .rs files:
protoc --rust_out . foo.protoThis will generate .rs files in current directory.
Customize generate code
Sometimes generated code need to be adjusted, e. g. to have custom derives.
rust-protobuf provides two options to do that:
- generated
.rsfiles contain@@protoc_insertion_point(...)markers (similar markers inserts Google’s protobuf generator for C++ or Java). Simple scriptsedone-liners can be used to replace these markers with custom annotations. Codegen::customize_callbackcan be used to patch generated code when invoked frombuild.rsscript.
Serde
rust-protobuf since version 3 no longer directly supports serde.
Rust-protobuf 3 fully supports:
- runtime reflection
- JSON parsing and printing
Which covers the most of serde use cases.
If you still need serde, generic customization callback (see above) can be used
to insert #[serde(...)] annotations.
Example project in the rust-protobuf repository demonstrates how to do it.
Structs
Entry point for .proto to .rs code generation.
Specifies style of generated code.
Generated files can be customized using this proto
or using rustproto.proto options.
Traits
Dynamic callback to customize code generation.