#protobuf #prost #serde

prost-helper

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

9 releases (4 breaking)

0.5.0 Feb 28, 2021
0.4.0 Feb 23, 2021
0.3.0 Feb 22, 2021
0.2.2 Feb 22, 2021
0.1.3 Feb 22, 2021

#357 in Development tools

Download history 3/week @ 2021-06-03 13/week @ 2021-06-10 1/week @ 2021-06-17 5/week @ 2021-06-24 2/week @ 2021-07-01 4/week @ 2021-07-08 13/week @ 2021-07-15 6/week @ 2021-07-22 25/week @ 2021-07-29 14/week @ 2021-08-05 22/week @ 2021-08-12 8/week @ 2021-08-19 3/week @ 2021-08-26 7/week @ 2021-09-02 25/week @ 2021-09-09 27/week @ 2021-09-16

122 downloads per month
Used in 2 crates

MIT license

16KB
365 lines

prost-helper

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

  • prost-serde: 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-serde and prost-helper to your Cargo.toml:

[dependencies]
prost-helper = "0.1"

[build-dependencies]
prost-serde = "0.1"

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

fn main() {
    let json = include_str!("path/to/your/build_config.json");
    build_with_serde(json);
}

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, 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

~1.4–2.2MB
~49K SLoC