• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..--

src/25-Apr-2025-8,4457,175

.cargo-checksum.jsonD25-Apr-20254.5 KiB11

Android.bpD25-Apr-20251.7 KiB7569

Cargo.lockD25-Apr-20257.3 KiB290256

Cargo.tomlD25-Apr-20251.3 KiB5845

LICENSED25-Apr-20251 KiB1916

LICENSE.txtD25-Apr-20251 KiB1916

METADATAD25-Apr-2025509 1817

MODULE_LICENSE_MITD25-Apr-20250

NOTICED25-Apr-20251 KiB1916

README.mdD25-Apr-20254 KiB13086

cargo_embargo.jsonD25-Apr-2025126 109

README.md

1<!-- cargo-sync-readme start -->
2
3# Protobuf code generator for `protobuf` crate
4
5This crate is useful mostly from `build.rs` scripts to generate `.rs` files during the build.
6
7# How to generate code
8
9There are three main ways to generate `.rs` files from `.proto` files:
10* using `protoc` command line tool and `protoc-gen-rust` plugin
11* using this crate `Codegen` with pure rust parser
12* using this crate `Codegen` with `protoc` parser
13
14Which one should you use depends on your needs.
15
16If you are using non-cargo build system (like Bazel), you might prefer
17using `protoc-gen-rust` plugin for `protoc`.
18
19If you build with `cargo`, you probably want to use `Codegen` from this crate.
20
21# Protoc parser vs pure rust parser
22
23There are two protobuf parsers which can be plugged into this crate:
24* `protoc`-based parser (`protoc` is a command like utility from Google protobuf)
25* pure rust parser (`protobuf-parse` crate)
26
27`protoc`-based parser is expected to parse `.proto` files very correctly:
28all Google's protobuf implementations rely on it.
29
30While there are no known bugs in `protobuf-parse`, it is not tested very well.
31Also `protobuf-parse` does not implement certain rarely used features of `.proto` parser,
32mostly complex message options specified in `.proto` files.
33I never saw anyone using them, but you have been warned.
34
35Note `protoc` command can be obtained from
36[`protoc-bin-vendored`](https://docs.rs/protoc-bin-vendored) crate.
37
38# Example
39
40```rust
41// Use this in build.rs
42protobuf_codegen::Codegen::new()
43    // Use `protoc` parser, optional.
44    .protoc()
45    // Use `protoc-bin-vendored` bundled protoc command, optional.
46    .protoc_path(&protoc_bin_vendored::protoc_bin_path().unwrap())
47    // All inputs and imports from the inputs must reside in `includes` directories.
48    .includes(&["src/protos"])
49    // Inputs must reside in some of include paths.
50    .input("src/protos/apple.proto")
51    .input("src/protos/banana.proto")
52    // Specify output directory relative to Cargo output directory.
53    .cargo_out_dir("protos")
54    .run_from_script();
55```
56
57## How to use `protoc-gen-rust`
58
59If you have to.
60
61(Note `protoc` can be invoked programmatically with
62[protoc crate](https://docs.rs/protoc/%3E=3.0.0-alpha))
63
640) Install protobuf for `protoc` binary.
65
66On OS X [Homebrew](https://github.com/Homebrew/brew) can be used:
67
68```sh
69brew install protobuf
70```
71
72On Ubuntu, `protobuf-compiler` package can be installed:
73
74```sh
75apt-get install protobuf-compiler
76```
77
78Protobuf is needed only for code generation, `rust-protobuf` runtime
79does not use C++ protobuf library.
80
811) Install `protoc-gen-rust` program (which is `protoc` plugin)
82
83It can be installed either from source or with `cargo install protobuf-codegen` command.
84
852) Add `protoc-gen-rust` to $PATH
86
87If you installed it with cargo, it should be
88
89```sh
90PATH="$HOME/.cargo/bin:$PATH"
91```
92
933) Generate .rs files:
94
95```sh
96protoc --rust_out . foo.proto
97```
98
99This will generate .rs files in current directory.
100
101# Customize generate code
102
103Sometimes generated code need to be adjusted, e. g. to have custom derives.
104
105rust-protobuf provides two options to do that:
106* generated `.rs` files contain `@@protoc_insertion_point(...)` markers
107  (similar markers inserts Google's protobuf generator for C++ or Java).
108  Simple script `sed` one-liners can be used to replace these markers with custom annotations.
109* `Codegen::customize_callback` can be used to patch generated code
110  when invoked from `build.rs` script.
111
112# Serde
113
114rust-protobuf since version 3 no longer directly supports serde.
115
116Rust-protobuf 3 fully supports:
117* runtime reflection
118* JSON parsing and printing via
119 [`protobuf-json-mapping`](https://docs.rs/protobuf-json-mapping)
120
121Which covers the most of serde use cases.
122
123If you still need serde, generic customization callback (see above) can be used
124to insert `#[serde(...)]` annotations.
125
126[Example project](https://github.com/stepancheg/rust-protobuf/tree/master/protobuf-examples/customize-serde)
127in the rust-protobuf repository demonstrates how to do it.
128
129<!-- cargo-sync-readme end -->
130