prost icon indicating copy to clipboard operation
prost copied to clipboard

prost-build: Idiomatic Code Generation

Open theduke opened this issue 2 years ago • 7 comments

I prefer to generate code manually instead of in build.rs for readability and better IDE support. (rust-analyzer and Intellij both run into issues and instability , no matter the settings).

Currently code generation spits out files named after the Proto package, and creates a single file with the module hierarchy and include!()s.

I've written a little script to transform this into a normal Rust module hierarchy with the expected directory structure.

It would be nice to have this as an official setting in prost-build.

Happy to implement this if it would be accepted.

theduke avatar Apr 15 '22 20:04 theduke

What do you rename the files too?

In addition, this is the approach I took for console to commit the generated code https://github.com/tokio-rs/console/tree/main/console-api/src

LucioFranco avatar Apr 18 '22 15:04 LucioFranco

What do you rename the files too?

To the idiomatic rust structure you'd expect.

So package a.b.c ends up looking like:

a/mod.rs  // pub mod b;
a/b/mod.rs // pub mod c;
a/b/c.rs // the generated "a.b.c.rs"

theduke avatar Apr 19 '22 04:04 theduke

If support for manual code generation is a goal (which I do like), then it would also be nice to have a command line tool (such as pb-rs's binary, but for prost instead of quick-protobuf). If I understand right, prost-build doesn't provide a binary tool as of yet.

JanBeh avatar Feb 03 '23 11:02 JanBeh

I might be missing something, but the source files generated now from proto files with complex package names (e.g. a.b.c) must be manually manipulated to be attached to a module (the dots cause compilation errors). To me, this looked like an obvious bug but I tend to search through issues first :smile:

IMO, @theduke's proposal is necessary to isolate generated files and not worry about them after the build even in the standard build.rs approach. I.e:

  1. Generate a proto to a directory, e.g. ~/src/generated/
  2. Currently manual: create ~/src/generated/mod.rs with pub mod <name> for every file in that directory
  3. Add an entry of pub mod generated to ~/src/lib.rs
  4. Call it a library

bkosm avatar May 07 '23 14:05 bkosm

Has there been any solution?

beautyfree avatar Oct 07 '23 18:10 beautyfree

@theduke can you share your script?

beautyfree avatar Oct 07 '23 19:10 beautyfree

there is already a solution https://github.com/tokio-rs/prost/issues/826

beautyfree avatar Oct 07 '23 19:10 beautyfree