#tonic-build #buf #protobuf #integrate #yaml #generate #helper

tonic-buf-build

A build helper that integrates buf.build to tonic-build

6 releases

0.3.0 Oct 27, 2024
0.2.1 Jun 7, 2024
0.2.0 Feb 16, 2024
0.1.2 Sep 21, 2023
0.1.1 Aug 16, 2023

#942 in Encoding

Download history 34/week @ 2024-07-31 79/week @ 2024-08-07 141/week @ 2024-08-14 83/week @ 2024-08-21 74/week @ 2024-08-28 86/week @ 2024-09-04 335/week @ 2024-09-11 328/week @ 2024-09-18 347/week @ 2024-09-25 275/week @ 2024-10-02 195/week @ 2024-10-09 278/week @ 2024-10-16 416/week @ 2024-10-23 357/week @ 2024-10-30 346/week @ 2024-11-06 348/week @ 2024-11-13

1,522 downloads per month
Used in 3 crates (2 directly)

MIT license

13KB
216 lines

tonic-buf-build

A build helper that allows you to integrate buf.build with tonic-build. Using buf.build and tonic, you can easily manage third party dependencies for proto files and generate code for your proto files in Rust. Works with both buf.yaml and buf.work.yaml.

Usage

Add the following to your Cargo.toml:

tonic-buf-build = "*"
tonic-build = "*"

Then, in your build.rs:

fn main() -> Result<(), tonic_buf_build::error::TonicBufBuildError> {
   tonic_buf_build::compile_from_buf(tonic_build::configure(), None)?;
   Ok(())
}

To use buf workspaces, simply call tonic_buf_build::compile_from_buf_workspace instead.

For complete and working examples, take a look at the examples folder.

When the buf files are not located in the current directory, you can configure the absolute path to the directory, containing either buf.yaml or buf.work.yaml, and call the corresponding tonic_buf_build::compile_from_buf_with_config or tonic_buf_build::compile_from_buf_workspace_with_config.

Consider the following build.rs where the buf workspace directory is located one level above the crate (a usual case for multilanguage clients with common protos)

use std::env;
use std::path::PathBuf;

fn main() -> Result<(), tonic_buf_build::error::TonicBufBuildError> {
    let mut path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
    path.pop();
    tonic_buf_build::compile_from_buf_workspace_with_config(
        tonic_build::configure(),
        None,
        tonic_buf_build::TonicBufConfig{buf_dir: Some(path)},
    )?;
    Ok(())
}

Dependencies

~9–20MB
~292K SLoC