Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 64 additions & 7 deletions grpc/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

/// Includes generated proto message, client, and server code.
///
/// You must specify the path to the `.proto` file
/// **relative to the proto root directory**, without the `.proto` extension.
/// You must specify the path to the `.proto` file **relative to the proto root
/// directory**, without the `.proto` extension.
///
/// For example, if your proto directory is `path/to/protos` and it contains the
/// file `helloworld.proto`, you would write:
Expand All @@ -38,8 +38,7 @@
///
/// # Note
/// **This macro only works if the gRPC build output directory and message path
/// are unmodified.**
/// By default:
/// are unmodified.** By default:
/// - The output directory is set to the [`OUT_DIR`] environment variable.
/// - The message path is set to `self`.
///
Expand All @@ -52,8 +51,15 @@
/// }
/// ```
///
/// If you have modified the output directory or message path, you should
/// include the generated code manually instead of using this macro.
/// If you have modified the output directory or message path, you should use
/// the include_generated_code macro below instead of using this macro or
/// manually include it yourself.
///
/// ```rust,ignore
/// mod grpc {
/// grpc::include_generated_proto!("path/to/protos", "helloworld");
/// }
/// ```
///
/// The following example assumes the message code is imported using `self`:
///
Expand Down Expand Up @@ -82,7 +88,8 @@
/// }
/// ```
///
/// [`OUT_DIR`]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
/// [`OUT_DIR`]:
/// https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
#[macro_export]
macro_rules! include_proto {
// Assume the generated output dir is OUT_DIR.
Expand All @@ -102,3 +109,53 @@ macro_rules! include_proto {
));
};
}

/// Includes generated proto message, client, and server code. This macro is for
/// if you manually set output_dir instead of using the default OUT_DIR.
///
/// You must specify the path to the `.proto` file **relative to the proto root
/// directory**, without the `.proto` extension.
///
/// For example, if your proto directory is `path/to/protos` and it contains the
/// file `helloworld.proto`, you would write:
///
/// ```rust,ignore
/// mod pb {
/// grpc::include_generated_proto!("path/to/protos", "helloworld");
/// }
/// ```
///
/// If your `.proto` files are not in a subdirectory, you can omit the first
/// parameter.
///
/// ```rust,ignore
/// mod pb {
/// grpc::include_generated_proto!("helloworld");
/// }
/// ```
///

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this missing more documentation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes will add!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added documentation!

/// [`CARGO_MANIFEST_DIR`]:
/// https://doc.rust-lang.org/cargo/reference/environment-variables.html
#[macro_export]
macro_rules! include_generated_proto {
($proto_file:literal) => {
$crate::include_generated_proto!("", $proto_file);
};

($parent_dir:literal, $proto_file:literal) => {
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/",
$parent_dir,
"/generated.rs"
));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/",
$parent_dir,
"/",
$proto_file,
"_grpc.pb.rs"
));
};
}
Loading