Skip to content

Commit 78fc2ba

Browse files
authored
Merge pull request #30 from EmbarkStudios/bwe/update-readme
Improvements and fixes to the readme
2 parents 8e79c48 + 54af40c commit 78fc2ba

File tree

2 files changed

+96
-29
lines changed

2 files changed

+96
-29
lines changed

README.md

Lines changed: 89 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,39 @@
66

77
<div align="center">
88

9-
# `󱌣 proto-gen`
9+
# `💠 proto-gen`
1010

11-
**Protobuf to `Rust` code generation using tonic-build**
11+
**[Protobuf](https://protobuf.dev/) to [Rust](https://www.rust-lang.org/) code generation using [tonic-build](https://docs.rs/tonic-build/latest/tonic_build/)**
1212

1313
[![Embark](https://img.shields.io/badge/embark-open%20source-blueviolet.svg)](https://embark.dev)
1414
[![Embark](https://img.shields.io/badge/discord-ark-%237289da.svg?logo=discord)](https://discord.gg/dAuKfZS)
15+
[![Crates.io](https://img.shields.io/crates/v/proto-gen.svg)](https://crates.io/crates/proto-gen)
1516
[![dependency status](https://deps.rs/repo/github/EmbarkStudios/proto-gen/status.svg)](https://deps.rs/repo/github/EmbarkStudios/proto-gen)
1617
[![Build status](https://github.com/EmbarkStudios/proto-gen/workflows/CI/badge.svg)](https://github.com/EmbarkStudios/proto-gen/actions)
1718
</div>
1819

1920
## What
20-
The repo contains a a cli that uses tonic-build to generate rust-code from protobuf-files.
21+
The repo contains a cli tool that uses [tonic-build](https://docs.rs/tonic-build/latest/tonic_build/) to generate rust-code from protobuf-files.
2122
[tonic-build](https://docs.rs/tonic-build/latest/tonic_build/) already does this, the cli is a front-end to
2223
that with some added code to make sure that the generated files are placed in a valid path, and takes care of the
2324
module structuring.
2425

2526
## Why
2627
[prost-build](https://docs.rs/prost-build/latest/prost_build/) used to ship with `cmake` which we would like to avoid.
2728
`cmake` was used to build `protoc` which was then used for the proto-to-rust codegen.
28-
The final decision from the prost maintainers side is that the user should provide their own protoc and check in the code
29-
instead of building it in a `build.rs`, to make that process simpler, this cli was created.
29+
The final decision from the prost maintainers side is that the user should provide their own `protoc` and check in the code
30+
instead of building it in a `build.rs`, to make that process simpler, therefore this cli was created.
3031

3132
## How
32-
Uses [a local version of protoc on path](https://grpc.io/docs/protoc-installation/) to generate rust code form a
33-
workspace specification passed to the CLI. The CLI can also be used to validate generated Rust code against source
34-
protocol files, for example, in CI.
33+
Uses [a local version of protoc on path](https://grpc.io/docs/protoc-installation/) to generate rust code from a
34+
workspace specification passed to the CLI. The CLI can also be used to validate the generated Rust code against source
35+
protobuf files, for example, in CI.
3536

3637
## Considerations
37-
There are quite a few problems with this tool, for now it remains the best alternative for our Protobuf pipeline,
38+
There are quite a few problems with this tool, for now it remains the best alternative for our protobuf pipeline,
3839
some things to take into account follows.
3940

40-
### Requires manual installations on the user's machine
41+
### Requires manual installation on the user's machine
4142
Running this tool requires at minimum `protoc` to be installed on the user's machine,
4243
many protocol buffers found in the wild have dependencies in the form of other `.proto` files. Which also
4344
have to be sourced.
@@ -61,38 +62,104 @@ for this tool, although that may be a bit philosophical since the generated code
6162
### Doc comments are executed
6263
Tonic converts protobuf comments to doc comments, if these include code examples, cargo will attempt to execute them
6364
when running `cargo test` (`doc-test` stage).
64-
This behaviour is different form using `tonic-build`, since those doc-comments are generated where cargo doesn't try
65+
This behaviour is different from using [tonic-build](https://docs.rs/tonic-build/latest/tonic_build/), since those doc-comments are generated where cargo doesn't try
6566
to run them.
6667
Running them is almost always guaranteed to fail in the best case (because they will be non-rust code or not code at all),
6768
and be a lurking security vulnerability in the worst case where someone have inserted malicious Rust-code in a doc-comment.
6869

6970
Ideally it'd be easier to disable running doc-test on a per-module basis. Placing generated protos in a separate lib
7071
which doesn't run doc-comments is a safer choice, although doc-tests being opt-in would be significantly safer.
7172

72-
### Usage
73-
Generate types from a `.proto` into an project.
73+
## Usage
7474

75-
#### Example in this project
76-
```bash
77-
cargo r -r -p proto-gen -- generate -d examples/example-project/proto -f examples/example-project/proto/my-proto.proto -o examples/example-project/src/proto_types
75+
### Install
76+
To install `proto-gen` from source do the following. This assumed you have installed a recent [rust toolchain](https://www.rust-lang.org/tools/install).
77+
```
78+
cargo install --locked proto-gen
79+
```
80+
81+
### Command-line options
82+
83+
Usage:
84+
85+
```
86+
proto-gen [OPTIONS] <COMMAND>
7887
```
88+
89+
#### Commands:
90+
91+
The top-level commands are:
92+
93+
- `validate` Generate new Rust code for proto files, checking current files for differences. Returns error code 1 on any found differences.
94+
- `generate` Generate new Rust code for proto files, overwriting old files if present.
95+
- `help` Print this message or the help of the given subcommand(s).
96+
97+
`validate` & `generate` share the following sub-options:
98+
99+
- `-d`, `--proto-dirs` `<PROTO_DIRS>` Directories containing proto files to source (Ex. Dependencies), needs to include any directory containing files to be included in generation.
100+
- `-f`, `--proto-files` `<PROTO_FILES>` The files to be included in generation.
101+
- `-t`, `--tmp-dir` `<TMP_DIR>` Temporary working directory, if left blank, `tempfile` is used to create a temporary directory.
102+
- `-o`, `--output-dir` `<OUTPUT_DIR>` Where to place output files. Will get cleaned up (all contents deleted). A module file will be placed in the parent of this directory.
103+
- `-h`, `--help` Print help.
104+
105+
#### Options:
106+
The top-level options are:
107+
108+
- `-s`, `--build-server` Whether to build server code.
109+
- `-c`, `--build-client` Whether to build client code.
110+
- `--generate-transport` Whether to generate the `::connect` and similar functions for tonic.
111+
- `-d`, `--disable-comments` `<DISABLE_COMMENTS>` Disable comments based on proto path. Passing `'.'` disables all comments.
112+
- `-b`, `--btree-map` `<BTREE_MAPS>` Output maps as `BTreeMap` instead of `HashMap`. Passing `'.'` makes all maps `BTreeMap`.
113+
- `--type-attribute` `<TYPE_ATTRIBUTES>` Type attributes to add.
114+
- `--enum-attribute` `<ENUM_ATTRIBUTES>` Enum attributes to add.
115+
- `--client-attribute` `<CLIENT_ATTRIBUTES>` Client mod attributes to add.
116+
- `--server-attribute` `<SERVER_ATTRIBUTES>` Server mod attributes to add.
117+
- `-f`, `--format` Use `rustfmt` on the code after generation, `rustfmt` needs to be on the path.
118+
- `-p`, `--prepend-header` Prepend header indicating tool version in generated source files.
119+
- `--prepend-header-file` `<PREPEND_HEADER_FILE>` Prepend header file in generated source files.
120+
- `--toplevel-attribute` `<TOPLEVEL_ATTRIBUTE>` Toplevel mod attribute to add.
121+
- `-h`, `--help` Print help.
122+
- `-V`, `--version` Print version.
123+
124+
### Examples in this project
79125
This will generate Rust code from the proto specified in `examples/example-project/proto/my-proto.proto` and place it
80-
in `examples/example-project/src/proto_types`.
126+
in `examples/example-project/src/proto_types`.
81127

82128
```bash
83-
cargo r -r -p proto-gen -- validate -d examples/example-project/proto -f examples/example-project/proto/my-proto.proto -o examples/example-project/src/proto_types
129+
proto-gen generate \
130+
-d examples/example-project/proto \
131+
-f examples/example-project/proto/my-proto.proto \
132+
-o examples/example-project/src/proto_types
84133
```
134+
85135
This will also generate Rust code (to a temporary directory) and the run a diff against the code contained in `examples/example-project/src/proto_types`.
86136
If it finds any diffs it will exit with error code 1 and a message.
87137

88-
If we want to use includes, the directory to include needs to be specified.
138+
```bash
139+
proto-gen validate \
140+
-d examples/example-project/proto \
141+
-f examples/example-project/proto/my-proto.proto \
142+
-o examples/example-project/src/proto_types
143+
```
144+
145+
If we want to use includes, the directory to include needs to be specified. Here we're passing `-d` twice, once to include the dependency protos, and one to include the protos we want to generate.
89146

90147
```bash
91-
cargo r -r -p proto-gen -- generate -d examples/example-project/dep_protos -d examples/example-project/proto_with_deps -f examples/example-project/proto_with_deps/my-proto.proto -o examples/example-project/src/proto_types
148+
proto-gen generate \
149+
-d examples/example-project/dep_protos \
150+
-d examples/example-project/proto_with_deps \
151+
-f examples/example-project/proto_with_deps/my-proto.proto \
152+
-o examples/example-project/src/proto_types
92153
```
93-
Here we're passing -d twice, once to include the dependency protos, and one to include the protos we want to generate.
154+
155+
The same applies for validate.
156+
94157
```bash
95-
cargo r -r -p proto-gen -- validate -d examples/example-project/dep_protos -d examples/example-project/proto_with_deps -f examples/example-project/proto_with_deps/my-proto.proto -o examples/example-project/src/proto_types
158+
proto-gen validate \
159+
-d examples/example-project/dep_protos \
160+
-d examples/example-project/proto_with_deps \
161+
-f examples/example-project/proto_with_deps/my-proto.proto \
162+
-o examples/example-project/src/proto_types
96163
```
97164

98165
## Contributing

proto-gen/src/main.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ struct Opts {
2727
#[clap(flatten)]
2828
tonic: TonicOpts,
2929

30-
/// Use `rustfmt` on the code after generation, `rustfmt` needs to be on the path
30+
/// Use `rustfmt` on the code after generation, `rustfmt` needs to be on the path.
3131
#[clap(short, long)]
3232
format: bool,
3333

34-
/// Prepend header indicating tool version in generated source files
34+
/// Prepend header indicating tool version in generated source files.
3535
#[clap(short, long, default_value_t = false)]
3636
prepend_header: bool,
3737

38-
/// Prepend header file in generated source files
38+
/// Prepend header file in generated source files.
3939
#[clap(long)]
4040
prepend_header_file: Option<PathBuf>,
4141

@@ -46,11 +46,11 @@ struct Opts {
4646

4747
#[derive(Args, Debug, Clone)]
4848
struct TonicOpts {
49-
/// Whether to build server code
49+
/// Whether to build server code.
5050
#[clap(short = 's', long)]
5151
build_server: bool,
5252

53-
/// Whether to build client code
53+
/// Whether to build client code.
5454
#[clap(short = 'c', long)]
5555
build_client: bool,
5656

@@ -92,7 +92,7 @@ enum Routine {
9292
workspace: WorkspaceOpts,
9393
},
9494

95-
/// Generate new Rust code for proto files, overwriting old files if present
95+
/// Generate new Rust code for proto files, overwriting old files if present.
9696
Generate {
9797
#[clap(flatten)]
9898
workspace: WorkspaceOpts,
@@ -115,7 +115,7 @@ struct WorkspaceOpts {
115115
#[clap(short, long)]
116116
tmp_dir: Option<PathBuf>,
117117

118-
/// Where to place output files. Will get cleaned up (all contents deleted)
118+
/// Where to place output files. Will get cleaned up (all contents deleted).
119119
/// A module file will be placed in the parent of this directory.
120120
#[clap(short, long)]
121121
output_dir: PathBuf,

0 commit comments

Comments
 (0)