rustprotocol-buffersgrpctonic

"." in a Rust module identifier


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?


Solution

  • 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;