File structure for generated code in Rust

185 Views Asked by At

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)

0

There are 0 best solutions below