38 releases

0.12.3 Sep 26, 2024
0.12.1 Jul 17, 2024
0.11.0 Feb 8, 2024
0.10.2 Sep 28, 2023
0.1.0-alpha.6 Nov 10, 2019

#37 in Network programming

Download history 635310/week @ 2024-10-12 663961/week @ 2024-10-19 657537/week @ 2024-10-26 801258/week @ 2024-11-02 807926/week @ 2024-11-09 786112/week @ 2024-11-16 587130/week @ 2024-11-23 678668/week @ 2024-11-30 891272/week @ 2024-12-07 775301/week @ 2024-12-14 235685/week @ 2024-12-21 357432/week @ 2024-12-28 779549/week @ 2025-01-04 822326/week @ 2025-01-11 662581/week @ 2025-01-18 611535/week @ 2025-01-25

2,984,808 downloads per month
Used in 1,126 crates (585 directly)

MIT license

99KB
2K SLoC

tonic-build

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

Features

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_protos(
            &["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

git submodule update --remote

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_protos(
            &["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.5–11MB
~128K SLoC

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy