#rpc #grpc #async #codegen #protobuf

build tonic-build

Codegen module of tonic gRPC implementation

16 unstable releases (4 breaking)

0.4.2 Apr 13, 2021
0.4.1 Mar 17, 2021
0.4.0 Jan 15, 2021
0.3.1 Aug 20, 2020
0.1.0-alpha.6 Nov 10, 2019

#18 in Network programming

Download history 31771/week @ 2021-01-15 33894/week @ 2021-01-22 39052/week @ 2021-01-29 37408/week @ 2021-02-05 37979/week @ 2021-02-12 36370/week @ 2021-02-19 40183/week @ 2021-02-26 36948/week @ 2021-03-05 37212/week @ 2021-03-12 40547/week @ 2021-03-19 43859/week @ 2021-03-26 44631/week @ 2021-04-02 49531/week @ 2021-04-09 47797/week @ 2021-04-16 48283/week @ 2021-04-23 39464/week @ 2021-04-30

165,179 downloads per month
Used in 108 crates (84 directly)

MIT license

49KB
1K SLoC

tonic-build

Compiles proto files via prost and generates service stubs and proto definitiones for use with tonic.

Features

  • rustfmt: This feature enables the use of rustfmt to format the output code this makes the code readable and the error messages nice. This requires that rustfmt is installed. This is enabled by default.

Required dependencies

[dependencies]
tonic = <tonic-version>
prost = <prost-version>

[build-dependencies]
tonic-build = <tonic-version>

Examples

Simple

In build.rs:

fn main() -> Result<(), Box<dyn std::error::Error>> {
    tonic_build::compile_protos("proto/service.proto")?;
    Ok(())
}

Configuration

fn main() -> Result<(), Box<dyn std::error::Error>> {
   tonic_build::configure()
        .build_server(false)
        .compile(
            &["proto/helloworld/helloworld.proto"],
            &["proto/helloworld"],
        )?;
   Ok(())
}

See more examples here

Google APIs example

A good way to use Google API is probably using git submodules.

So suppose in our proto folder we do:

git submodule add https://github.com/googleapis/googleapis

And a bunch of Google proto files in structure will be like this:

├── googleapis
│   └── google
│       ├── api
│       │   ├── annotations.proto
│       │   ├── client.proto
│       │   ├── field_behavior.proto
│       │   ├── http.proto
│       │   └── resource.proto
│       └── pubsub
│           └── v1
│               ├── pubsub.proto
│               └── schema.proto

Then we can generate Rust code via this setup in our build.rs

fn main() {
    tonic_build::configure()
        .build_server(false)
        //.out_dir("src/google")  // you can change the generated code's location
        .compile(
            &["proto/googleapis/google/pubsub/v1/pubsub.proto"],
            &["proto/googleapis"], // specify the root location to search proto dependencies
        ).unwrap();
}

Then you can reference the generated Rust like this this in your code:

pub mod api {
    tonic::include_proto!("google.pubsub.v1");
}
use api::{publisher_client::PublisherClient, ListTopicsRequest};

Or if you want to save the generated code in your own code base, you can uncomment the line .out_dir(...) above, and in your lib file config a mod like this:

pub mod google {
    #[path = ""]
    pub mod pubsub {
        #[path = "google.pubsub.v1.rs"]
        pub mod v1;
    }
}

See the example here

Dependencies

~0.3–3.5MB
~28K SLoC