14 unstable releases (6 breaking)

0.7.0 Jul 8, 2024
0.6.0 Feb 7, 2024
0.5.0 Sep 1, 2023
0.4.1 Jun 25, 2023
0.1.0 Aug 14, 2022

#1081 in Encoding

Download history 38613/week @ 2024-07-30 37083/week @ 2024-08-06 43683/week @ 2024-08-13 36456/week @ 2024-08-20 31417/week @ 2024-08-27 27853/week @ 2024-09-03 42990/week @ 2024-09-10 33377/week @ 2024-09-17 36137/week @ 2024-09-24 32079/week @ 2024-10-01 39358/week @ 2024-10-08 35057/week @ 2024-10-15 46955/week @ 2024-10-22 39081/week @ 2024-10-29 36536/week @ 2024-11-05 31928/week @ 2024-11-12

161,622 downloads per month
Used in 110 crates (via protox)

MIT/Apache

160KB
4K SLoC

crates.io docs.rs deps.rs MSRV Continuous integration codecov Apache 2.0 OR MIT licensed

protox

An implementation of the protobuf compiler in rust, intended for use as a library with crates such as prost-build to avoid needing to build protoc.

Examples

Compiling a single source file:

assert_eq!(protox::compile(["root.proto"], ["."]).unwrap(), FileDescriptorSet {
    file: vec![
        FileDescriptorProto {
            name: Some("root.proto".to_owned()),
            /* ... */
        }
    ],
});

Usage with prost-build:

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

Usage with tonic-build:

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}
   ╰────

Minimum Supported Rust Version

Rust 1.70 or higher.

The minimum supported Rust version may be changed in the future, but it will be done with a minor version bump.

License

Licensed under either of

at your option.

This project includes code imported from the Protocol Buffers project, which is included under its original (BSD) license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~4MB
~48K SLoC