Description
Hi!
I'm trying to support serde
-based serialization on some Protobuf types I have generated using prost
. I do this conditionally by putting the following in the build script for my crate.
fn main() {
let protos =
prost_build::Config::new()
.type_attribute(".", "#[cfg_attr(feature = \"serde\", derive(serde::Serialize, serde::Deserialize))]");
.compile_protos(&[/* list of files */], &[/* list of includes */])
.unwrap();
}
This works great until I use any of the well-known types. The problem, of course, is that the well-known types have already been generated and exist in prost-types
. The workaround is to use compile_well_known_types()
, like so.
syntax = "proto3";
import "google/protobuf/timestamp.proto";
message LogWithTime {
string log = 1;
google.protobuf.Timestamp timestamp = 2;
}
fn main() {
let protos =
prost_build::Config::new()
.type_attribute(".", "#[cfg_attr(feature = \"serde\", derive(serde::Serialize, serde::Deserialize))]");
.compile_well_known_types() // <---- added this line!
.compile_protos(&[/* list of files */], &[/* list of includes */])
.unwrap();
}
The problem now, however, is that the well-known types I generated are no longer compatible with any other crate's well-known types. So, it becomes difficult to do transformations between the types generated across crates.
My workaround is to generate the well-known types with the appropriate serde
attributes in a new shared crate that can be used by all the other crates in my workspace. But, I was wondering: would it be possible to add a feature flag to prost-types
that would allow one to turn on support for serde
? Or perhaps the feature flag is better situated in prost-build
? I'm not sure exactly where the best place to put it is, but generally having support for serde
would be nice.