Generating interfaces

Rust build scripts let you implement a function that will do some additional preparation for project compilation. In our case, we have the ring.proto file with a protocol definition and we want to convert it to Rust sources using the protoc-rust-grpc crate.

Create the build.rs file in the project and add the following content:

extern crate protoc_rust_grpc;

fn main() {
protoc_rust_grpc::run(protoc_rust_grpc::Args {
out_dir: "src",
includes: &[],
input: &["ring.proto"],
rust_protobuf: true,
..Default::default()
}).expect("protoc-rust-grpc");
}

Build scripts use the main function as an entry point, in which you can implement any activities you want. We used the run function of the protoc-rust-grpc crate with Args—we set the output directory in the out_dir field, set the ring.proto file as input declaration with the input field, activate the rust_protobuf Boolean flag to generate sources for the rust-protobuf crate (you don't need it if you are already using the protobuf crate and generating types with it), then set the includes field to an empty array.

Then, when you run cargo build, it will produce two modules in the src folder: ring.rs and ring_grpc.rs. I don't put its sources here, because generated files are large, but we will use it to create a wrapper for a gRPC client, as we did in the previous example.