#prost #protobuf #prost-build #serde #macro-helpers

prost-helper

A set of prost helper functions to make prost generated code easy to work with

14 releases (7 breaking)

0.8.3 Dec 22, 2023
0.7.0 Oct 7, 2022
0.6.0 Nov 25, 2021
0.5.0 Feb 28, 2021

#410 in Development tools

Download history 6371/week @ 2024-07-04 11477/week @ 2024-07-11 963/week @ 2024-07-18 378/week @ 2024-07-25 439/week @ 2024-08-01 351/week @ 2024-08-08 407/week @ 2024-08-15 505/week @ 2024-08-22 707/week @ 2024-08-29 325/week @ 2024-09-05 232/week @ 2024-09-12 347/week @ 2024-09-19 579/week @ 2024-09-26 227/week @ 2024-10-03 287/week @ 2024-10-10 295/week @ 2024-10-17

1,480 downloads per month
Used in 10 crates (4 directly)

MIT license

27KB
620 lines

prost-helper

Two crates are provided to facilitate prost to better work with protobuf:

  • prost-build-config: help you to generate serde compatible code with prost and protobuf files. More in documentation.
  • prost-helper: macros and functions to facilitate prost. More in documentation.

Using prost-helper in a Cargo Project

First, add prost-build-config and prost-helper to your Cargo.toml:

[dependencies]
prost-helper = "0.6"

[build-dependencies]
prost-build-config = "0.4"

Then copy the prost-build-config/default_build_config.yml to your project and customize it. See more information in prost-build-config/examples/build_config.yml. Then you could add this in your build.rs:

use prost_build_config::{BuildConfig, Builder};

fn main() {
    let config: BuildConfig = serde_yaml::from_str(include_str!("path/to/your/build_config.yml")).unwrap();
    Builder::from(config).build_protos();
}

That's it!

If you'd like to generate the implementation for From trait to Vec<u8>, or TryFrom trait from Vec<u8> to protobuf data structure, you could use prost_into_vec! and vec_try_into_prost! macros. Here is an example:

use prost::Message;
use std::convert::TryInto;

#[derive(Clone, PartialEq, Eq, Message)]
pub struct Hello {
    #[prost(string, tag = "1")]
    pub msg: String,
}

// implement the traits
prost_into_vec!(Hello, 32);
vec_try_into_prost!(Hello);


let hello = Hello::default();

// use `Into` to convert message to `Vec<u8>`
let data: Vec<u8> = hello.into();

// use `TryInto` to convert `Vec<u8>` back to message
let hello_result: Result<Hello, prost::DecodeError> = data.try_into();

Have fun with prost!

License

prost-helper is distributed under the terms of MIT.

See LICENSE for details.

Copyright 2021 Tyr Chen

Dependencies

~0.6–1.6MB
~33K SLoC