Skip to content

Commit 982d26e

Browse files
committed
feat: v6.0.3
1 parent 23eae10 commit 982d26e

File tree

5 files changed

+90
-46
lines changed

5 files changed

+90
-46
lines changed

src/common/enum.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,35 @@ use crate::*;
22

33
/// Defines the type of macro handler.
44
///
5-
/// This enum is used to differentiate between simple macros and those that take attributes.
5+
/// This enum distinguishes between simple macros that do not accept attributes
6+
/// and more complex macros that can process attribute inputs. It is used to route
7+
/// macro invocations to the appropriate expansion logic based on their expected syntax.
68
pub(crate) enum Handler {
7-
/// A simple macro handler that takes no attributes.
9+
/// A macro handler for simple macros that do not take any attributes.
10+
///
11+
/// This variant is used for attribute-like macros that are invoked as `#[my_macro]`
12+
/// without any additional arguments. The associated `MacroHandlerPosition` is responsible
13+
/// for handling the macro at a specific location in the syntax tree.
814
NoAttrPosition(MacroHandlerPosition),
9-
/// A macro handler that takes attributes.
15+
/// A macro handler for macros that accept attribute arguments.
16+
///
17+
/// This variant is used for macros that support syntax like `#[my_macro(...)]`,
18+
/// where the content inside the parentheses is parsed and processed as input.
19+
/// The `MacroHandlerWithAttr` contains the logic to interpret and expand such macros.
1020
WithAttr(MacroHandlerWithAttr),
21+
/// A macro handler for macros that accept attribute arguments and depend on position.
22+
///
23+
/// This variant is used for macros with syntax like `#[my_macro(...)]`, similar to `WithAttr`.
24+
/// The difference is that `WithAttrPosition` also incorporates the syntactic position
25+
/// of the macro invocation into the expansion logic.
26+
/// The `MacroHandlerWithAttrPosition` handles both the attribute input and the positional context.
1127
WithAttrPosition(MacroHandlerWithAttrPosition),
1228
}
1329

30+
/// Defines the position where code should be injected in a function.
1431
pub(crate) enum Position {
32+
/// Injects code at the beginning of the function body.
1533
Prologue,
34+
/// Injects code at the end of the function body.
1635
Epilogue,
1736
}

src/common/fn.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ fn inject_at_end(input: TokenStream, after_fn: impl FnOnce(&Ident) -> TokenStrea
6969
}
7070
}
7171

