rust-protobuf icon indicating copy to clipboard operation
rust-protobuf copied to clipboard

Customizing output of proto plugin

Open snowp opened this issue 3 years ago • 0 comments

From my understanding, there are two ways to customize the output (e.g. using lite_runtime): 1) setting the options on Customize when generating the protos and 2) setting proto options on the input files.

For our use case, we're consuming rust-protobuf via rules_rust which uses the rustc plugin, which eliminates the first option. The second option is also not ideal for us because the protos are shared between with other systems which does want to use features such as JSON serialization via rust-protobuf.

My short term solution here is to patch rust-protobuf to set the additional flags:

--- protobuf-codegen-3.1.0/src/protoc_gen_rust.rs
+++ protobuf-codegen-3.1.0/src/protoc_gen_rust.rs
@@ -8,7 +8,11 @@ use crate::Customize;
 #[doc(hidden)]
 pub fn protoc_gen_rust_main() {
     compiler_plugin::plugin_main(|r| {
-        let customize = Customize::parse_from_parameter(r.parameter).expect("parse options");
+        let mut customize = Customize::parse_from_parameter(r.parameter).expect("parse options");
+        customize.generate_accessors = Some(false);
+        customize.lite_runtime = Some(true);
+        customize.generate_getter = Some(false);
+
         gen_all(
             r.file_descriptors,
             "protoc --rust-out=...",
(END)

I could imagine a few ways to make this easier to support:

  • Ingest environment variables in the proto plugin which can set these customize flags. This would allow me to wrap the plugin with a script that sets the appropriate values.
  • Adjust the plugin API such that one could write a custom protoc_gen_rust.rs version which provides a different set of Customize. This would let me write my own plugin main with my own defaults

Neither seem too hard to do and I'd happily help contribute PRs

Let me know if I missed something obvious :)

snowp avatar Jun 28 '22 12:06 snowp