Skip to main content

Crate proto_build_kit

Crate proto_build_kit 

Source
Expand description

Generic build-helper primitives for proto-source services.

The four primitives this crate exposes are the ones every .proto-publishing or .proto-consuming Rust crate ends up reimplementing:

  1. Stage embedded proto bytes onto a tempdir at protoc-relative paths so import "myproto/v1/foo.proto"; resolves at build time without the consumer vendoring the file (Stager).
  2. Compile .proto files via protox (pure Rust, no protoc subprocess) and return both the prost-reflect descriptor pool (preserves custom-option VALUES) and the FDS bytes for downstream codegen (compile_protos).
  3. Read MethodOptions extension values from the descriptor pool — encoded FDS drops them, the pool keeps them (extract_method_string_extension).
  4. Drive tonic-prost-build with type_attribute(...) injection from an annotation-FQN map (tonic_prost_build_with_attrs, gated on the tonic feature).

The crate is schema-agnostic — it doesn’t know about any specific proto package or annotation. Consumers pair it with a tiny sibling crate that ships their .proto bytes via include_bytes!:

// some-protos/src/lib.rs (your bytes crate):
const FOO: &[u8] = include_bytes!("../proto/my/v1/foo.proto");
pub fn files() -> impl Iterator<Item = (&'static str, &'static [u8])> {
    [("my/v1/foo.proto", FOO)].into_iter()
}

Consumer build.rs then composes any number of *-protos crates:

use proto_build_kit::Stager;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let staged = Stager::new()
        .with(some_protos::files())
        .with(other_protos::files())
        .stage()?;

    // Drive whatever codegen you want (connectrpc-build, tonic, ...)
    // against `staged.path()` on the include path.
    Ok(())
}

Hold the returned tempfile::TempDir until codegen completes — drop deletes the staged files.

Structs§

CompileOutput
Result of compile_protos.
Stager
Accumulates (relative_path, bytes) pairs and writes them to a fresh tempdir laid out as <tmp>/<relative_path>.

Enums§

Error
Errors surfaced by the proto-build-kit primitives.

Functions§

compile_protos
Compile a set of .proto files (and all their transitive imports) via protox.
extract_method_string_extension
Walk every method declared in pool, look for the MethodOptions-level extension named extension_fqn, and return a map keyed by response-message FQN (e.g. my.v1.Resource) with the extension’s string value.
tonic_prost_build_with_attrs
Run tonic-prost-build codegen on fds_bytes, applying every type_attribute(<fqn>, <attribute>) from type_attributes before invocation.