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