Skip to content

Commit 65f1627

Browse files
doc: try to improve the doc-disparity between tonic-build's lib.rs and the readme
1 parent eaa81dd commit 65f1627

File tree

2 files changed

+35
-67
lines changed

2 files changed

+35
-67
lines changed

tonic-build/README.md

+34-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
Compiles proto files via prost and generates service stubs and proto definitions for use with tonic.
44

5+
# Feature flags
6+
7+
- `cleanup-markdown`: Enables cleaning up documentation from the generated code. Useful
8+
when documentation of the generated code fails `cargo test --doc` for example. The
9+
`prost` feature must be enabled to use this feature.
10+
- `prost`: Enables usage of prost generator (enabled by default).
11+
- `transport`: Enables generation of `connect` method using `tonic::transport::Channel`
12+
(enabled by default).
13+
514
## Features
615

716
Required dependencies
@@ -15,19 +24,20 @@ prost = "<prost-version>"
1524
tonic-build = "<tonic-version>"
1625
```
1726

18-
## Examples
27+
## Getting Started
28+
29+
`tonic-build` works by being included as a [`build.rs` file](https://doc.rust-lang.org/cargo/reference/build-scripts.html) at the root of the binary/library.
1930

20-
### Simple
31+
You can rely on the defaults via
2132

22-
In `build.rs`:
2333
```rust
2434
fn main() -> Result<(), Box<dyn std::error::Error>> {
2535
tonic_build::compile_protos("proto/service.proto")?;
2636
Ok(())
2737
}
2838
```
2939

30-
### Configuration
40+
Or configure the generated code deeper via
3141

3242
```rust
3343
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -40,7 +50,24 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4050
Ok(())
4151
}
4252
```
43-
See [more examples here](https://github.com/hyperium/tonic/tree/master/examples)
53+
54+
For further details how to use the generated client/server, see the [examples here](https://github.com/hyperium/tonic/tree/master/examples) or the Google APIs example below.
55+
56+
57+
## NixOS related hints
58+
59+
On NixOS, it is better to specify the location of `PROTOC` and `PROTOC_INCLUDE` explicitly.
60+
61+
```bash
62+
$ export PROTOBUF_LOCATION=$(nix-env -q protobuf --out-path --no-name)
63+
$ export PROTOC=$PROTOBUF_LOCATION/bin/protoc
64+
$ export PROTOC_INCLUDE=$PROTOBUF_LOCATION/include
65+
$ cargo build
66+
```
67+
68+
The reason being that if `prost_build::compile_protos` fails to generate the resultant package,
69+
the failure is not obvious until the `include!(concat!(env!("OUT_DIR"), "/resultant.rs"));`
70+
fails with `No such file or directory` error.
4471

4572
### Google APIs example
4673
A good way to use Google API is probably using git submodules.
@@ -68,7 +95,8 @@ And a bunch of Google proto files in structure will be like this:
6895
│   └── schema.proto
6996
```
7097

71-
Then we can generate Rust code via this setup in our `build.rs`
98+
Then we can generate Rust code via this setup in our `build.rs`:
99+
72100
```rust
73101
fn main() {
74102
tonic_build::configure()

tonic-build/src/lib.rs

+1-61
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,4 @@
1-
//! `tonic-build` compiles `proto` files via `prost` and generates service stubs
2-
//! and proto definitions for use with `tonic`.
3-
//!
4-
//! # Feature flags
5-
//!
6-
//! - `cleanup-markdown`: Enables cleaning up documentation from the generated code. Useful
7-
//! when documentation of the generated code fails `cargo test --doc` for example. The
8-
//! `prost` feature must be enabled to use this feature.
9-
//! - `prost`: Enables usage of prost generator (enabled by default).
10-
//! - `transport`: Enables generation of `connect` method using `tonic::transport::Channel`
11-
//! (enabled by default).
12-
//!
13-
//! # Required dependencies
14-
//!
15-
//! ```toml
16-
//! [dependencies]
17-
//! tonic = <tonic-version>
18-
//! prost = <prost-version>
19-
//!
20-
//! [build-dependencies]
21-
//! tonic-build = <tonic-version>
22-
//! ```
23-
//!
24-
//! # Examples
25-
//! Simple
26-
//!
27-
//! ```rust,no_run
28-
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
29-
//! tonic_build::compile_protos("proto/service.proto")?;
30-
//! Ok(())
31-
//! }
32-
//! ```
33-
//!
34-
//! Configuration
35-
//!
36-
//! ```rust,no_run
37-
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
38-
//! tonic_build::configure()
39-
//! .build_server(false)
40-
//! .compile_protos(
41-
//! &["proto/helloworld/helloworld.proto"],
42-
//! &["proto/helloworld"],
43-
//! )?;
44-
//! Ok(())
45-
//! }
46-
//!```
47-
//!
48-
//! ## NixOS related hints
49-
//!
50-
//! On NixOS, it is better to specify the location of `PROTOC` and `PROTOC_INCLUDE` explicitly.
51-
//!
52-
//! ```bash
53-
//! $ export PROTOBUF_LOCATION=$(nix-env -q protobuf --out-path --no-name)
54-
//! $ export PROTOC=$PROTOBUF_LOCATION/bin/protoc
55-
//! $ export PROTOC_INCLUDE=$PROTOBUF_LOCATION/include
56-
//! $ cargo build
57-
//! ```
58-
//!
59-
//! The reason being that if `prost_build::compile_protos` fails to generate the resultant package,
60-
//! the failure is not obvious until the `include!(concat!(env!("OUT_DIR"), "/resultant.rs"));`
61-
//! fails with `No such file or directory` error.
1+
#![doc = include_str!("README.md")]
622

633
#![recursion_limit = "256"]
644
#![warn(

0 commit comments

Comments
 (0)