Skip to content

Commit 4ac9542

Browse files
committed
feat: v4.5.2
1 parent ce19918 commit 4ac9542

File tree

14 files changed

+53
-134
lines changed

14 files changed

+53
-134
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "hyperlane-macros"
3-
version = "4.5.1"
3+
version = "4.5.2"
44
readme = "README.md"
55
edition = "2024"
66
authors = ["[email protected]"]

src/aborted/fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::*;
1515
///
1616
/// Returns the expanded `TokenStream` with the aborted call inserted.
1717
pub(crate) fn aborted_macro(item: TokenStream) -> TokenStream {
18-
expand_macro_with_after_insertion(item, |context| {
18+
inject_at_end(item, |context| {
1919
quote! {
2020
let _ = #context.aborted().await;
2121
}

src/closed/fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::*;
1515
///
1616
/// Returns the expanded `TokenStream` with the closed call inserted.
1717
pub(crate) fn closed_macro(item: TokenStream) -> TokenStream {
18-
expand_macro_with_after_insertion(item, |context| {
18+
inject_at_end(item, |context| {
1919
quote! {
2020
let _ = #context.closed().await;
2121
}

src/common/fn.rs

Lines changed: 2 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::*;
1010
/// # Returns
1111
///
1212
/// - `TokenStream` - The expanded token stream with inserted code.
13-
pub(crate) fn expand_macro_with_before_insertion(
13+
pub(crate) fn inject_at_start(
1414
input: TokenStream,
1515
before_fn: impl FnOnce(&Ident) -> TokenStream2,
1616
) -> TokenStream {
@@ -46,7 +46,7 @@ pub(crate) fn expand_macro_with_before_insertion(
4646
/// # Returns
4747
///
4848
/// - `TokenStream` - The expanded token stream with inserted code.
49-
pub(crate) fn expand_macro_with_after_insertion(
49+
pub(crate) fn inject_at_end(
5050
input: TokenStream,
5151
after_fn: impl FnOnce(&Ident) -> TokenStream2,
5252
) -> TokenStream {
@@ -72,47 +72,6 @@ pub(crate) fn expand_macro_with_after_insertion(
7272
}
7373
}
7474

75-
/// Expands macro with attribute parsing and code insertion before function body.
76-
///
77-
/// # Arguments
78-
///
79-
/// - `TokenStream` - The attribute token stream to parse.
80-
/// - `TokenStream` - The input token stream to process.
81-
/// - `impl FnOnce(TokenStream) -> syn::Result<T>` - Function to parse attributes.
82-
/// - `impl FnOnce(&Ident, T) -> TokenStream2` - Function to generate code inserted before.
83-
///
84-
/// # Returns
85-
///
86-
/// - `TokenStream` - The expanded token stream with inserted code.
87-
pub(crate) fn expand_macro_with_attr_and_before_insertion<T>(
88-
attr: TokenStream,
89-
item: TokenStream,
90-
parse_attr: impl FnOnce(TokenStream) -> syn::Result<T>,
91-
before_fn: impl FnOnce(&Ident, T) -> TokenStream2,
92-
) -> TokenStream {
93-
match parse_attr(attr) {
94-
Ok(value) => expand_macro_with_before_insertion(item, |context| before_fn(context, value)),
95-
Err(err) => err.to_compile_error().into(),
96-
}
97-
}
98-
99-
/// Expands macro with check code inserted before function body.
100-
///
101-
/// # Arguments
102-
///
103-
/// - `TokenStream` - The input token stream to process.
104-
/// - `impl FnOnce(&Ident) -> TokenStream2` - Function to generate check code.
105-
///
106-
/// # Returns
107-
///
108-
/// - `TokenStream` - The expanded token stream with check code.
109-
pub(crate) fn expand_check_macro(
110-
input: TokenStream,
111-
check_fn: impl FnOnce(&Ident) -> TokenStream2,
112-
) -> TokenStream {
113-
expand_macro_with_before_insertion(input, check_fn)
114-
}
115-
11675
/// Parses context identifier from function signature.
11776
///
11877
/// # Arguments
@@ -142,45 +101,6 @@ pub(crate) fn parse_context_from_fn(sig: &Signature) -> syn::Result<&Ident> {
142101
}
143102
}
144103

145-
/// Expands macro with code inserted before the return statement of the function body.
146-
///
147-
/// This function modifies the input `TokenStream` representing a function by inserting
148-
/// generated code right before the final return statement (or at the end if no explicit return).
149-
///
150-
/// # Arguments
151-
///
152-
/// - `input` - The input `TokenStream` to process, typically an `async fn`.
153-
/// - `before_return_fn` - A closure that generates the `TokenStream2` to be inserted.
154-
/// It takes an `&Ident` (the context identifier) as input.
155-
///
156-
/// # Returns
157-
///
158-
/// - `TokenStream` - The expanded `TokenStream` with the generated code inserted.
159-
pub(crate) fn expand_macro_with_before_return_insertion(
160-
input: TokenStream,
161-
before_return_fn: impl FnOnce(&Ident) -> TokenStream2,
162-
) -> TokenStream {
163-
let input_fn: ItemFn = parse_macro_input!(input as ItemFn);
164-
let vis: &Visibility = &input_fn.vis;
165-
let sig: &Signature = &input_fn.sig;
166-
let attrs: &Vec<Attribute> = &input_fn.attrs;
167-
match parse_context_from_fn(sig) {
168-
Ok(context) => {
169-
let before_return_code: TokenStream2 = before_return_fn(context);
170-
let orig_stmts: Vec<Stmt> = input_fn.block.stmts;
171-
let gen_code: TokenStream2 = quote! {
172-
#(#attrs)*
173-
#vis #sig {
174-
#(#orig_stmts)*
175-
#before_return_code
176-
}
177-
};
178-
gen_code.into()
179-
}
180-
Err(err) => err.to_compile_error().into(),
181-
}
182-
}
183-
184104
/// Convert an optional expression into an `Option<isize>` token stream.
185105
///
186106
/// This function supports integer and string literals only:

src/filter/fn.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::*;
1010
///
1111
/// - `TokenStream` - The expanded token stream with filter checks.
1212
pub(crate) fn filter_unknown_macro(item: TokenStream) -> TokenStream {
13-
expand_macro_with_before_insertion(item, |context| {
13+
inject_at_start(item, |context| {
1414
quote! {
1515
{
1616
let request: ::hyperlane::Request = #context.get_request().await;
@@ -34,7 +34,7 @@ pub(crate) fn filter_unknown_macro(item: TokenStream) -> TokenStream {
3434
macro_rules! impl_filter_macro {
3535
($name:ident, $check:ident) => {
3636
pub(crate) fn $name(item: TokenStream) -> TokenStream {
37-
expand_check_macro(item, |context| {
37+
inject_at_start(item, |context| {
3838
let check_fn = Ident::new(stringify!($check), proc_macro2::Span::call_site());
3939
quote! {
4040
if !#context.get_request().await.#check_fn() {

src/flush/fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::*;
1010
///
1111
/// - `TokenStream` - The expanded token stream with flush call.
1212
pub(crate) fn flush_macro(item: TokenStream) -> TokenStream {
13-
expand_macro_with_after_insertion(item, |context| {
13+
inject_at_end(item, |context| {
1414
quote! {
1515
let _ = #context.flush().await;
1616
}

src/hook/fn.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ pub(crate) fn disable_ws_hook_macro(attr: TokenStream, item: TokenStream) -> Tok
174174
/// - `TokenStream` - The expanded token stream with pre-hook call.
175175
pub(crate) fn pre_hook_macro(attr: TokenStream, item: TokenStream) -> TokenStream {
176176
let function_name: Ident = parse_macro_input!(attr as Ident);
177-
expand_macro_with_before_insertion(item, |context| {
177+
inject_at_start(item, |context| {
178178
quote! {
179179
let _ = #function_name(#context.clone()).await;
180180
}
@@ -193,7 +193,7 @@ pub(crate) fn pre_hook_macro(attr: TokenStream, item: TokenStream) -> TokenStrea
193193
/// - `TokenStream` - The expanded token stream with post-hook call.
194194
pub(crate) fn post_hook_macro(attr: TokenStream, item: TokenStream) -> TokenStream {
195195
let function_name: Ident = parse_macro_input!(attr as Ident);
196-
expand_macro_with_after_insertion(item, |context| {
196+
inject_at_end(item, |context| {
197197
quote! {
198198
let _ = #function_name(#context.clone()).await;
199199
}

src/host/fn.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::*;
1313
pub(crate) fn host_macro(attr: TokenStream, item: TokenStream) -> TokenStream {
1414
let host_data: HostData = parse_macro_input!(attr as HostData);
1515
let host_value: Expr = host_data.host_value;
16-
expand_macro_with_before_insertion(item, |context| {
16+
inject_at_start(item, |context| {
1717
quote! {
1818
let request_host: ::hyperlane::RequestHost = #context.get_request_host().await;
1919
if request_host != #host_value.to_string() {
@@ -36,7 +36,7 @@ pub(crate) fn host_macro(attr: TokenStream, item: TokenStream) -> TokenStream {
3636
pub(crate) fn host_filter_macro(attr: TokenStream, item: TokenStream) -> TokenStream {
3737
let host_data: HostData = parse_macro_input!(attr as HostData);
3838
let host_value: Expr = host_data.host_value;
39-
expand_macro_with_before_insertion(item, |context| {
39+
inject_at_start(item, |context| {
4040
quote! {
4141
let request_host: ::hyperlane::RequestHost = #context.get_request_host().await;
4242
if request_host == #host_value.to_string() {

src/http/fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::*;
1212
macro_rules! impl_http_method_macro {
1313
($name:ident, $method:expr) => {
1414
pub(crate) fn $name(item: TokenStream) -> TokenStream {
15-
expand_check_macro(
15+
inject_at_start(
1616
item,
1717
create_method_check($method, proc_macro2::Span::call_site()),
1818
)

src/protocol/fn.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::*;
1010
///
1111
/// - `TokenStream` - The expanded token stream with protocol check.
1212
pub(crate) fn ws_macro(item: TokenStream) -> TokenStream {
13-
expand_check_macro(item, |context| {
13+
inject_at_start(item, |context| {
1414
quote! {
1515
if !#context.get_request().await.is_ws() {
1616
return;
@@ -29,7 +29,7 @@ pub(crate) fn ws_macro(item: TokenStream) -> TokenStream {
2929
///
3030
/// - `TokenStream` - The expanded token stream with protocol check.
3131
pub(crate) fn http_macro(item: TokenStream) -> TokenStream {
32-
expand_check_macro(item, |context| {
32+
inject_at_start(item, |context| {
3333
quote! {
3434
if !#context.get_request().await.is_http() {
3535
return;
@@ -50,7 +50,7 @@ pub(crate) fn http_macro(item: TokenStream) -> TokenStream {
5050
macro_rules! impl_protocol_check_macro {
5151
($name:ident, $check:ident) => {
5252
pub(crate) fn $name(item: TokenStream) -> TokenStream {
53-
expand_check_macro(item, |context| {
53+
inject_at_start(item, |context| {
5454
let check_fn = Ident::new(stringify!($check), proc_macro2::Span::call_site());
5555
quote! {
5656
let request: ::hyperlane::Request = #context.get_request().await;

0 commit comments

Comments
 (0)