Reputation: 675
I'm trying to use or add an external create like serde, to the build.rs file that tonic_build will use to generate the .rs file from the .proto file.
// build.rs file
let proto_file = "src/hello/hello.proto";
tonic_build::configure()
.build_server(true)
.out_dir("./src/hello")
.type_attribute("HelloRequest", "#[derive(Deserialize, Serialize)]") // adding attributes
.compile(&[proto_file], &["."])
.unwrap_or_else(|e| panic!("protobuf compile error: {}", e));
I can see the attributes are added to the rust file, see below :
#[derive(Deserialize, Serialize)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct HelloRequest {
#[prost(string, tag="1")]
pub id: ::prost::alloc::string::String,
#[prost(string, tag="2")]
pub message: ::prost::alloc::string::String,
}
but the file doesn't add: use serde::{Serialize, Deserialize};
causing the build to fail. I tried using server_mod_attribute
but failed. how can I add use serde::{Serialize, Deserialize}
to the auto-generated .rs file using tonic_build?
Upvotes: 3
Views: 1745
Reputation: 749
you can use the full path to the library
.type_attribute("HelloRequest", "#[derive(serde::Deserialize, serde::Serialize)]")
Upvotes: 3