tonic-build-protobuf

Crates.iotonic-build-protobuf
lib.rstonic-build-protobuf
version0.1.0
sourcesrc
created_at2024-06-02 15:25:36.090883
updated_at2024-06-02 15:25:36.090883
descriptionCodegen module of `tonic` gRPC implementation with rust-protobuf.
homepagehttps://github.com/overvenus/tonic-protobuf
repositoryhttps://github.com/overvenus/tonic-protobuf
max_upload_size
id1259279
size18,670
Neil Shen (overvenus)

documentation

https://docs.rs/tonic-build-protobuf

README

tonic-build-protobuf / tonic-codec-protobuf

Compiles proto files via rust-protobuf and generates service stubs for use with tonic.

Features

Required dependencies

[dependencies]
tonic = "<tonic-version>"
protobuf = "<protobuf-version>"
tonic-codec-protobuf = "<tonic-codec-protobuf-version>"

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

Examples

In build.rs:

fn main() {
    // Project layout:
    // .
    // ├── Cargo.toml
    // ├── build.rs
    // ├── include
    // │   └── rustproto.proto
    // ├── proto
    // │   └── debugpb.proto
    // └── src
    //     └── lib.rs
    tonic_build_protobuf::Builder::new()
        .out_dir(format!(
            "{}/protos",
            std::env::var("OUT_DIR").expect("No OUT_DIR defined")
        ))
        .proto_path("crate")
        .file_name(|pkg, svc| format!("{pkg}_{svc}_tonic"))
        .codec_path("::tonic_codec_protobuf::ProtobufCodecV3")
        .compile(&["proto/debugpb.proto"], &["proto", "include"]);
}

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

mod generated {
    include!(concat!(env!("OUT_DIR"), "debugpb_debug_tonic.rs"));
}

pub use generated::*;

See examples here

License

This project is licensed under the MIT license.

Commit count: 5

cargo fmt