Crate prost_build_config
source ·Expand description
A prost toolkit to build protobuf with extra derive/attributes support.
Usually when we define our protobuf messages, we hope some of the generated
data structures can derive other traits. e.g. serde support. Fortunately
prost-build
has that capability - you can create a config with
prost_build::Config::new()
and then set proper attributes for type or field.
For example, 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, Eq, ::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 YAML file which contains configuration for the builder. You can get a copy of a default YAML file from: default_build_config.yml. See an example of build_config.yml. 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_build_config::{BuildConfig, Builder};
fn main() {
let content = include_str!("../examples/build_config.yml");
let config: BuildConfig = serde_yaml::from_str(content).unwrap();
Builder::from(config).build_protos();
}