#protobuf #grpc #rpc #google-api #server-client

macro tonic_include_protos

A helper proc-macro to include all protobuf files generated by tonic_build

3 releases

0.1.2 Jun 9, 2021
0.1.1 Jul 9, 2020
0.1.0 Jul 8, 2020

#96 in #server-client

33 downloads per month

MIT license

7KB

Include all generated proto server and client items.

tonic_include_protos::include_protos!();

Motivation:

I am working on a project with somewhat large grpc base, where we used tonic and google apis. tonic uses nice prost-based build system, but it generates a lot of files. Each of them must be included manually in separate mod to work correctly. If you just smash them all in one module, compilation will likely fail.

How it works:

This macro will construct mod tree based on grpc package names. For example if you have two generated by tonic_build files in OUT_DIR:

  • package.api.example.rs
  • package.api.another_example.rs

The result will be equivalent to this:

pub mod package{
    pub mod api {
        pub mod example {
            include!(concat!(env!("OUT_DIR"), "/google.api.example.rs"));
        }
        pub mod another_example {
            include!(concat!(env!("OUT_DIR"), "/google.api.another_example.rs"));
        }
    }
}

If OUT_DIR won't work for you (when you set tonic_build to save files in other directory or for some other reason) you can set TIP_OUT_DIR environment variable to point on the directory you need.

I know this solution is not perfect, but it's getting work done. If you have better ideas on implementation - feel free to open issue or PR.

License

This project is licensed under the MIT license.

No runtime deps