tonic-buf-build

Crates.iotonic-buf-build
lib.rstonic-buf-build
version0.3.0
sourcesrc
created_at2023-08-16 18:06:43.355769
updated_at2024-10-27 10:31:04.880897
descriptionA build helper that integrates buf.build to tonic-build.
homepage
repositoryhttps://github.com/Valensas/tonic-buf-build
max_upload_size
id946216
size16,203
Moray Baruh (moray95)

documentation

README

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(())
}
Commit count: 11

cargo fmt