Crate prost_serde[−][src]
A prost toolkit to build protobuf with serde support.
Usually when we define our protobuf messages, we hope some of the generated
data structures have good serde support. Fortunately serde-build
has that
capability - you can create a config with prost_build::Config::new()
and
and then set proper attributes for type or field. For exmaple, you can add
serde support for this structure by using:
config.type_attribute("package.RequestPing", "#[derive(serde::Serialize, serde::Deserialize)]"); config.type_attribute("package.RequestPing", "#[serde(default)]");
and you will get this generated code:
#[derive(serde::Serialize, serde::Deserialize)] #[serde(default)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct RequestPing { #[prost(string, tag = "1")] pub ping: ::prost::alloc::string::String, }
This crate helps to simplify this build script by using a predefined build configuration.
Getting started
First of all, you shall create a JSON file which contains configuration for the builder. You can get a copy of a default JSON file from: default_build_config.json. See an example of build_config.json. Please add the proto files, proto includes, output dir, and the data structure or field you want to add the desired attributes. Then you could use it in:
use prost_serde::build_with_serde; fn main() { let json = include_str!("../examples/build_config.json"); build_with_serde(json); }
Structs
BuildConfig | |
BuildOption |
Functions
build_with_serde | Build the protobuf files with the build opts provided by a JSON string. |