Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Rust as first-class citizen for gRPC ecosystem
This crate provides 4 macros that will handle all proto-related work, so you don't need to touch .proto files at all.
Motivation
- I hate to do conversion after conversion for conversion
- I love to see Rust only as first-class citizen for all my stuff
- I hate bloat, so no protoc (shoutout to PewDiePie debloat trend)
- I don't want to touch .proto files at all
Usage
The #[proto_rpc] macro will convert your Rust native trait to tonic and optionally emit .proto file:
Yep, all types here are just Rust types. We can then implement the server like this:
This is possible because of this trait, that handles all conversions automagically:
We can derive it (or manually implement) for most types with #[proto_message] macro:
;
But that's not all — #[proto_message] and #[proto_rpc] will also create .proto definitions for non-Rust clients.
Build All .proto Files from Dependencies at once
Pure Rust Black Magic
This crate provides a powerful feature to collect and build .proto files from ALL dependencies that use proto_rs in a single place. This is incredibly useful for building a centralized proto schema from a multi-crate workspace.
Usage
In your build.rs or main.rs (or any crate that has other proto_rs dependent crates):
use ProtoSchema;
This will automatically collect and build all .proto files from all crates in your dependency tree that use proto_rs macros!
Examples
You can see more in examples:
- proto_gen_example - simple service with streaming (generated .proto saved here: protos/gen_proto)
- prosto_proto - showcase of type possibilities (generated .proto saved here: protos/showcase_proto)
- tests/proto_build_test - example of how you can build .proto files only on demand
.proto Auto-Emission Control
Controls auto-emission of .proto files by macros:
"emit-proto-files"- cargo feature"PROTO_EMIT_FILE"- env var
.proto Auto-Emission Behavior
| Feature | Env Var | Result |
|---|---|---|
| none | not set | ❌ No emission |
| none | true | ✅ Emit files |
| none | false | ❌ No emission |
| emit-proto-files | not set | ✅ Emit files |
| emit-proto-files | true | ✅ Emit files |
| emit-proto-files | false | ❌ No emission (override) |
| build-schemas | (any) | ✅ Emit const |
proto_dump for hand-written types
This crate also provides an auxiliary macro #[proto_dump(proto_path ="protos/proto_dump.proto")] that outputs a .proto file. This is helpful for hand-written types.
Generated proto:
syntax = "proto3";
package proto_dump;
message Lamports {
uint64 amount = 1;
}
Inject Proto Imports
It's not mandatory to use this macro at all, macros above derive and inject imports from attributes automaticly
But in case you need it, to add custom imports to your generated .proto files use the inject_proto_import! macro:
inject_proto_import!;
This will inject the specified import statements into the target .proto file
Advanced Features
Macros support all prost types, imports, skipping with default and custom functions, custom conversions, support for native Rust enums (like Status below) and prost enumerations (TestEnum in this example, see more in prosto_proto).
Struct with Advanced Attributes
Generated proto:
message Attr {
repeated string id_vec = 1;
optional string id_opt = 2;
Status status = 3;
optional Status status_opt = 4;
repeated Status status_vec = 5;
google.protobuf.Timestamp timestamp = 6;
repeated google.protobuf.Timestamp timestamp_vec = 7;
optional google.protobuf.Timestamp timestamp_opt = 8;
google.protobuf.TestEnum test_enum = 9;
optional google.protobuf.TestEnum test_enum_opt = 10;
repeated google.protobuf.TestEnum test_enum_vec = 11;
int64 updated_at = 12;
}
Complex Enums
Generated proto:
message VeryComplexProto {
oneof value {
VeryComplexProtoFirst first = 1;
Address second = 2;
VeryComplexProtoThird third = 3;
VeryComplexProtoRepeated repeated = 4;
VeryComplexProtoOption option = 5;
VeryComplexProtoAttr attr = 6;
}
}
message VeryComplexProtoFirst {}
message VeryComplexProtoThird {
uint64 id = 1;
Address address = 2;
}
message VeryComplexProtoRepeated {
repeated uint64 id = 1;
repeated Address address = 2;
}
message VeryComplexProtoOption {
optional uint64 id = 1;
optional Option address = 2;
}
message VeryComplexProtoAttr {
repeated string id_vec = 1;
optional string id_opt = 2;
Status status = 3;
optional Status status_opt = 4;
repeated Status status_vec = 5;
google.protobuf.Timestamp timestamp = 6;
repeated google.protobuf.Timestamp timestamp_vec = 7;
optional google.protobuf.Timestamp timestamp_opt = 8;
google.protobuf.TestEnum test_enum = 9;
optional google.protobuf.TestEnum test_enum_opt = 10;
repeated google.protobuf.TestEnum test_enum_vec = 11;
}
Simple Rust Enum
Generated proto:
enum Status {
PENDING = 0;
ACTIVE = 1;
INACTIVE = 2;
COMPLETED = 3;
}
Dependencies
Crate pulled dependencies:
01:04:53 ➜ cargo tree
proto_rs v0.2.0
├── prost v0.14.1
│ ├── bytes v1.10.1
│ └── prost-derive v0.14.1 (proc-macro)
│ ├── anyhow v1.0.100
│ ├── itertools v0.14.0
│ │ └── either v1.15.0
│ ├── proc-macro2 v1.0.101
│ │ └── unicode-ident v1.0.19
│ ├── quote v1.0.41
│ │ └── proc-macro2 v1.0.101 (*)
│ └── syn v2.0.106
│ ├── proc-macro2 v1.0.101 (*)
│ ├── quote v1.0.41 (*)
│ └── unicode-ident v1.0.19
└── prosto_derive v0.2.0 (proc-macro)
├── proc-macro2 v1.0.101 (*)
├── quote v1.0.41 (*)
└── syn v2.0.106 (*)