Skip to content

Latest commit

 

History

17 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust Proto Rules

Rust protobuf definitions for the Proto plugin, for the Please build system.

Basic Usage

First, add the proto plugin and this plugin to your project:

# plugins/BUILD
plugin_repo(
    name = "proto",
    plugin = "proto-rules",
    revision = "<Some git tag, commit, or other reference>",
)

plugin_repo(
    name = "rust_proto",
    owner = "becomeliminal",
    plugin = "rust-proto-rules",
    revision = "<Some git tag, commit, or other reference>",
)

# Generated code is compiled by these rules, which this plugin pins by tag
plugin_repo(
    name = "rust",
    owner = "becomeliminal",
    plugin = "rust-rules",
    revision = "<Some git tag, commit, or other reference>",
)

Then add rust to the list of language definitions for the proto plugin:

[Plugin "proto"]
LanguageDef = ///rust_proto//build_defs:rust_proto

You'll then need the protobuf sdk, the gRPC sdk if you want grpc_library(), and the two protoc plugins that generate the code. Declare them with rust_repo(), or let rust-rules resolve them for you:

plz run ///rust//tools/please_rust -- lock --add prost@0.13 --add tonic@0.12 --add protoc-gen-prost@0.4 --add protoc-gen-tonic@0.4

The protoc plugins are built from source like any other crate, so nothing is downloaded as a prebuilt binary.

Now add the rust proto plugin to your .plzconfig:

[Plugin "rust_proto"]
Target = //plugins:rust_proto
ProtoPlugin = ///third_party/rust/protoc_gen_prost//:protoc_gen_prost_bin
ProtoDep = //third_party/rust:prost
GrpcPlugin = ///third_party/rust/protoc_gen_tonic//:protoc_gen_tonic_bin
GrpcDep = //third_party/rust:tonic

The Rust plugin and Rust Proto plugin expects a specific package structure:

path/to/your/package/
├── BUILD
└── src
    ├── lib.rs || main.rs
    └── proto
        ├── BUILD
        └── service.proto

You can then use proto_library() or grpc_library() to generate Rust code for your .proto files:

grpc_library(
    name = "proto",
    srcs = ["service.proto"],
    languages = {
        "rust": rust_grpc_language(),
    },
    visibility = ["PUBLIC"],
    deps = [
        "//third_party/rust:prost",
        "//third_party/rust:tonic",
    ],
)

And you can use the protoc generated service.rs and service.tonic.rs in your Rust code:

rust_binary(
    name = "service_test",
    edition = "2021",
    root = "src/main.rs",
    deps = [
        "//path/to/your/package/src/proto:_proto#rust",
        "//third_party/rust:prost",
        "//third_party/rust:tonic",
    ],
)

See the Proto plugin for more information on these rules.

Configuration

This plugin can be configured via the plugins section as follows:

[Plugin "rust_proto"]
SomeConfig = some-value
Option What it is
ProtoPlugin protoc-gen-prost binary used for message codegen
GrpcPlugin protoc-gen-tonic binary used for service codegen
ProtoDep prost runtime crate the generated messages link against
GrpcDep tonic runtime crate the generated services link against

About

Rust language implementation for Protobuf and Please

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages