pub fn compile_protos(
    protos: &[impl AsRef<Path>],
    includes: &[impl AsRef<Path>]
) -> Result<()>
Expand description

Compile .proto files into Rust files during a Cargo build.

The generated .rs files are written to the Cargo OUT_DIR directory, suitable for use with the include! macro. See the Cargo build.rs code generation example for more info.

This function should be called in a project’s build.rs.

Arguments

protos - Paths to .proto files to compile. Any transitively imported .proto files are automatically be included.

includes - Paths to directories in which to search for imports. Directories are searched in order. The .proto files passed in protos must be found in one of the provided include directories.

Errors

This function can fail for a number of reasons:

  • Failure to locate or download protoc.
  • Failure to parse the .protos.
  • Failure to locate an imported .proto.
  • Failure to compile a .proto without a package specifier.

It’s expected that this function call be unwraped in a build.rs; there is typically no reason to gracefully recover from errors during a build.

Example build.rs

fn main() -> Result<()> {
  prost_build::compile_protos(&["src/frontend.proto", "src/backend.proto"], &["src"])?;
  Ok(())
}