Giter Site home page Giter Site logo

j-chmielewski / prost-helper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tyrchen/prost-helper

0.0 0.0 0.0 2.87 MB

Two crates to facilitate prost to better work with protobuf. Serde is supported and best practices are applied.

License: MIT License

Rust 100.00%

prost-helper's Introduction

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.6"

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

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

prost-helper's People

Contributors

tyrchen avatar danni-m avatar graelo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.