#tonic #codec #protobuf #codegen

tonic-codec-protobuf

A tonic::Codec that implements application/grpc+proto via the rust-protobuf

1 unstable release

0.1.0 Jun 2, 2024

#109 in #codegen

MIT license

10KB
135 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

~4–11MB
~105K SLoC