From de03a88152afa1b33dc7044523e504fd9ecd6a63 Mon Sep 17 00:00:00 2001 From: jokemanfire Date: Mon, 6 Jan 2025 14:58:51 +0800 Subject: [PATCH] Add macros for include ttrpc gen code Give macros for more convenient to use, if it is in Default dir Signed-off-by: jokemanfire --- src/lib.rs | 29 +++++++++++++++++++++++++++++ ttrpc-codegen/src/lib.rs | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 9742b7a..141bbc4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,3 +76,32 @@ cfg_async! { #[doc(hidden)] pub use asynchronous as r#async; } + +/// Macro to include the mod.rs file from the OUT_DIR +/// This is only work on out_dir not set and use 'gen_mod' feature +/// ```rust,ignore +/// mod protocals { +/// include_mod!(); +/// } +/// ``` +/// [`OUT_DIR`]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts +#[macro_export] +macro_rules! include_mod { + () => { + include!(concat!(env!("OUT_DIR"), concat!("/", "mod.rs"))); + }; +} + +/// Macro to include files ending with _ttrpc.rs from the OUT_DIR +/// This is only work on out_dir not set +/// ```rust,ignore +/// mod protocals { +/// include_ttrpc!("helloworld"); +/// } +/// ``` +#[macro_export] +macro_rules! include_ttrpc { + ($name:expr) => { + include!(concat!(env!("OUT_DIR"), concat!("/", $name, "_ttrpc.rs"))); + }; +} diff --git a/ttrpc-codegen/src/lib.rs b/ttrpc-codegen/src/lib.rs index e9c7445..57038c5 100644 --- a/ttrpc-codegen/src/lib.rs +++ b/ttrpc-codegen/src/lib.rs @@ -26,7 +26,7 @@ //! ``` //! If there's no out_dir and use 'gen_mod' feature //! You can use the following method to include the target file -//! include!(concat!(env!("OUT_DIR"), "/mod.rs")); +//! ttrpc::include_mod!(); pub use protobuf_codegen::{ Customize as ProtobufCustomize, CustomizeCallback as ProtobufCustomizeCallback,