#tonic #codegen #protobuf #codec #tonic-build

tonic-build-protobuf

Codegen module of tonic gRPC implementation with rust-protobuf

1 unstable release

0.1.0 Jun 2, 2024

#6 in #tonic-build

MIT license

18KB
336 lines

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.

Dependencies

~5–14MB
~191K SLoC