#protoc #serialization #protobuf #proto #buffer #generate #api

build protoc-rust

protoc --rust_out=... available as API. protoc needs to be in $PATH, protoc-gen-run does not.

81 stable releases

3.0.0-alpha.2 Nov 1, 2021
2.28.0 Sep 26, 2022
2.27.1 Feb 5, 2022
2.25.2 Oct 24, 2021
1.4.1 Jun 24, 2017

#2383 in Encoding

Download history 6250/week @ 2024-07-22 7759/week @ 2024-07-29 5892/week @ 2024-08-05 5166/week @ 2024-08-12 5023/week @ 2024-08-19 5300/week @ 2024-08-26 5468/week @ 2024-09-02 5046/week @ 2024-09-09 4978/week @ 2024-09-16 6294/week @ 2024-09-23 5576/week @ 2024-09-30 5487/week @ 2024-10-07 5571/week @ 2024-10-14 4311/week @ 2024-10-21 4533/week @ 2024-10-28 4867/week @ 2024-11-04

19,460 downloads per month
Used in fewer than 54 crates

MIT license

2MB
41K SLoC

API to generate .rs files

API to generate .rs files to be used e. g. from build.rs.

Example code:

extern crate protoc_rust;

use protoc_rust::Customize;

fn main() {
    protoc_rust::Codegen::new()
        .out_dir("src/protos")
        .inputs(&["protos/a.proto", "protos/b.proto"])
        .include("protos")
        .run()
        .expect("protoc");
}

And in Cargo.toml:

[build-dependencies]
protoc-rust = "2.0"

Note 1: This API requires protoc command present in $PATH. Although protoc-gen-rust command is not needed.

Note 2: Is advisable that protoc-rust build-dependecy version be the same as protobuf dependency.

The alternative is to use pure-rust .proto parser and code generator.


lib.rs:

API to generate .rs files.

This API requires protoc command present in $PATH or passed explicitly to Codegen object.

extern crate protoc_rust;

fn main() {
    protoc_rust::Codegen::new()
        .out_dir("src/protos")
        .inputs(&["protos/a.proto", "protos/b.proto"])
        .include("protos")
        .run()
        .expect("Running protoc failed.");
}

It is advisable that protoc-rust build-dependecy version be the same as protobuf dependency.

The alternative is to use protobuf-codegen-pure.

Dependencies

~2–11MB
~134K SLoC