0

I'm generating protobuf code for my Rust project using the neoeinstein-prost buf plugin which, so far, is working well for my use case. My question is where is the idiomatic place for the generated *.rs files to go and be referenced? Is this a use-case for workspaces (which for this project, so far, I haven't needed)?

A couple of options that work for me but may not be idiomatic (proto holds the proto spec and gen the generated output in each case):

Option 1

The pro of this option is generated code spec and output is clearly separate from hand-crafted code. The con is the nasty path directive that points outside the main src tree.

- examples (uses lib)
- gen
  |- src
  |  |- my_model.v1.rs
  |  |- my_model.v1.serde.rs (gets !included by my_model.v1.rs)
- proto
  |- my_model
  |  |- v1
  |  |  |- my_model.proto
- src
  |- main.rs (uses lib)
  |- lib
  |  |- lib.rs (exports "model" module using #[path = "../../gen/src/my_model.v1.rs"]

Option 2

The pro of this option is all lib code is self-contained, whether hand-crafted or generated. The con is you need to go looking for the generated code -it's not as obvious.

- examples (uses lib)
- proto
  |- my_model
  |  |- v1
  |  |  |- my_model.proto
- src
  |- main.rs (uses lib)
  |- lib
  |  |- lib.rs (exports "model" module using #[path = "gen/src/my_model.v1.rs"]
  |  |- gen
  |  |  |- src (unnecessary level?)
  |  |  |  |- my_model.v1.rs
  |  |  |  |- my_model.v1.serde.rs (gets !included by my_model.v1.rs)

Goldie
  • 81
  • 1
  • 7
  • 1
    I think the answer depends on whether you want to commit generated code to a repository. – Ivan C Jul 06 '23 at 20:54
  • .gitignore can be configured to ignore either of the above scenarios and it is our preference to not commit generated code in our case but use the buf schema repository for schemas shared across microservices – Goldie Jul 06 '23 at 22:48
  • What do you mean by "you need to go looking for the generated code"? It seems the gen is one of the modules of your crate. – Campbell He Jul 07 '23 at 02:58
  • Yes, you're right @CampbellHe. It is all there, it's just the files are kind-of buried in the file structure a bit. Everything works just fine, my question is just about idiomatic Rust and being kind to my fellow developers. If it doesn't really matter, all good - we'll just pick one and move forward – Goldie Jul 07 '23 at 03:47
  • 1
    For code that is generated from the `build.rs`, common practice is to output it to the `target` folder as stored in the `OUT_DIR` environment variable. This is the default behaviour for [`prost-build`](https://docs.rs/prost-build/latest/prost_build/fn.compile_protos.html) for example. – Jmb Jul 07 '23 at 07:11
  • 1
    I see. I don't know whether there exists a consensus. An example may be [rust-analyzer's syntax kind](https://github.com/rust-lang/rust-analyzer/tree/master/crates/parser/src/syntax_kind) – Campbell He Jul 07 '23 at 08:17
  • Thank you @Jmb. I'll look into that idea. (The buf add-on uses prost under the hood). – Goldie Jul 09 '23 at 05:33
  • Thank you @CampbellHe. That would point to a simplified variant of my second option being at least something done by another respected project. – Goldie Jul 09 '23 at 05:34

0 Answers0