#protobuf #proto-file #parser #quick-protobuf

no-std bin+lib pb-rs

A converter from proto files into quick-protobuf compatible Rust module

14 releases (8 breaking)

0.10.0 Nov 22, 2022
0.9.1 Sep 26, 2020
0.9.0 May 17, 2020
0.8.3 Nov 20, 2019
0.2.0 Feb 1, 2018

#1122 in Encoding

Download history 1018/week @ 2024-06-09 1131/week @ 2024-06-16 1357/week @ 2024-06-23 1417/week @ 2024-06-30 1577/week @ 2024-07-07 1701/week @ 2024-07-14 2305/week @ 2024-07-21 1490/week @ 2024-07-28 2165/week @ 2024-08-04 1971/week @ 2024-08-11 1834/week @ 2024-08-18 1996/week @ 2024-08-25 2723/week @ 2024-09-01 1744/week @ 2024-09-08 1833/week @ 2024-09-15 2136/week @ 2024-09-22

8,615 downloads per month
Used in 7 crates (4 directly)

MIT license

150KB
4K SLoC

pb-rs

A simple converter from .proto files into Rust quick-protobuf compatible modules.

Usage

pb-rs <file.proto>

pb-rs can also be used as a crate (for example in your cargo build scripts):

Cargo.toml:

[dependencies]
quick-protobuf = "0.8.0"

[build-dependencies]
pb-rs = "0.9.1"

build.rs:

use pb_rs::{types::FileDescriptor, ConfigBuilder};
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

fn main() {
    let out_dir = std::env::var("OUT_DIR").unwrap();
    let out_dir = Path::new(&out_dir).join("protos");

    let in_dir = PathBuf::from(::std::env::var("CARGO_MANIFEST_DIR").unwrap()).join("protos");
    // Re-run this build.rs if the protos dir changes (i.e. a new file is added)
    println!("cargo:rerun-if-changed={}", in_dir.to_str().unwrap());

    // Find all *.proto files in the `in_dir` and add them to the list of files
    let mut protos = Vec::new();
    let proto_ext = Some(Path::new("proto").as_os_str());
    for entry in WalkDir::new(&in_dir) {
        let path = entry.unwrap().into_path();
        if path.extension() == proto_ext {
            // Re-run this build.rs if any of the files in the protos dir change
            println!("cargo:rerun-if-changed={}", path.to_str().unwrap());
            protos.push(path);
        }
    }

    // Delete all old generated files before re-generating new ones
    if out_dir.exists() {
        std::fs::remove_dir_all(&out_dir).unwrap();
    }
    std::fs::DirBuilder::new().create(&out_dir).unwrap();
    let config_builder = ConfigBuilder::new(&protos, None, Some(&out_dir), &[in_dir]).unwrap();
    FileDescriptor::run(&config_builder.build()).unwrap()
}

main.rs or lib.rs:

mod hello {
    include_bytes!(concat!(env!("OUT_DIR")), "/hello.rs");
}

Dependencies

~0.9–8.5MB
~71K SLoC