2424
2525/// Includes generated proto message, client, and server code.
2626///
27- /// You must specify the path to the `.proto` file
28- /// **relative to the proto root directory**, without the `.proto` extension.
27+ /// You must specify the path to the `.proto` file **relative to the proto root
28+ /// directory**, without the `.proto` extension.
2929///
3030/// For example, if your proto directory is `path/to/protos` and it contains the
3131/// file `helloworld.proto`, you would write:
3838///
3939/// # Note
4040/// **This macro only works if the gRPC build output directory and message path
41- /// are unmodified.**
42- /// By default:
41+ /// are unmodified.** By default:
4342/// - The output directory is set to the [`OUT_DIR`] environment variable.
4443/// - The message path is set to `self`.
4544///
5251/// }
5352/// ```
5453///
55- /// If you have modified the output directory or message path, you should
56- /// include the generated code manually instead of using this macro.
54+ /// If you have modified the output directory or message path, you should use
55+ /// the include_generated_code macro below instead of using this macro or
56+ /// manually include it yourself.
57+ ///
58+ /// ```rust,ignore
59+ /// mod grpc {
60+ /// grpc::include_generated_proto!("path/to/protos", "helloworld");
61+ /// }
62+ /// ```
5763///
5864/// The following example assumes the message code is imported using `self`:
5965///
8288/// }
8389/// ```
8490///
85- /// [`OUT_DIR`]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
91+ /// [`OUT_DIR`]:
92+ /// https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
8693#[ macro_export]
8794macro_rules! include_proto {
8895 // Assume the generated output dir is OUT_DIR.
@@ -102,3 +109,53 @@ macro_rules! include_proto {
102109 ) ) ;
103110 } ;
104111}
112+
113+ /// Includes generated proto message, client, and server code. This macro is for
114+ /// if you manually set output_dir instead of using the default OUT_DIR.
115+ ///
116+ /// You must specify the path to the `.proto` file **relative to the proto root
117+ /// directory**, without the `.proto` extension.
118+ ///
119+ /// For example, if your proto directory is `path/to/protos` and it contains the
120+ /// file `helloworld.proto`, you would write:
121+ ///
122+ /// ```rust,ignore
123+ /// mod pb {
124+ /// grpc::include_generated_proto!("path/to/protos", "helloworld");
125+ /// }
126+ /// ```
127+ ///
128+ /// If your `.proto` files are not in a subdirectory, you can omit the first
129+ /// parameter.
130+ ///
131+ /// ```rust,ignore
132+ /// mod pb {
133+ /// grpc::include_generated_proto!("helloworld");
134+ /// }
135+ /// ```
136+ ///
137+ /// [`CARGO_MANIFEST_DIR`]:
138+ /// https://doc.rust-lang.org/cargo/reference/environment-variables.html
139+ #[ macro_export]
140+ macro_rules! include_generated_proto {
141+ ( $proto_file: literal) => {
142+ $crate:: include_generated_proto!( "" , $proto_file) ;
143+ } ;
144+
145+ ( $parent_dir: literal, $proto_file: literal) => {
146+ include!( concat!(
147+ env!( "CARGO_MANIFEST_DIR" ) ,
148+ "/" ,
149+ $parent_dir,
150+ "/generated.rs"
151+ ) ) ;
152+ include!( concat!(
153+ env!( "CARGO_MANIFEST_DIR" ) ,
154+ "/" ,
155+ $parent_dir,
156+ "/" ,
157+ $proto_file,
158+ "_grpc.pb.rs"
159+ ) ) ;
160+ } ;
161+ }
0 commit comments