Crate protox

source ·
Expand description

A rust implementation of the protobuf compiler.

For convenient compilation of protobuf source files in a single function, see compile(). For more options see Compiler.

§Examples

Usage with prost-build:

let file_descriptors = protox::compile(["root.proto"], ["."]).unwrap();
prost_build::compile_fds(file_descriptors).unwrap();

Usage with tonic-build:

use protox::prost::Message;

let file_descriptors = protox::compile(["root.proto"], ["."]).unwrap();

let file_descriptor_path = PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR not set"))
    .join("file_descriptor_set.bin");
fs::write(&file_descriptor_path, file_descriptors.encode_to_vec()).unwrap();

tonic_build::configure()
    .build_server(true)
    .file_descriptor_set_path(&file_descriptor_path)
    .skip_protoc_run()
    .compile(&["root.proto"], &["."])
    .unwrap();

§Error messages

This crate uses miette to add additional details to errors. For nice error messages, add miette as a dependency with the fancy feature enabled and return a miette::Result from your build script.

fn main() -> miette::Result<()> {
  let _ = protox::compile(["root.proto"], ["."])?;

  Ok(())
}

Example error message:

Error:
  × name 'Bar' is not defined
   ╭─[root.proto:3:1]
 3 │ message Foo {
 4 │     Bar bar = 1;
   ·     ─┬─
   ·      ╰── found here
 5 │ }
   ╰────

Re-exports§

Modules§

  • Interfaces for customizing resolution of protobuf source files.

Structs§

  • Options for compiling protobuf files.
  • An error that can occur when compiling protobuf files.

Functions§

  • Compiles a set of protobuf files using the given include paths.