#protobuf #prost #serde #prost-build

构建 prost-serde

一个支持 serde 的 prost 工具包,用于构建 protobuf

6 个版本

0.3.0 2021 年 11 月 25 日
0.2.0 2021 年 2 月 24 日
0.1.3 2021 年 2 月 23 日

510构建实用工具

Download history 280/week @ 2024-03-13 203/week @ 2024-03-20 200/week @ 2024-03-27 251/week @ 2024-04-03 185/week @ 2024-04-10 125/week @ 2024-04-17 143/week @ 2024-04-24 129/week @ 2024-05-01 202/week @ 2024-05-08 254/week @ 2024-05-15 239/week @ 2024-05-22 300/week @ 2024-05-29 225/week @ 2024-06-05 149/week @ 2024-06-12 482/week @ 2024-06-19 170/week @ 2024-06-26

1,093 每月下载量
7 个 crate 中使用 (通过 redgold-schema)

MIT 许可证

7KB
71

prost-helper

提供了两个 crate,以帮助 prost 更好地与 protobuf 一起工作

  • prost-serde:帮助您使用 prost 和 protobuf 文件生成与 serde 兼容的代码。更多信息请参阅 文档
  • prost-helper:提供 prost 的宏和函数。更多信息请参阅 文档

在 Cargo 项目中使用 prost-helper

首先,将 prost-serdeprost-helper 添加到您的 Cargo.toml

[dependencies]
prost-helper = "0.1"

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

然后将 default_build_config.json 复制到您的项目并自定义它。更多信息请参阅 prost-serde/examples/build_config.json。然后您可以在您的 build.rs 中添加以下内容

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

就是这样!

如果您想为 From 特性生成 Vec<u8> 的实现,或者从 Vec<u8> 到 protobuf 数据结构的 TryFrom 特性,您可以使用 prost_into_vec!vec_try_into_prost! 宏。以下是一个示例

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();

享受 prost 的乐趣!

许可证

prost-helper 根据 MIT 许可证分发。

有关详细信息,请参阅 LICENSE

版权 2021 Tyr Chen

依赖关系

~8–19MB
~255K SLoC