Open mvanotti opened 2 years ago
Its likely the way you are importing your files. Prost expects them to be under specific mods. What you can do is use the include_file
config item and only import that file. It will ensure the modules are all up to date.
Hi Lucio,
Thanks for your answer! That solved the issue :). It took me some time to find the right documentation and understand how to use it. Maybe it would be nice to add a note mentioning how the mods should be defined (and suggesting using include_file
to help with the boilerplate.
My code ended up looking like this:
build.rs
use std::io::Result;
fn main() -> Result<()> {
let mut prost_build = prost_build::Config::new();
prost_build.include_file("_includes.rs");
prost_build.compile_protos(&["src/items.proto"], &["src/"])?;
Ok(())
}
main.rs
include!(concat!(env!("OUT_DIR"), "/_includes.rs"));
use snazzy::items;
pub fn create_large_shirt(color: String) -> items::Shirt {
let mut shirt = items::Shirt::default();
shirt.color = color;
shirt.set_size(items::shirt::Size::Large);
shirt
}
fn main() {
let shirt = create_large_shirt("BLUE".to_string());
println!("shirt: {:?}", shirt);
}
$ tree
.
├── build.rs
├── Cargo.lock
├── Cargo.toml
└── src
├── google
│ ├── protobuf
│ │ └── any.proto
│ └── rpc
│ └── status.proto
├── items.proto
└── main.rs
The contents of _includes.rs
are:
pub mod google {
pub mod rpc {
include!(concat!(env!("OUT_DIR"), "/google.rpc.rs"));
}
pub mod protobuf {
include!(concat!(env!("OUT_DIR"), "/google.protobuf.rs"));
}
}
pub mod snazzy {
pub mod items {
include!(concat!(env!("OUT_DIR"), "/snazzy.items.rs"));
}
}
I think the problem with the super was because in my first attempt, I was not adding pub mod snazzy
.
Lucio, should I send a PR updating the example to add the pub mod snazzy? Probably also mention there using the config + include_file
Thanks a lot! rly helpful
Hi!
I am trying to build a simple example with three protos, but I keep getting this weird error. I am probably holding it wrong, so any advice on how to fix this would be appreciated :)
My example contains basically, items.proto + google.rpc.status + google.protobuf.any:
My
build.rs
:main.rs
:And this is the error I am getting:
Similar issues were reported multiple times, but I couldn't figure out how to fix this problem based on the responses 😅