1

A question about gRPC codegen with tonic (uses protoc) under the hood.

The third party protos I am using are have package names like thirdparty.specificpackage, for example:

syntax = "proto3";

package thirdparty.common;

import "google/protobuf/empty.proto";
import "google/protobuf/wrappers.proto";

service CommonService {
...
}

and a build.rs that looks like this:

tonic_build::configure()
        .protoc_arg("--experimental_allow_proto3_optional") // for older systems
        .build_client(true)
        .build_server(true)
        .out_dir("./src")
        .compile(
            &[
                "src/myprotoproject/protos/services/decision_engine.proto"],
            &["src"],
        )?;

the generated code from my cargo build is named thirdparty.common.rs - how do I import this into my Rust code?

mod thirdparty.common is not a valid Rust identifier because of the . - are there best practices around this?

Thomas Murphy
  • 1,360
  • 4
  • 14
  • 41
  • 1
    `tonic::include_proto!` ? https://docs.rs/tonic/latest/tonic/macro.include_proto.html – Ömer Erden Aug 23 '23 at 20:17
  • @jsejcksn Going to accept answer below since it is the most direct solution for posterity, but that post is linked to the "can you include a '.'" part of the question, so let's leave it linked. – Thomas Murphy Aug 23 '23 at 20:44

1 Answers1

5

If you cannot reasonably control or change the name of the file, you can use a path attribute:

#[path = "thirdparty.common.rs"]
mod thirdparty_common;
kmdreko
  • 42,554
  • 6
  • 57
  • 106