Skip to content

Commit 2737de3

Browse files
docs(tonic-build): remove doc-difference between lib.rs and Readme (#2308)
* doc: try to improve the doc-disparity between `tonic-build`'s `lib.rs` and the readme * Fix indentation * fix the doctests * fix a typo and retes an assumption * Change `compile_fail` to `ignore` Co-authored-by: tottoto <[email protected]> --------- Co-authored-by: tottoto <[email protected]>
1 parent 91be74d commit 2737de3

File tree

2 files changed

+45
-77
lines changed

2 files changed

+45
-77
lines changed

tonic-build/README.md

Lines changed: 44 additions & 15 deletions
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.
8+
Useful when documentation of the generated code fails `cargo test --doc` for example.
9+
The `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,21 +24,22 @@ prost = "<prost-version>"
1524
tonic-build = "<tonic-version>"
1625
```
1726

18-
## Examples
27+
## Getting Started
1928

20-
### Simple
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.
2130

22-
In `build.rs`:
23-
```rust
31+
You can rely on the defaults via
32+
33+
```rust,no_run
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

32-
```rust
42+
```rust,no_run
3343
fn main() -> Result<(), Box<dyn std::error::Error>> {
3444
tonic_build::configure()
3545
.build_server(false)
@@ -40,20 +50,37 @@ 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.
4774

4875
So suppose in our `proto` folder we do:
49-
```
76+
```bash
5077
git submodule add https://github.com/googleapis/googleapis
5178

5279
git submodule update --remote
5380
```
5481

5582
And a bunch of Google proto files in structure will be like this:
56-
```
83+
```raw
5784
├── googleapis
5885
│   └── google
5986
│   ├── api
@@ -68,21 +95,23 @@ 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`
72-
```rust
73-
fn main() {
98+
Then we can generate Rust code via this setup in our `build.rs`:
99+
100+
```rust,no_run
101+
fn main() -> Result<(), Box<dyn std::error::Error>> {
74102
tonic_build::configure()
75103
.build_server(false)
76104
//.out_dir("src/google") // you can change the generated code's location
77105
.compile_protos(
78106
&["proto/googleapis/google/pubsub/v1/pubsub.proto"],
79107
&["proto/googleapis"], // specify the root location to search proto dependencies
80-
).unwrap();
108+
)?;
109+
Ok(())
81110
}
82111
```
83112

84113
Then you can reference the generated Rust like this this in your code:
85-
```rust
114+
```rust,ignore
86115
pub mod api {
87116
tonic::include_proto!("google.pubsub.v1");
88117
}
@@ -92,7 +121,7 @@ use api::{publisher_client::PublisherClient, ListTopicsRequest};
92121
Or if you want to save the generated code in your own code base,
93122
you can uncomment the line `.out_dir(...)` above, and in your lib file
94123
config a mod like this:
95-
```rust
124+
```rust,ignore
96125
pub mod google {
97126
#[path = ""]
98127
pub mod pubsub {

tonic-build/src/lib.rs

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +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.
62-
1+
#![doc = include_str!("../README.md")]
632
#![recursion_limit = "256"]
643
#![doc(
654
html_logo_url = "https://raw.githubusercontent.com/tokio-rs/website/master/public/img/icons/tonic.svg"

0 commit comments

Comments
 (0)