1 unstable release
Uses new Rust 2024
new 0.2.2 | Mar 27, 2025 |
---|---|
0.2.1 |
|
0.2.0 |
|
0.1.0 |
|
#171 in Build Utils
190 downloads per month
145KB
3K
SLoC
Protocol Buffers Parity SCALE Codec Generator
It generates Rust code from source .proto files using the proto2 or proto3 syntax. It's goal is to make the generated code as simple as possible in Parity SCALE Codec fromat.
ppsc-build compiles .proto files into Rust.
ppsc-build is designed to be used for build-time code generation as part of a Cargo build-script.
Usage
Let's create a small library crate, snazzy
, that defines a collection of snazzy new items in a protobuf file.
$ cargo new --lib snazzy && cd snazzy
First, add pppsc-build
and parity-scale-codec
as dependencies to Cargo.toml
:
$ cargo add --build ppsc-build
$ cargo add parity-scale-codec
Next, add src/items.proto
to the project:
syntax = "proto3";
package snazzy.items;
// A snazzy new shirt!
message Shirt {
// Label sizes
enum Size {
SMALL = 0;
MEDIUM = 1;
LARGE = 2;
}
// The base color
string color = 1;
// The size as stated on the label
Size size = 2;
}
To generate Rust code from items.proto
, we use ppsc-build
in the crate's build.rs
build-script:
use std::io::Result;
fn main() -> Result<()> {
ppsc_build::compile_protos(&["src/items.proto"], &["src/"])?;
Ok(())
}
And finally, in lib.rs
, include the generated code:
// Include the `items` module, which is generated from items.proto.
// It is important to maintain the same structure as in the proto.
pub mod snazzy {
pub mod items {
include!(concat!(env!("OUT_DIR"), "/snazzy.items.rs"));
}
}
use snazzy::items;
/// Returns a large shirt of the specified color
pub fn create_large_shirt(color: String) -> items::Shirt {
let mut shirt = items::Shirt::default();
shirt.color = color;
shirt.set_size(items::shirt::Size::Large);
shirt
}
Inspirition
LICENSE
MIT
Dependencies
~12–23MB
~313K SLoC