72+
/// Injects code into a function at a specified position.
73+
///
74+
/// # Arguments
75+
///
76+
/// - `Position` - The position at which to inject the code (`Prologue` or `Epilogue`).
77+
/// - `TokenStream` - The input `TokenStream` of the function to modify.
78+
/// - `impl FnOnce(&Ident) -> TokenStream2` - A closure that generates the code to be injected, based on the function's context identifier.
79+
///
80+
/// # Returns
81+
///
82+
/// - `TokenStream` - Returns the modified `TokenStream` with the injected code.
7283
pub(crate) fn inject(
7384
position: Position,
7485
input: TokenStream,
@@ -88,7 +99,7 @@ pub(crate) fn inject(
8899
///
89100
/// # Returns
90101
///
91-
/// - `syn::Result<&Ident>` - The parsed context identifier or error.
102+
/// - `syn::Result<&Ident>` - Returns a `syn::Result` containing the context identifier if successful, or an error otherwise.
92103
pub(crate) fn parse_context_from_fn(sig: &Signature) -> syn::Result<&Ident> {
93104
match sig.inputs.first() {
94105
Some(FnArg::Typed(pat_type)) => match &*pat_type.pat {
@@ -117,11 +128,13 @@ pub(crate) fn parse_context_from_fn(sig: &Signature) -> syn::Result<&Ident> {
117128
/// - Any other expression types will result in `None`.
118129
/// - If `opt_expr` is `None`, the result is also `None`.
119130
///
120-
/// # Parameters
121-
/// - `opt_expr` - An optional reference to the expression to convert.
131+
/// # Arguments
132+
///
133+
/// - `&Option<Expr>` - An optional reference to the expression to convert.
122134
///
123135
/// # Returns
124-
/// - A `TokenStream2` representing `Some(isize)` for supported literals, or `None` otherwise.
136+
///
137+
/// - `TokenStream` - A `TokenStream2` representing `Some(isize)` for supported literals, or `None` otherwise.
125138
pub(crate) fn expr_to_isize(opt_expr: &Option<Expr>) -> TokenStream2 {
126139
match opt_expr {
127140
Some(expr) => match expr {

src/common/type.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ pub(crate) type MacroHandlerPosition = fn(TokenStream, Position) -> TokenStream;
1010
/// and returns a `TokenStream`.
1111
pub(crate) type MacroHandlerWithAttr = fn(TokenStream, TokenStream) -> TokenStream;
1212

13+
/// A type alias for a macro handler function that accepts attributes and a position.
14+
///
15+
/// This handler takes two `TokenStream`s as input (one for attributes, one for the item),
16+
/// a `Position` enum, and returns a `TokenStream`.
1317
pub(crate) type MacroHandlerWithAttrPosition =
1418
fn(TokenStream, TokenStream, Position) -> TokenStream;

src/hook/fn.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ use crate::*;
77
///
88
/// # Arguments
99
///
10-
/// - `TokenStream` - The attribute token stream, which can optionally specify an `order`.
11-
/// - `TokenStream` - The input token stream representing the function to be registered as a hook.
10+
/// - `attr` - The attribute `TokenStream`, which can optionally specify an `order`.
11+
/// - `item` - The input `TokenStream` representing the function to be registered as a hook.
1212
///
1313
/// # Note
1414
///
1515
/// If an order parameter is not specified, the hook will have a higher priority than hooks with a specified order.
1616
///
1717
/// # Returns
1818
///
19-
/// - `TokenStream` - The expanded token stream with the hook registration.
19+
/// Returns the expanded `TokenStream` with the hook registration.
2020
pub(crate) fn connected_hook_macro(attr: TokenStream, item: TokenStream) -> TokenStream {
2121
let attr_args: OrderAttr = parse_macro_input!(attr as OrderAttr);
2222
let order: TokenStream2 = expr_to_isize(&attr_args.order);
@@ -48,16 +48,16 @@ inventory::submit! {
4848
///
4949
/// # Arguments
5050
///
51-
/// - `TokenStream` - The attribute token stream, which can optionally specify an `order`.
52-
/// - `TokenStream` - The input token stream representing the function to be registered as a hook.
51+
/// - `attr` - The attribute `TokenStream`, which can optionally specify an `order`.
52+
/// - `item` - The input `TokenStream` representing the function to be registered as a hook.
5353
///
5454
/// # Note
5555
///
5656
/// If an order parameter is not specified, the hook will have a higher priority than hooks with a specified order.
5757
///
5858
/// # Returns
5959
///
60-
/// - `TokenStream` - The expanded token stream with the hook registration.
60+
/// Returns the expanded `TokenStream` with the hook registration.
6161
pub(crate) fn prologue_upgrade_hook_macro(attr: TokenStream, item: TokenStream) -> TokenStream {
6262
let attr_args: OrderAttr = parse_macro_input!(attr as OrderAttr);
6363
let order: TokenStream2 = expr_to_isize(&attr_args.order);
@@ -89,16 +89,16 @@ inventory::submit! {
8989
///
9090
/// # Arguments
9191
///
92-
/// - `TokenStream` - The attribute token stream, which can optionally specify an `order`.
93-
/// - `TokenStream` - The input token stream representing the function to be registered as a hook.
92+
/// - `attr` - The attribute `TokenStream`, which can optionally specify an `order`.
93+
/// - `item` - The input `TokenStream` representing the function to be registered as a hook.
9494
///
9595
/// # Note
9696
///
9797
/// If an order parameter is not specified, the hook will have a higher priority than hooks with a specified order.
9898
///
9999
/// # Returns
100100
///
101-
/// - `TokenStream` - The expanded token stream with the hook registration.
101+
/// Returns the expanded `TokenStream` with the hook registration.
102102
pub(crate) fn panic_hook_macro(attr: TokenStream, item: TokenStream) -> TokenStream {
103103
let attr_args: OrderAttr = parse_macro_input!(attr as OrderAttr);
104104
let order: TokenStream2 = expr_to_isize(&attr_args.order);
@@ -130,12 +130,12 @@ inventory::submit! {
130130
///
131131
/// # Arguments
132132
///
133-
/// - `TokenStream` - The attribute token stream, containing the path to disable the hook for and an optional order.
134-
/// - `TokenStream` - The input token stream representing the function to be registered.
133+
/// - `attr` - The attribute `TokenStream`, containing the path to disable the hook for and an optional order.
134+
/// - `item` - The input `TokenStream` representing the function to be registered.
135135
///
136136
/// # Returns
137137
///
138-
/// - `TokenStream` - The expanded token stream with the hook disabling registration.
138+
/// Returns the expanded `TokenStream` with the hook disabling registration.
139139
pub(crate) fn disable_http_hook_macro(attr: TokenStream, item: TokenStream) -> TokenStream {
140140
let attr_args: PathAttr = parse_macro_input!(attr as PathAttr);
141141
let path: &Expr = &attr_args.path;
@@ -167,12 +167,12 @@ inventory::submit! {
167167
///
168168
/// # Arguments
169169
///
170-
/// - `TokenStream` - The attribute token stream, containing the path to disable the hook for and an optional order.
171-
/// - `TokenStream` - The input token stream representing the function to be registered.
170+
/// - `attr` - The attribute `TokenStream`, containing the path to disable the hook for and an optional order.
171+
/// - `item` - The input `TokenStream` representing the function to be registered.
172172
///
173173
/// # Returns
174174
///
175-
/// - `TokenStream` - The expanded token stream with the hook disabling registration.
175+
/// Returns the expanded `TokenStream` with the hook disabling registration.
176176
pub(crate) fn disable_ws_hook_macro(attr: TokenStream, item: TokenStream) -> TokenStream {
177177
let attr_args: PathAttr = parse_macro_input!(attr as PathAttr);
178178
let path: &Expr = &attr_args.path;
@@ -201,12 +201,12 @@ inventory::submit! {
201201
///
202202
/// # Arguments
203203
///
204-
/// - `TokenStream` - The attribute token stream.
205-
/// - `TokenStream` - The input token stream to process.
204+
/// - `attr` - The attribute token stream.
205+
/// - `item` - The input token stream to process.
206206
///
207207
/// # Returns
208208
///
209-
/// - `TokenStream` - The expanded token stream with pre-hook call.
209+
/// Returns the expanded `TokenStream` with pre-hook call.
210210
pub(crate) fn prologue_hook_macro(
211211
attr: TokenStream,
212212
item: TokenStream,
@@ -231,12 +231,12 @@ inventory::submit! {
231231
///
232232
/// # Arguments
233233
///
234-
/// - `TokenStream` - The attribute token stream.
235-
/// - `TokenStream` - The input token stream to process.
234+
/// - `attr` - The attribute token stream.
235+
/// - `item` - The input token stream to process.
236236
///
237237
/// # Returns
238238
///
239-
/// - `TokenStream` - The expanded token stream with post-hook call.
239+
/// Returns the expanded `TokenStream` with post-hook call.
240240
pub(crate) fn epilogue_hook_macro(
241241
attr: TokenStream,
242242
item: TokenStream,

src/http/fn.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ use crate::*;
77
///
88
/// # Arguments
99
///
10-
/// - `$name`: The name of the generated handler function.
11-
/// - `$method`: The HTTP method as a string literal (e.g., "get", "post").
10+
/// - `$name` - The name of the generated handler function.
11+
/// - `$method` - The HTTP method as a string literal (e.g., "get", "post").
12+
///
13+
/// # Returns
14+
///
15+
/// Returns a macro that generates a handler function for the specified HTTP method.
1216
macro_rules! impl_http_method_macro {
1317
($name:ident, $method:expr) => {
1418
pub(crate) fn $name(item: TokenStream, position: Position) -> TokenStream {
@@ -27,43 +31,43 @@ macro_rules! impl_http_method_macro {
2731
};
2832
}
2933

30-
// This macro expands to a check that aborts the request if the HTTP method is not GET.
34+
// Generates a handler that checks if the HTTP method is GET.
3135
impl_http_method_macro!(get_handler, "get");
3236

33-
// This macro expands to a check that aborts the request if the HTTP method is not POST.
37+
// Generates a handler that checks if the HTTP method is POST.
3438
impl_http_method_macro!(epilogue_handler, "post");
3539

36-
// This macro expands to a check that aborts the request if the HTTP method is not PUT.
40+
// Generates a handler that checks if the HTTP method is PUT.
3741
impl_http_method_macro!(put_handler, "put");
3842

39-
// This macro expands to a check that aborts the request if the HTTP method is not DELETE.
43+
// Generates a handler that checks if the HTTP method is DELETE.
4044
impl_http_method_macro!(delete_handler, "delete");
4145

42-
// This macro expands to a check that aborts the request if the HTTP method is not PATCH.
46+
// Generates a handler that checks if the HTTP method is PATCH.
4347
impl_http_method_macro!(patch_handler, "patch");
4448

45-
// This macro expands to a check that aborts the request if the HTTP method is not HEAD.
49+
// Generates a handler that checks if the HTTP method is HEAD.
4650
impl_http_method_macro!(head_handler, "head");
4751

48-
// This macro expands to a check that aborts the request if the HTTP method is not OPTIONS.
52+
// Generates a handler that checks if the HTTP method is OPTIONS.
4953
impl_http_method_macro!(options_handler, "options");
5054

51-
// This macro expands to a check that aborts the request if the HTTP method is not CONNECT.
55+
// Generates a handler that checks if the HTTP method is CONNECT.
5256
impl_http_method_macro!(connect_handler, "connect");
5357

54-
// This macro expands to a check that aborts the request if the HTTP method is not TRACE.
58+
// Generates a handler that checks if the HTTP method is TRACE.
5559
impl_http_method_macro!(trace_handler, "trace");
5660

57-
/// Creates method check function for HTTP request validation.
61+
/// Creates a method check function for HTTP request validation.
5862
///
5963
/// # Arguments
6064
///
61-
/// - `&str` - The method name string.
62-
/// - `proc_macro2::Span` - The span for error reporting.
65+
/// - `method_name` - The HTTP method name as a string.
66+
/// - `span` - The span for error reporting.
6367
///
6468
/// # Returns
6569
///
66-
/// - `impl FnOnce(&Ident) -> TokenStream2` - The generated check function.
70+
/// Returns a closure that generates the method check code.
6771
pub(crate) fn create_method_check(
6872
method_name: &str,
6973
span: proc_macro2::Span,
@@ -80,14 +84,18 @@ pub(crate) fn create_method_check(
8084

8185
/// Handles HTTP requests for multiple method types.
8286
///
87+
/// This macro allows a handler to respond to multiple HTTP methods.
88+
/// It generates code that checks if the request method matches any of the specified methods.
89+
///
8390
/// # Arguments
8491
///
85-
/// - `TokenStream` - The attribute token stream.
86-
/// - `TokenStream` - The input token stream to process.
92+
/// - `attr` - The attribute `TokenStream` containing the list of allowed HTTP methods.
93+
/// - `item` - The input `TokenStream` representing the handler function.
94+
/// - `position` - The position at which to inject the method check code.
8795
///
8896
/// # Returns
8997
///
90-
/// - `TokenStream` - The expanded token stream with methods check.
98+
/// Returns the expanded `TokenStream` with the methods check code injected.
9199
pub(crate) fn methods_macro(
92100
attr: TokenStream,
93101
item: TokenStream,

0 commit comments

Comments
 (0)