"." 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:

        .protoc_arg("--experimental_allow_proto3_optional") // for older systems

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;

Leave a Reply