hyperlane_macros/
lib.rs

1//! hyperlane-macros
2//!
3//! A comprehensive collection of procedural macros for building
4//! HTTP servers with enhanced functionality. This crate provides
5//! attribute macros that simplify HTTP request handling, protocol
6//! validation, response management, and request data extraction.
7
8mod aborted;
9mod closed;
10mod common;
11mod filter;
12mod flush;
13mod from_stream;
14mod hook;
15mod host;
16mod http;
17mod http_from_stream;
18mod hyperlane;
19mod inject;
20mod protocol;
21mod referer;
22mod reject;
23mod request;
24mod request_middleware;
25mod response;
26mod response_middleware;
27mod route;
28mod send;
29mod stream;
30
31pub(crate) use aborted::*;
32pub(crate) use closed::*;
33pub(crate) use common::*;
34pub(crate) use filter::*;
35pub(crate) use flush::*;
36pub(crate) use from_stream::*;
37pub(crate) use hook::*;
38pub(crate) use host::*;
39pub(crate) use http::*;
40pub(crate) use http_from_stream::*;
41pub(crate) use hyperlane::*;
42pub(crate) use inject::*;
43pub(crate) use protocol::*;
44pub(crate) use referer::*;
45pub(crate) use reject::*;
46pub(crate) use request::*;
47pub(crate) use request_middleware::*;
48pub(crate) use response::*;
49pub(crate) use response_middleware::*;
50pub(crate) use route::*;
51pub(crate) use send::*;
52pub(crate) use stream::*;
53
54pub(crate) use proc_macro::TokenStream;
55pub(crate) use proc_macro2::TokenStream as TokenStream2;
56pub(crate) use quote::quote;
57pub(crate) use syn::{
58    Ident, Token,
59    parse::{Parse, ParseStream, Parser, Result},
60    punctuated::Punctuated,
61    token::Comma,
62    *,
63};
64
65inventory::collect!(InjectableMacro);
66
67/// Restricts function execution to HTTP GET requests only.
68///
69/// This attribute macro ensures the decorated function only executes when the incoming request
70/// uses the GET HTTP method. Requests with other methods will be filtered out.
71///
72/// # Usage
73///
74/// ```rust
75/// use hyperlane::*;
76/// use hyperlane_macros::*;
77///
78/// #[get]
79/// async fn handle_get(ctx: Context) {
80///     // Function body
81/// }
82/// ```
83///
84/// The macro takes no parameters and should be applied directly to async functions
85/// that accept a `Context` parameter.
86#[proc_macro_attribute]
87pub fn get(_attr: TokenStream, item: TokenStream) -> TokenStream {
88    get_handler(item, Position::Prologue)
89}
90
91/// Restricts function execution to HTTP POST requests only.
92///
93/// This attribute macro ensures the decorated function only executes when the incoming request
94/// uses the POST HTTP method. Requests with other methods will be filtered out.
95///
96/// # Usage
97///
98/// ```rust
99/// use hyperlane::*;
100/// use hyperlane_macros::*;
101///
102/// #[post]
103/// async fn handle_post(ctx: Context) {
104///     // Function body
105/// }
106/// ```
107///
108/// The macro takes no parameters and should be applied directly to async functions
109/// that accept a `Context` parameter.
110#[proc_macro_attribute]
111pub fn post(_attr: TokenStream, item: TokenStream) -> TokenStream {
112    epilogue_handler(item, Position::Prologue)
113}
114
115/// Restricts function execution to HTTP PUT requests only.
116///
117/// This attribute macro ensures the decorated function only executes when the incoming request
118/// uses the PUT HTTP method. Requests with other methods will be filtered out.
119///
120/// # Usage
121///
122/// ```rust
123/// use hyperlane::*;
124/// use hyperlane_macros::*;
125///
126/// #[put]
127/// async fn handle_put(ctx: Context) {
128///     // Function body
129/// }
130/// ```
131///
132/// The macro takes no parameters and should be applied directly to async functions
133/// that accept a `Context` parameter.
134#[proc_macro_attribute]
135pub fn put(_attr: TokenStream, item: TokenStream) -> TokenStream {
136    put_handler(item, Position::Prologue)
137}
138
139/// Restricts function execution to HTTP DELETE requests only.
140///
141/// This attribute macro ensures the decorated function only executes when the incoming request
142/// uses the DELETE HTTP method. Requests with other methods will be filtered out.
143///
144/// # Usage
145///
146/// ```rust
147/// use hyperlane::*;
148/// use hyperlane_macros::*;
149///
150/// #[delete]
151/// async fn handle_delete(ctx: Context) {
152///     // Function body
153/// }
154/// ```
155///
156/// The macro takes no parameters and should be applied directly to async functions
157/// that accept a `Context` parameter.
158#[proc_macro_attribute]
159pub fn delete(_attr: TokenStream, item: TokenStream) -> TokenStream {
160    delete_handler(item, Position::Prologue)
161}
162
163/// Restricts function execution to HTTP PATCH requests only.
164///
165/// This attribute macro ensures the decorated function only executes when the incoming request
166/// uses the PATCH HTTP method. Requests with other methods will be filtered out.
167///
168/// # Usage
169///
170/// ```rust
171/// use hyperlane::*;
172/// use hyperlane_macros::*;
173///
174/// #[patch]
175/// async fn handle_patch(ctx: Context) {
176///     // Function body
177/// }
178/// ```
179///
180/// The macro takes no parameters and should be applied directly to async functions
181/// that accept a `Context` parameter.
182#[proc_macro_attribute]
183pub fn patch(_attr: TokenStream, item: TokenStream) -> TokenStream {
184    patch_handler(item, Position::Prologue)
185}
186
187/// Restricts function execution to HTTP HEAD requests only.
188///
189/// This attribute macro ensures the decorated function only executes when the incoming request
190/// uses the HEAD HTTP method. Requests with other methods will be filtered out.
191///
192/// # Usage
193///
194/// ```rust
195/// use hyperlane::*;
196/// use hyperlane_macros::*;
197///
198/// #[head]
199/// async fn handle_head(ctx: Context) {
200///     // Function body
201/// }
202/// ```
203///
204/// The macro takes no parameters and should be applied directly to async functions
205/// that accept a `Context` parameter.
206#[proc_macro_attribute]
207pub fn head(_attr: TokenStream, item: TokenStream) -> TokenStream {
208    head_handler(item, Position::Prologue)
209}
210
211/// Restricts function execution to HTTP OPTIONS requests only.
212///
213/// This attribute macro ensures the decorated function only executes when the incoming request
214/// uses the OPTIONS HTTP method. Requests with other methods will be filtered out.
215///
216/// # Usage
217///
218/// ```rust
219/// use hyperlane::*;
220/// use hyperlane_macros::*;
221///
222/// #[options]
223/// async fn handle_options(ctx: Context) {
224///     // Function body
225/// }
226/// ```
227///
228/// The macro takes no parameters and should be applied directly to async functions
229/// that accept a `Context` parameter.
230#[proc_macro_attribute]
231pub fn options(_attr: TokenStream, item: TokenStream) -> TokenStream {
232    options_handler(item, Position::Prologue)
233}
234
235/// Restricts function execution to HTTP CONNECT requests only.
236///
237/// This attribute macro ensures the decorated function only executes when the incoming request
238/// uses the CONNECT HTTP method. Requests with other methods will be filtered out.
239///
240/// # Usage
241///
242/// ```rust
243/// use hyperlane::*;
244/// use hyperlane_macros::*;
245///
246/// #[connect]
247/// async fn handle_connect(ctx: Context) {
248///     // Function body
249/// }
250/// ```
251///
252/// The macro takes no parameters and should be applied directly to async functions
253/// that accept a `Context` parameter.
254#[proc_macro_attribute]
255pub fn connect(_attr: TokenStream, item: TokenStream) -> TokenStream {
256    connect_handler(item, Position::Prologue)
257}
258
259/// Restricts function execution to HTTP TRACE requests only.
260///
261/// This attribute macro ensures the decorated function only executes when the incoming request
262/// uses the TRACE HTTP method. Requests with other methods will be filtered out.
263///
264/// # Usage
265///
266/// ```rust
267/// use hyperlane::*;
268/// use hyperlane_macros::*;
269///
270/// #[trace]
271/// async fn handle_trace(ctx: Context) {
272///     // Function body
273/// }
274/// ```
275///
276/// The macro takes no parameters and should be applied directly to async functions
277/// that accept a `Context` parameter.
278#[proc_macro_attribute]
279pub fn trace(_attr: TokenStream, item: TokenStream) -> TokenStream {
280    trace_handler(item, Position::Prologue)
281}
282
283/// Allows function to handle multiple HTTP methods.
284///
285/// This attribute macro configures the decorated function to execute for any of the specified
286/// HTTP methods. Methods should be provided as a comma-separated list.
287///
288/// # Usage
289///
290/// ```rust
291/// use hyperlane::*;
292/// use hyperlane_macros::*;
293///
294/// #[methods(get, post)]
295/// async fn handle_get_post(ctx: Context) {
296///     // Function body
297/// }
298///
299/// #[methods(put, patch, delete)]
300/// async fn handle_modifications(ctx: Context) {
301///     // Function body
302/// }
303/// ```
304///
305/// The macro accepts a comma-separated list of HTTP method names (lowercase) and should be
306/// applied to async functions that accept a `Context` parameter.
307#[proc_macro_attribute]
308pub fn methods(attr: TokenStream, item: TokenStream) -> TokenStream {
309    methods_macro(attr, item, Position::Prologue)
310}
311
312/// Restricts function execution to WebSocket upgrade requests only.
313///
314/// This attribute macro ensures the decorated function only executes when the incoming request
315/// is a valid WebSocket upgrade request with proper request headers and protocol negotiation.
316///
317/// # Usage
318///
319/// ```rust
320/// use hyperlane::*;
321/// use hyperlane_macros::*;
322///
323/// #[ws]
324/// async fn handle_websocket(ctx: Context) {
325///     // WebSocket handling logic
326/// }
327/// ```
328///
329/// The macro takes no parameters and should be applied directly to async functions
330/// that accept a `Context` parameter.
331#[proc_macro_attribute]
332pub fn ws(_attr: TokenStream, item: TokenStream) -> TokenStream {
333    ws_macro(item, Position::Prologue)
334}
335
336/// Restricts function execution to standard HTTP requests only.
337///
338/// This attribute macro ensures the decorated function only executes for standard HTTP requests,
339/// excluding WebSocket upgrades and other protocol upgrade requests.
340///
341/// # Usage
342///
343/// ```rust
344/// use hyperlane::*;
345/// use hyperlane_macros::*;
346///
347/// #[http]
348/// async fn handle_http(ctx: Context) {
349///     // HTTP request handling logic
350/// }
351/// ```
352///
353/// The macro takes no parameters and should be applied directly to async functions
354/// that accept a `Context` parameter.
355#[proc_macro_attribute]
356pub fn http(_attr: TokenStream, item: TokenStream) -> TokenStream {
357    http_macro(item, Position::Prologue)
358}
359
360/// Sets the HTTP status code for the response.
361///
362/// This attribute macro configures the HTTP status code that will be sent with the response.
363/// The status code can be provided as a numeric literal or a global constant.
364///
365/// # Usage
366///
367/// ```rust
368/// use hyperlane::*;
369/// use hyperlane_macros::*;
370///
371/// const CUSTOM_STATUS: i32 = 418;
372///
373/// #[response_status_code(200)]
374/// async fn success_handler(ctx: Context) {
375///     // Response will have status code 200
376/// }
377///
378/// #[response_status_code(404)]
379/// async fn not_found_handler(ctx: Context) {
380///     // Response will have status code 404
381/// }
382///
383/// #[response_status_code(CUSTOM_STATUS)]
384/// async fn custom_handler(ctx: Context) {
385///     // Response will have status code from global constant
386/// }
387/// ```
388///
389/// The macro accepts a numeric HTTP status code or a global constant
390/// and should be applied to async functions that accept a `Context` parameter.
391#[proc_macro_attribute]
392pub fn response_status_code(attr: TokenStream, item: TokenStream) -> TokenStream {
393    response_status_code_macro(attr, item, Position::Prologue)
394}
395
396/// Sets the HTTP reason phrase for the response.
397///
398/// This attribute macro configures the HTTP reason phrase that accompanies the status code.
399/// The reason phrase can be provided as a string literal or a global constant.
400///
401/// # Usage
402///
403/// ```rust
404/// use hyperlane::*;
405/// use hyperlane_macros::*;
406///
407/// const CUSTOM_REASON: &str = "I'm a teapot";
408///
409/// #[response_reason_phrase("OK")]
410/// async fn success_handler(ctx: Context) {
411///     // Response will have reason phrase "OK"
412/// }
413///
414/// #[response_reason_phrase("Not Found")]
415/// async fn not_found_handler(ctx: Context) {
416///     // Response will have reason phrase "Not Found"
417/// }
418///
419/// #[response_reason_phrase(CUSTOM_REASON)]
420/// async fn custom_handler(ctx: Context) {
421///     // Response will have reason phrase from global constant
422/// }
423/// ```
424///
425/// The macro accepts a string literal or global constant for the reason phrase and should be
426/// applied to async functions that accept a `Context` parameter.
427#[proc_macro_attribute]
428pub fn response_reason_phrase(attr: TokenStream, item: TokenStream) -> TokenStream {
429    response_reason_phrase_macro(attr, item, Position::Prologue)
430}
431
432/// Sets or replaces a specific HTTP response header.
433///
434/// This attribute macro configures a specific HTTP response header that will be sent with the response.
435/// Both the header name and value can be provided as string literals or global constants.
436/// Use `"key", "value"` to set a header (add to existing headers) or `"key" => "value"` to replace a header (overwrite existing).
437///
438/// # Usage
439///
440/// ```rust
441/// use hyperlane::*;
442/// use hyperlane_macros::*;
443///
444/// const HEADER_NAME: &str = "X-Custom-Header";
445/// const HEADER_VALUE: &str = "custom-value";
446///
447/// #[response_header("Content-Type", "application/json")]
448/// async fn json_handler(ctx: Context) {
449///     // Response will have Content-Type header set to application/json
450/// }
451///
452/// #[response_header("X-Static-Header" => "static-value")]
453/// async fn set_header_handler(ctx: Context) {
454///     // Response will have static header replaced (overwrite existing)
455/// }
456///
457/// #[response_header(HEADER_NAME, HEADER_VALUE)]
458/// async fn dynamic_header_handler(ctx: Context) {
459///     // Response will have header from global constants
460/// }
461///
462/// #[response_header("Cache-Control" => "no-cache")]
463/// async fn set_cache_handler(ctx: Context) {
464///     // Response will have Cache-Control header replaced
465/// }
466///
467/// #[response_header("X-Add-Header", "add-value")]
468/// #[response_header("X-Set-Header" => "set-value")]
469/// async fn header_operations_handler(ctx: Context) {
470///     // Response will have X-Add-Header set and X-Set-Header replaced
471/// }
472/// ```
473///
474/// The macro accepts header name and header value, both can be string literals or global constants.
475/// Use `"key", "value"` for setting headers and `"key" => "value"` for replacing headers.
476/// Should be applied to async functions that accept a `Context` parameter.
477#[proc_macro_attribute]
478pub fn response_header(attr: TokenStream, item: TokenStream) -> TokenStream {
479    response_header_macro(attr, item, Position::Prologue)
480}
481
482/// Sets the HTTP response body.
483///
484/// This attribute macro configures the HTTP response body that will be sent with the response.
485/// The body content can be provided as a string literal or a global constant.
486///
487/// # Usage
488///
489/// ```rust
490/// use hyperlane::*;
491/// use hyperlane_macros::*;
492///
493/// const RESPONSE_DATA: &str = "Dynamic content from constant";
494///
495/// #[response_body("Hello, World!")]
496/// async fn hello_handler(ctx: Context) {
497///     // Response will have body "Hello, World!"
498/// }
499///
500/// #[response_body("{\"message\": \"success\"}")]
501/// async fn json_response_handler(ctx: Context) {
502///     // Response will have JSON body
503/// }
504///
505/// #[response_body(RESPONSE_DATA)]
506/// async fn dynamic_body_handler(ctx: Context) {
507///     // Response will have body from global constant
508/// }
509/// ```
510///
511/// The macro accepts a string literal or global constant for the response body and should be
512/// applied to async functions that accept a `Context` parameter.
513#[proc_macro_attribute]
514pub fn response_body(attr: TokenStream, item: TokenStream) -> TokenStream {
515    response_body_macro(attr, item, Position::Prologue)
516}
517
518/// Sets the HTTP response version.
519///
520/// This attribute macro configures the HTTP response version that will be sent with the response.
521/// The version can be provided as a variable or code block.
522///
523/// # Usage
524///
525/// ```rust
526/// use hyperlane::*;
527/// use hyperlane_macros::*;
528///
529/// #[response_version(HttpVersion::HTTP1_1)]
530/// async fn version_from_constant(ctx: Context) {
531///     // Response will have version from global constant
532/// }
533/// ```
534///
535/// The macro accepts a variable or code block for the response version and should be
536/// applied to async functions that accept a `Context` parameter.
537#[proc_macro_attribute]
538pub fn response_version(attr: TokenStream, item: TokenStream) -> TokenStream {
539    response_version_macro(attr, item, Position::Prologue)
540}
541
542/// Automatically sends the complete response after function execution.
543///
544/// This attribute macro ensures that the response (request headers and body) is automatically sent
545/// to the client after the function completes execution.
546///
547/// # Usage
548///
549/// ```rust
550/// use hyperlane::*;
551/// use hyperlane_macros::*;
552///
553/// #[send]
554/// async fn auto_send_handler(ctx: Context) {
555///     let _ = ctx.set_response_body("Hello World").await;
556///     // Response is automatically sent after function returns
557/// }
558/// ```
559///
560/// The macro takes no parameters and should be applied directly to async functions
561/// that accept a `Context` parameter.
562#[proc_macro_attribute]
563pub fn send(_attr: TokenStream, item: TokenStream) -> TokenStream {
564    send_macro(item, Position::Epilogue)
565}
566
567/// Automatically sends only the response body after function execution.
568///
569/// This attribute macro ensures that only the response body is automatically sent
570/// to the client after the function completes, handling request headers separately.
571///
572/// # Usage
573///
574/// ```rust
575/// use hyperlane::*;
576/// use hyperlane_macros::*;
577///
578/// #[send_body]
579/// async fn auto_send_body_handler(ctx: Context) {
580///     let _ = ctx.set_response_body("Response body content").await;
581///     // Only response body is automatically sent after function returns
582/// }
583/// ```
584///
585/// The macro takes no parameters and should be applied directly to async functions
586/// that accept a `Context` parameter.
587#[proc_macro_attribute]
588pub fn send_body(_attr: TokenStream, item: TokenStream) -> TokenStream {
589    send_body_macro(item, Position::Epilogue)
590}
591
592/// Sends the complete response with data after function execution.
593///
594/// This attribute macro ensures that the response (request headers and body) is automatically sent
595/// to the client after the function completes execution, with the specified data.
596///
597/// # Usage
598///
599/// ```rust
600/// use hyperlane::*;
601/// use hyperlane_macros::*;
602///
603/// #[send_with_data("Hello, World!")]
604/// async fn auto_send_with_data_handler(ctx: Context) {
605///     // Response is automatically sent with the specified data after function returns
606/// }
607/// ```
608///
609/// The macro accepts data to send and should be applied to async functions
610/// that accept a `Context` parameter.
611#[proc_macro_attribute]
612pub fn send_with_data(attr: TokenStream, item: TokenStream) -> TokenStream {
613    send_with_data_macro(attr, item, Position::Epilogue)
614}
615
616/// Sends the complete response exactly once after function execution.
617///
618/// This attribute macro ensures that the response is sent exactly once to the client,
619/// preventing multiple response transmissions for single-use scenarios.
620///
621/// # Usage
622///
623/// ```rust
624/// use hyperlane::*;
625/// use hyperlane_macros::*;
626///
627/// #[send_once]
628/// async fn send_once_handler(ctx: Context) {
629///     let _ = ctx.set_response_body("One-time response").await;
630///     // Response is sent exactly once after function returns
631/// }
632/// ```
633///
634/// The macro takes no parameters and should be applied directly to async functions
635/// that accept a `Context` parameter.
636#[proc_macro_attribute]
637pub fn send_once(_attr: TokenStream, item: TokenStream) -> TokenStream {
638    send_once_macro(item, Position::Epilogue)
639}
640
641/// Sends only the response body exactly once after function execution.
642///
643/// This attribute macro ensures that the response body is sent exactly once to the client,
644/// preventing multiple body transmissions for single-use scenarios.
645///
646/// # Usage
647///
648/// ```rust
649/// use hyperlane::*;
650/// use hyperlane_macros::*;
651///
652/// #[send_body_once]
653/// async fn send_body_once_handler(ctx: Context) {
654///     let _ = ctx.set_response_body("One-time body content").await;
655///     // Response body is sent exactly once after function returns
656/// }
657/// ```
658///
659/// The macro takes no parameters and should be applied directly to async functions
660/// that accept a `Context` parameter.
661#[proc_macro_attribute]
662pub fn send_body_once(_attr: TokenStream, item: TokenStream) -> TokenStream {
663    send_body_once_macro(item, Position::Epilogue)
664}
665
666/// Sends the complete response exactly once with data after function execution.
667///
668/// This attribute macro ensures that the response is sent exactly once to the client,
669/// preventing multiple response transmissions for single-use scenarios, with the specified data.
670///
671/// # Usage
672///
673/// ```rust
674/// use hyperlane::*;
675/// use hyperlane_macros::*;
676///
677/// #[send_once_with_data("One-time response")]
678/// async fn send_once_with_data_handler(ctx: Context) {
679///     // Response is sent exactly once with the specified data after function returns
680/// }
681/// ```
682///
683/// The macro accepts data to send and should be applied to async functions
684/// that accept a `Context` parameter.
685#[proc_macro_attribute]
686pub fn send_once_with_data(attr: TokenStream, item: TokenStream) -> TokenStream {
687    send_once_with_data_macro(attr, item, Position::Epilogue)
688}
689
690/// Flushes the response stream after function execution.
691///
692/// This attribute macro ensures that the response stream is flushed to guarantee immediate
693/// data transmission, forcing any buffered response data to be sent to the client.
694///
695/// # Usage
696///
697/// ```rust
698/// use hyperlane::*;
699/// use hyperlane_macros::*;
700///
701/// #[flush]
702/// async fn flush_handler(ctx: Context) {
703///     let _ = ctx.set_response_body("Immediate response").await;
704///     // Response stream is flushed after function returns
705/// }
706/// ```
707///
708/// The macro takes no parameters and should be applied directly to async functions
709/// that accept a `Context` parameter.
710#[proc_macro_attribute]
711pub fn flush(_attr: TokenStream, item: TokenStream) -> TokenStream {
712    flush_macro(item, Position::Prologue)
713}
714
715/// Handles aborted request scenarios.
716///
717/// This attribute macro configures the function to handle cases where the client has
718/// aborted the request, providing appropriate handling for interrupted or cancelled requests.
719///
720/// # Usage
721///
722/// ```rust
723/// use hyperlane::*;
724/// use hyperlane_macros::*;
725///
726/// #[aborted]
727/// async fn handle_aborted(ctx: Context) {
728///     // Handle aborted request logic
729/// }
730/// ```
731///
732/// The macro takes no parameters and should be applied directly to async functions
733/// that accept a `Context` parameter.
734#[proc_macro_attribute]
735pub fn aborted(_attr: TokenStream, item: TokenStream) -> TokenStream {
736    aborted_macro(item, Position::Prologue)
737}
738
739/// Handles closed connection scenarios.
740///
741/// This attribute macro configures the function to handle cases where the connection
742/// has been closed, providing appropriate handling for terminated or disconnected connections.
743///
744/// # Usage
745///
746/// ```rust
747/// use hyperlane::*;
748/// use hyperlane_macros::*;
749///
750/// #[closed]
751/// async fn handle_closed(ctx: Context) {
752///     // Handle closed connection logic
753/// }
754/// ```
755///
756/// The macro takes no parameters and should be applied directly to async functions
757/// that accept a `Context` parameter.
758#[proc_macro_attribute]
759pub fn closed(_attr: TokenStream, item: TokenStream) -> TokenStream {
760    closed_macro(item, Position::Prologue)
761}
762
763/// Restricts function execution to HTTP/2 Cleartext (h2c) requests only.
764///
765/// This attribute macro ensures the decorated function only executes for HTTP/2 cleartext
766/// requests that use the h2c upgrade mechanism.
767///
768/// # Usage
769///
770/// ```rust
771/// use hyperlane::*;
772/// use hyperlane_macros::*;
773///
774/// #[h2c]
775/// async fn handle_h2c(ctx: Context) {
776///     // Handle HTTP/2 cleartext requests
777/// }
778/// ```
779///
780/// The macro takes no parameters and should be applied directly to async functions
781/// that accept a `Context` parameter.
782#[proc_macro_attribute]
783pub fn h2c(_attr: TokenStream, item: TokenStream) -> TokenStream {
784    h2c_macro(item, Position::Prologue)
785}
786
787/// Restricts function execution to HTTP/0.9 requests only.
788///
789/// This attribute macro ensures the decorated function only executes for HTTP/0.9
790/// protocol requests, the earliest version of the HTTP protocol.
791///
792/// # Usage
793///
794/// ```rust
795/// use hyperlane::*;
796/// use hyperlane_macros::*;
797///
798/// #[http0_9]
799/// async fn handle_http09(ctx: Context) {
800///     // Handle HTTP/0.9 requests
801/// }
802/// ```
803///
804/// The macro takes no parameters and should be applied directly to async functions
805/// that accept a `Context` parameter.
806#[proc_macro_attribute]
807pub fn http0_9(_attr: TokenStream, item: TokenStream) -> TokenStream {
808    http0_9_macro(item, Position::Prologue)
809}
810
811/// Restricts function execution to HTTP/1.0 requests only.
812///
813/// This attribute macro ensures the decorated function only executes for HTTP/1.0
814/// protocol requests.
815///
816/// # Usage
817///
818/// ```rust
819/// use hyperlane::*;
820/// use hyperlane_macros::*;
821///
822/// #[http1_0]
823/// async fn handle_http10(ctx: Context) {
824///     // Handle HTTP/1.0 requests
825/// }
826/// ```
827///
828/// The macro takes no parameters and should be applied directly to async functions
829/// that accept a `Context` parameter.
830#[proc_macro_attribute]
831pub fn http1_0(_attr: TokenStream, item: TokenStream) -> TokenStream {
832    http1_0_macro(item, Position::Prologue)
833}
834
835/// Restricts function execution to HTTP/1.1 requests only.
836///
837/// This attribute macro ensures the decorated function only executes for HTTP/1.1
838/// protocol requests.
839///
840/// # Usage
841///
842/// ```rust
843/// use hyperlane::*;
844/// use hyperlane_macros::*;
845///
846/// #[http1_1]
847/// async fn handle_http11(ctx: Context) {
848///     // Handle HTTP/1.1 requests
849/// }
850/// ```
851///
852/// The macro takes no parameters and should be applied directly to async functions
853/// that accept a `Context` parameter.
854#[proc_macro_attribute]
855pub fn http1_1(_attr: TokenStream, item: TokenStream) -> TokenStream {
856    http1_1_macro(item, Position::Prologue)
857}
858
859/// Restricts function execution to HTTP/1.1 or higher protocol versions.
860///
861/// This attribute macro ensures the decorated function only executes for HTTP/1.1
862/// or newer protocol versions, including HTTP/2, HTTP/3, and future versions.
863///
864/// # Usage
865///
866/// ```rust
867/// use hyperlane::*;
868/// use hyperlane_macros::*;
869///
870/// #[http1_1_or_higher]
871/// async fn handle_modern_http(ctx: Context) {
872///     // Handle HTTP/1.1, HTTP/2, HTTP/3, etc.
873/// }
874/// ```
875///
876/// The macro takes no parameters and should be applied directly to async functions
877/// that accept a `Context` parameter.
878#[proc_macro_attribute]
879pub fn http1_1_or_higher(_attr: TokenStream, item: TokenStream) -> TokenStream {
880    http1_1_or_higher_macro(item, Position::Prologue)
881}
882
883/// Restricts function execution to HTTP/2 requests only.
884///
885/// This attribute macro ensures the decorated function only executes for HTTP/2
886/// protocol requests.
887///
888/// # Usage
889///
890/// ```rust
891/// use hyperlane::*;
892/// use hyperlane_macros::*;
893///
894/// #[http2]
895/// async fn handle_http2(ctx: Context) {
896///     // Handle HTTP/2 requests
897/// }
898/// ```
899///
900/// The macro takes no parameters and should be applied directly to async functions
901/// that accept a `Context` parameter.
902#[proc_macro_attribute]
903pub fn http2(_attr: TokenStream, item: TokenStream) -> TokenStream {
904    http2_macro(item, Position::Prologue)
905}
906
907/// Restricts function execution to HTTP/3 requests only.
908///
909/// This attribute macro ensures the decorated function only executes for HTTP/3
910/// protocol requests, the latest version of the HTTP protocol.
911///
912/// # Usage
913///
914/// ```rust
915/// use hyperlane::*;
916/// use hyperlane_macros::*;
917///
918/// #[http3]
919/// async fn handle_http3(ctx: Context) {
920///     // Handle HTTP/3 requests
921/// }
922/// ```
923///
924/// The macro takes no parameters and should be applied directly to async functions
925/// that accept a `Context` parameter.
926#[proc_macro_attribute]
927pub fn http3(_attr: TokenStream, item: TokenStream) -> TokenStream {
928    http3_macro(item, Position::Prologue)
929}
930
931/// Restricts function execution to TLS-encrypted requests only.
932///
933/// This attribute macro ensures the decorated function only executes for requests
934/// that use TLS/SSL encryption on the connection.
935///
936/// # Usage
937///
938/// ```rust
939/// use hyperlane::*;
940/// use hyperlane_macros::*;
941///
942/// #[tls]
943/// async fn handle_secure(ctx: Context) {
944///     // Handle TLS-encrypted requests only
945/// }
946/// ```
947///
948/// The macro takes no parameters and should be applied directly to async functions
949/// that accept a `Context` parameter.
950#[proc_macro_attribute]
951pub fn tls(_attr: TokenStream, item: TokenStream) -> TokenStream {
952    tls_macro(item, Position::Prologue)
953}
954
955/// Filters requests based on a boolean condition.
956///
957/// The function continues execution only if the provided code block returns `true`.
958///
959/// # Usage
960///
961/// ```rust
962/// use hyperlane::*;
963/// use hyperlane_macros::*;
964///
965/// #[filter(ctx.get_request().await.is_ws())]
966/// async fn handle_ws(ctx: Context) {
967///     // This code will only run for WebSocket requests.
968/// }
969/// ```
970#[proc_macro_attribute]
971pub fn filter(attr: TokenStream, item: TokenStream) -> TokenStream {
972    filter_macro(attr, item, Position::Prologue)
973}
974
975/// Rejects requests based on a boolean condition.
976///
977/// The function continues execution only if the provided code block returns `false`.
978///
979/// # Usage
980///
981/// ```rust
982/// use hyperlane::*;
983/// use hyperlane_macros::*;
984///
985/// #[reject(ctx.get_request().await.is_http())]
986/// async fn handle_non_http(ctx: Context) {
987///     // This code will not run for HTTP requests.
988/// }
989/// ```
990#[proc_macro_attribute]
991pub fn reject(attr: TokenStream, item: TokenStream) -> TokenStream {
992    reject_macro(attr, item, Position::Prologue)
993}
994
995/// Restricts function execution to requests with a specific host.
996///
997/// This attribute macro ensures the decorated function only executes when the incoming request
998/// has a host header that matches the specified value. Requests with different or missing host headers will be filtered out.
999///
1000/// # Usage
1001///
1002/// ```rust
1003/// use hyperlane::*;
1004/// use hyperlane_macros::*;
1005///
1006/// #[host("localhost")]
1007/// async fn handle_example_com(ctx: Context) {
1008///     // Function body for localhost requests
1009/// }
1010///
1011/// #[host("api.localhost")]
1012/// async fn handle_api_subdomain(ctx: Context) {
1013///     // Function body for api.localhost requests
1014/// }
1015/// ```
1016///
1017/// The macro accepts a string literal specifying the expected host value and should be
1018/// applied to async functions that accept a `Context` parameter.
1019#[proc_macro_attribute]
1020pub fn host(attr: TokenStream, item: TokenStream) -> TokenStream {
1021    host_macro(attr, item, Position::Prologue)
1022}
1023
1024/// Reject requests that have no host header.
1025///
1026/// This attribute macro ensures the decorated function only executes when the incoming request
1027/// has a host header present. Requests without a host header will be filtered out.
1028///
1029/// # Usage
1030///
1031/// ```rust
1032/// use hyperlane::*;
1033/// use hyperlane_macros::*;
1034///
1035/// #[reject_host("localhost")]
1036/// async fn handle_with_host(ctx: Context) {
1037///     // Function body for requests with host header
1038/// }
1039/// ```
1040///
1041/// The macro takes no parameters and should be applied directly to async functions
1042/// that accept a `Context` parameter.
1043#[proc_macro_attribute]
1044pub fn reject_host(attr: TokenStream, item: TokenStream) -> TokenStream {
1045    reject_host_macro(attr, item, Position::Prologue)
1046}
1047
1048/// Restricts function execution to requests with a specific referer.
1049///
1050/// This attribute macro ensures the decorated function only executes when the incoming request
1051/// has a referer header that matches the specified value. Requests with different or missing referer headers will be filtered out.
1052///
1053/// # Usage
1054///
1055/// ```rust
1056/// use hyperlane::*;
1057/// use hyperlane_macros::*;
1058///
1059/// #[referer("http://localhost")]
1060/// async fn handle_example_referer(ctx: Context) {
1061///     // Function body for requests from localhost
1062/// }
1063///
1064/// #[referer("https://api.localhost")]
1065/// async fn handle_api_referer(ctx: Context) {
1066///     // Function body for requests from api.localhost
1067/// }
1068/// ```
1069///
1070/// The macro accepts a string literal specifying the expected referer value and should be
1071/// applied to async functions that accept a `Context` parameter.
1072#[proc_macro_attribute]
1073pub fn referer(attr: TokenStream, item: TokenStream) -> TokenStream {
1074    referer_macro(attr, item, Position::Prologue)
1075}
1076
1077/// Reject requests that have a specific referer header.
1078///
1079/// This attribute macro ensures the decorated function only executes when the incoming request
1080/// does not have a referer header that matches the specified value. Requests with the matching referer header will be filtered out.
1081///
1082/// # Usage
1083///
1084/// ```rust
1085/// use hyperlane::*;
1086/// use hyperlane_macros::*;
1087///
1088/// #[reject_referer("http://localhost")]
1089/// async fn handle_without_spam_referer(ctx: Context) {
1090///     // Function body for requests not from localhost
1091/// }
1092/// ```
1093///
1094/// The macro accepts a string literal specifying the referer value to filter out and should be
1095/// applied to async functions that accept a `Context` parameter.
1096#[proc_macro_attribute]
1097pub fn reject_referer(attr: TokenStream, item: TokenStream) -> TokenStream {
1098    reject_referer_macro(attr, item, Position::Prologue)
1099}
1100
1101/// Executes a specified function before the main handler function.
1102///
1103/// This attribute macro configures a pre-execution hook that runs before the main function logic.
1104/// The specified hook function will be called first, followed by the main function execution.
1105///
1106/// # Usage
1107///
1108/// ```rust
1109/// use hyperlane::*;
1110/// use hyperlane_macros::*;
1111///
1112/// #[get]
1113/// async fn prologue_handler(ctx: Context) {
1114///     // Pre-execution logic
1115/// }
1116///
1117/// #[prologue_hook(prologue_handler)]
1118/// async fn main_handler(ctx: Context) {
1119///     // Main function logic (runs after prologue_handler)
1120/// }
1121/// ```
1122///
1123/// The macro accepts a function name as parameter. Both the hook function and main function
1124/// must accept a `Context` parameter. Avoid combining this macro with other macros on the
1125/// same function to prevent macro expansion conflicts.
1126#[proc_macro_attribute]
1127pub fn prologue_hook(attr: TokenStream, item: TokenStream) -> TokenStream {
1128    prologue_hook_macro(attr, item, Position::Prologue)
1129}
1130
1131/// Executes a specified function after the main handler function.
1132///
1133/// This attribute macro configures a post-execution hook that runs after the main function logic.
1134/// The main function will execute first, followed by the specified hook function.
1135///
1136/// # Usage
1137///
1138/// ```rust
1139/// use hyperlane::*;
1140/// use hyperlane_macros::*;
1141///
1142/// #[send]
1143/// async fn epilogue_handler(ctx: Context) {
1144///     // Post-execution logic
1145/// }
1146///
1147/// #[epilogue_hook(epilogue_handler)]
1148/// async fn main_handler(ctx: Context) {
1149///     // Main function logic (runs before epilogue_handler)
1150/// }
1151/// ```
1152///
1153/// The macro accepts a function name as parameter. Both the hook function and main function
1154/// must accept a `Context` parameter. Avoid combining this macro with other macros on the
1155/// same function to prevent macro expansion conflicts.
1156#[proc_macro_attribute]
1157pub fn epilogue_hook(attr: TokenStream, item: TokenStream) -> TokenStream {
1158    epilogue_hook_macro(attr, item, Position::Epilogue)
1159}
1160
1161/// Extracts the raw request body into a specified variable.
1162///
1163/// This attribute macro extracts the raw request body content into a variable
1164/// with the fixed type `RequestBody`. The body content is not parsed or deserialized.
1165///
1166/// # Usage
1167///
1168/// ```rust
1169/// use hyperlane::*;
1170/// use hyperlane_macros::*;
1171///
1172/// #[request_body(raw_body)]
1173/// async fn handle_raw_body(ctx: Context) {
1174///     // Use the raw request body
1175///     let body_content = raw_body;
1176/// }
1177/// ```
1178///
1179/// The macro accepts only a variable name. The variable will be available
1180/// in the function scope as a `RequestBody` type.
1181#[proc_macro_attribute]
1182pub fn request_body(attr: TokenStream, item: TokenStream) -> TokenStream {
1183    request_body_macro(attr, item, Position::Prologue)
1184}
1185
1186/// Parses the request body as JSON into a specified variable and type.
1187///
1188/// This attribute macro extracts and deserializes the request body content as JSON into a variable
1189/// with the specified type. The body content is parsed as JSON using serde.
1190///
1191/// # Usage
1192///
1193/// ```rust
1194/// use hyperlane::*;
1195/// use hyperlane_macros::*;
1196/// use serde::Deserialize;
1197///
1198/// #[derive(Deserialize, Clone)]
1199/// struct UserData {
1200///     name: String,
1201///     age: u32,
1202/// }
1203///
1204/// #[request_body_json(user_data: UserData)]
1205/// async fn handle_user(ctx: Context) {
1206///     if let Ok(data) = user_data {
1207///         // Use the parsed user data
1208///     }
1209/// }
1210/// ```
1211///
1212/// The macro accepts a variable name and type in the format `variable_name: Type`.
1213/// The variable will be available in the function scope as a `Result<Type, JsonError>`.
1214#[proc_macro_attribute]
1215pub fn request_body_json(attr: TokenStream, item: TokenStream) -> TokenStream {
1216    request_body_json_macro(attr, item, Position::Prologue)
1217}
1218
1219/// Extracts a specific attribute value into a variable.
1220///
1221/// This attribute macro retrieves a specific attribute by key and makes it available
1222/// as a typed variable from the request context.
1223///
1224/// # Usage
1225///
1226/// ```rust
1227/// use hyperlane::*;
1228/// use hyperlane_macros::*;
1229/// use serde::Deserialize;
1230///
1231/// const USER_KEY: &str = "user_data";
1232///
1233/// #[derive(Deserialize, Clone)]
1234/// struct User {
1235///     id: u64,
1236///     name: String,
1237/// }
1238///
1239/// #[attribute(USER_KEY => user: User)]
1240/// async fn handle_with_attribute(ctx: Context) {
1241///     if let Some(user_data) = user {
1242///         // Use the extracted attribute
1243///     }
1244/// }
1245/// ```
1246///
1247/// The macro accepts a key-to-variable mapping in the format `key => variable_name: Type`.
1248/// The variable will be available as an `Option<Type>` in the function scope.
1249#[proc_macro_attribute]
1250pub fn attribute(attr: TokenStream, item: TokenStream) -> TokenStream {
1251    attribute_macro(attr, item, Position::Prologue)
1252}
1253
1254/// Extracts all attributes into a HashMap variable.
1255///
1256/// This attribute macro retrieves all available attributes from the request context
1257/// and makes them available as a HashMap for comprehensive attribute access.
1258///
1259/// # Usage
1260///
1261/// ```rust
1262/// use hyperlane::*;
1263/// use hyperlane_macros::*;
1264///
1265/// #[attributes(all_attrs)]
1266/// async fn handle_with_all_attributes(ctx: Context) {
1267///     for (key, value) in all_attrs {
1268///         // Process each attribute
1269///     }
1270/// }
1271/// ```
1272///
1273/// The macro accepts a variable name that will contain a HashMap of all attributes.
1274/// The variable will be available as a HashMap in the function scope.
1275#[proc_macro_attribute]
1276pub fn attributes(attr: TokenStream, item: TokenStream) -> TokenStream {
1277    attributes_macro(attr, item, Position::Prologue)
1278}
1279
1280/// Extracts a specific route parameter into a variable.
1281///
1282/// This attribute macro retrieves a specific route parameter by key and makes it
1283/// available as a variable. Route parameters are extracted from the URL path segments.
1284///
1285/// # Usage
1286///
1287/// ```rust
1288/// use hyperlane::*;
1289/// use hyperlane_macros::*;
1290///
1291/// // For route like "/users/{id}"
1292/// #[route_param("id" => user_id)]
1293/// async fn get_user(ctx: Context) {
1294///     if let Some(id) = user_id {
1295///         // Use the route parameter
1296///     }
1297/// }
1298/// ```
1299///
1300/// The macro accepts a key-to-variable mapping in the format `"key" => variable_name`.
1301/// The variable will be available as an `Option<String>` in the function scope.
1302#[proc_macro_attribute]
1303pub fn route_param(attr: TokenStream, item: TokenStream) -> TokenStream {
1304    route_param_macro(attr, item, Position::Prologue)
1305}
1306
1307/// Extracts all route parameters into a collection variable.
1308///
1309/// This attribute macro retrieves all available route parameters from the URL path
1310/// and makes them available as a collection for comprehensive route parameter access.
1311///
1312/// # Usage
1313///
1314/// ```rust
1315/// use hyperlane::*;
1316/// use hyperlane_macros::*;
1317///
1318/// // For route like "/users/{id}/posts/{epilogue_id}"
1319/// #[route_params(params)]
1320/// async fn handle_nested_route(ctx: Context) {
1321///     for (key, value) in params {
1322///         // Process each route parameter
1323///     }
1324/// }
1325/// ```
1326///
1327/// The macro accepts a variable name that will contain all route parameters.
1328/// The variable will be available as a collection in the function scope.
1329#[proc_macro_attribute]
1330pub fn route_params(attr: TokenStream, item: TokenStream) -> TokenStream {
1331    route_params_macro(attr, item, Position::Prologue)
1332}
1333
1334/// Extracts a specific request query parameter into a variable.
1335///
1336/// This attribute macro retrieves a specific request query parameter by key and makes it
1337/// available as a variable. Query parameters are extracted from the URL request query string.
1338///
1339/// # Usage
1340///
1341/// ```rust
1342/// use hyperlane::*;
1343/// use hyperlane_macros::*;
1344///
1345/// // For URL like "/search?q=rust&limit=10"
1346/// #[request_query("q" => search_term)]
1347/// async fn search(ctx: Context) {
1348///     if let Some(term) = search_term {
1349///         // Use the request query parameter
1350///     }
1351/// }
1352/// ```
1353///
1354/// The macro accepts a key-to-variable mapping in the format `"key" => variable_name`.
1355/// The variable will be available as an `Option<String>` in the function scope.
1356#[proc_macro_attribute]
1357pub fn request_query(attr: TokenStream, item: TokenStream) -> TokenStream {
1358    request_query_macro(attr, item, Position::Prologue)
1359}
1360
1361/// Extracts all request query parameters into a collection variable.
1362///
1363/// This attribute macro retrieves all available request query parameters from the URL request query string
1364/// and makes them available as a collection for comprehensive request query parameter access.
1365///
1366/// # Usage
1367///
1368/// ```rust
1369/// use hyperlane::*;
1370/// use hyperlane_macros::*;
1371///
1372/// // For URL like "/search?q=rust&limit=10&sort=date"
1373/// #[request_querys(all_params)]
1374/// async fn search_with_params(ctx: Context) {
1375///     for (key, value) in all_params {
1376///         // Process each request query parameter
1377///     }
1378/// }
1379/// ```
1380///
1381/// The macro accepts a variable name that will contain all request query parameters.
1382/// The variable will be available as a collection in the function scope.
1383#[proc_macro_attribute]
1384pub fn request_querys(attr: TokenStream, item: TokenStream) -> TokenStream {
1385    request_querys_macro(attr, item, Position::Prologue)
1386}
1387
1388/// Extracts a specific HTTP request header into a variable.
1389///
1390/// This attribute macro retrieves a specific HTTP request header by name and makes it
1391/// available as a variable. Header values are extracted from the request request headers collection.
1392///
1393/// # Usage
1394///
1395/// ```rust
1396/// use hyperlane::*;
1397/// use hyperlane_macros::*;
1398///
1399/// #[request_header(HOST => host_request_header)]
1400/// async fn handle_with_host(ctx: Context) {
1401///     if let Some(host) = host_request_header {
1402///         // Use the host request_header value
1403///     }
1404/// }
1405///
1406/// #[request_header("Content-Type" => content_type)]
1407/// async fn handle_with_content_type(ctx: Context) {
1408///     if let Some(ct) = content_type {
1409///         // Use the content type request_header
1410///     }
1411/// }
1412/// ```
1413///
1414/// The macro accepts a request header name-to-variable mapping in the format `HEADER_NAME => variable_name`
1415/// or `"Header-Name" => variable_name`. The variable will be available as an `Option<String>`.
1416#[proc_macro_attribute]
1417pub fn request_header(attr: TokenStream, item: TokenStream) -> TokenStream {
1418    request_header_macro(attr, item, Position::Prologue)
1419}
1420
1421/// Extracts all HTTP request headers into a collection variable.
1422///
1423/// This attribute macro retrieves all available HTTP request headers from the request
1424/// and makes them available as a collection for comprehensive request header access.
1425///
1426/// # Usage
1427///
1428/// ```rust
1429/// use hyperlane::*;
1430/// use hyperlane_macros::*;
1431///
1432/// #[request_headers(all_request_headers)]
1433/// async fn handle_with_all_request_headers(ctx: Context) {
1434///     for (name, value) in all_request_headers {
1435///         // Process each request_header
1436///     }
1437/// }
1438/// ```
1439///
1440/// The macro accepts a variable name that will contain all HTTP request headers.
1441/// The variable will be available as a collection in the function scope.
1442#[proc_macro_attribute]
1443pub fn request_headers(attr: TokenStream, item: TokenStream) -> TokenStream {
1444    request_headers_macro(attr, item, Position::Prologue)
1445}
1446
1447/// Extracts a specific cookie value or all cookies into a variable.
1448///
1449/// This attribute macro supports two syntaxes:
1450/// 1. `cookie(key => variable_name)` - Extract a specific cookie value by key
1451/// 2. `cookie(variable_name)` - Extract all cookies as a raw string
1452///
1453/// # Usage
1454///
1455/// ```rust
1456/// use hyperlane::*;
1457/// use hyperlane_macros::*;
1458///
1459/// #[request_cookie("session_id" => session_cookie_opt)]
1460/// async fn handle_with_session(ctx: Context) {
1461///     if let Some(session) = session_cookie_opt {
1462///         // Use the session cookie value
1463///     }
1464/// }
1465/// ```
1466///
1467/// For specific cookie extraction, the variable will be available as `Option<String>`.
1468/// For all cookies extraction, the variable will be available as `String`.
1469#[proc_macro_attribute]
1470pub fn request_cookie(attr: TokenStream, item: TokenStream) -> TokenStream {
1471    request_cookie_macro(attr, item, Position::Prologue)
1472}
1473
1474/// Extracts all cookies as a raw string into a variable.
1475///
1476/// This attribute macro retrieves the entire Cookie header from the request and makes it
1477/// available as a String variable. If no Cookie header is present, an empty string is used.
1478///
1479/// # Usage
1480///
1481/// ```rust
1482/// use hyperlane::*;
1483/// use hyperlane_macros::*;
1484///
1485/// #[request_cookies(cookie_value)]
1486/// async fn handle_with_cookies(ctx: Context) {
1487///     // Use the cookie value
1488///     if !cookie_value.is_empty() {
1489///         // Process cookie data
1490///     }
1491/// }
1492/// ```
1493///
1494/// The macro accepts a variable name that will contain the Cookie header value.
1495/// The variable will be available as a String in the function scope.
1496#[proc_macro_attribute]
1497pub fn request_cookies(attr: TokenStream, item: TokenStream) -> TokenStream {
1498    request_cookies_macro(attr, item, Position::Prologue)
1499}
1500
1501/// Extracts the HTTP request version into a variable.
1502///
1503/// This attribute macro retrieves the HTTP version from the request and makes it
1504/// available as a variable. The version represents the HTTP protocol version used.
1505///
1506/// # Usage
1507///
1508/// ```rust
1509/// use hyperlane::*;
1510/// use hyperlane_macros::*;
1511///
1512/// #[request_version(http_version)]
1513/// async fn handle_with_version(ctx: Context) {
1514///     // Use the HTTP version
1515/// }
1516/// ```
1517///
1518/// The macro accepts a variable name that will contain the HTTP request version.
1519/// The variable will be available as a RequestVersion type in the function scope.
1520#[proc_macro_attribute]
1521pub fn request_version(attr: TokenStream, item: TokenStream) -> TokenStream {
1522    request_version_macro(attr, item, Position::Prologue)
1523}
1524
1525/// Extracts the HTTP request path into a variable.
1526///
1527/// This attribute macro retrieves the request path from the HTTP request and makes it
1528/// available as a variable. The path represents the URL path portion of the request.
1529///
1530/// # Usage
1531///
1532/// ```rust
1533/// use hyperlane::*;
1534/// use hyperlane_macros::*;
1535///
1536/// #[request_path(request_path)]
1537/// async fn handle_with_path(ctx: Context) {
1538///     // Use the request path
1539///     if request_path.starts_with("/api/") {
1540///         // Handle API requests
1541///     }
1542/// }
1543/// ```
1544///
1545/// The macro accepts a variable name that will contain the HTTP request path.
1546/// The variable will be available as a RequestPath type in the function scope.
1547#[proc_macro_attribute]
1548pub fn request_path(attr: TokenStream, item: TokenStream) -> TokenStream {
1549    request_path_macro(attr, item, Position::Prologue)
1550}
1551
1552/// Creates a new instance of a specified type with a given variable name.
1553///
1554/// This attribute macro generates an instance initialization at the beginning of the function.
1555///
1556/// # Usage
1557///
1558/// ```rust
1559/// use hyperlane::*;
1560/// use hyperlane_macros::*;
1561///
1562/// #[hyperlane(server: Server)]
1563/// #[tokio::main]
1564/// async fn main() {
1565///     // `server` is now available as: `let server: Server = Server::new().await;`
1566///     // The function body can now use `server`.
1567/// }
1568/// ```
1569///
1570/// The macro accepts a `variable_name: Type` pair.
1571/// The variable will be available as an instance of the specified type in the function scope.
1572#[proc_macro_attribute]
1573pub fn hyperlane(attr: TokenStream, item: TokenStream) -> TokenStream {
1574    hyperlane_macro(attr, item)
1575}
1576
1577/// Registers a function as a route handler.
1578///
1579/// This attribute macro registers the decorated function as a route handler for a given path.
1580/// This macro requires the `#[hyperlane(server: Server)]` macro to be used to define the server instance.
1581///
1582/// # Usage
1583///
1584/// ```rust
1585/// use hyperlane::*;
1586/// use hyperlane_macros::*;
1587///
1588/// #[route("/")]
1589/// async fn route(ctx: Context) {
1590///     // function body
1591/// }
1592/// ```
1593///
1594/// # Parameters
1595///
1596/// - `path`: String literal defining the route path
1597///
1598/// # Dependencies
1599///
1600/// This macro depends on the `#[hyperlane(server: Server)]` macro to define the server instance.
1601#[proc_macro_attribute]
1602pub fn route(attr: TokenStream, item: TokenStream) -> TokenStream {
1603    route_macro(attr, item)
1604}
1605
1606/// Registers a function as a request middleware.
1607///
1608/// This attribute macro registers the decorated function to be executed as a middleware
1609/// for incoming requests. This macro requires the `#[hyperlane(server: Server)]` macro to be used to define the server instance.
1610///
1611/// # Note
1612///
1613/// If an order parameter is not specified, the hook will have a higher priority than hooks with a specified order.
1614///
1615/// # Usage
1616///
1617/// ```rust
1618/// use hyperlane::*;
1619/// use hyperlane_macros::*;
1620///
1621/// #[request_middleware]
1622/// #[request_middleware(1)]
1623/// #[request_middleware("2")]
1624/// async fn log_request(ctx: Context) {
1625///     // Middleware logic
1626/// }
1627/// ```
1628///
1629/// # Dependencies
1630///
1631/// This macro depends on the `#[hyperlane(server: Server)]` macro to define the server instance.
1632#[proc_macro_attribute]
1633pub fn request_middleware(attr: TokenStream, item: TokenStream) -> TokenStream {
1634    request_middleware_macro(attr, item)
1635}
1636
1637/// Registers a function as a response middleware.
1638///
1639/// This attribute macro registers the decorated function to be executed as a middleware
1640/// for outgoing responses. This macro requires the `#[hyperlane(server: Server)]` macro to be used to define the server instance.
1641///
1642/// # Note
1643///
1644/// If an order parameter is not specified, the hook will have a higher priority than hooks with a specified order.
1645///
1646/// # Usage
1647///
1648/// ```rust
1649/// use hyperlane::*;
1650/// use hyperlane_macros::*;
1651///
1652/// #[response_middleware]
1653/// #[response_middleware(1)]
1654/// #[response_middleware("2")]
1655/// async fn add_custom_header(ctx: Context) {
1656///     // Middleware logic
1657/// }
1658/// ```
1659///
1660/// # Dependencies
1661///
1662/// This macro depends on the `#[hyperlane(server: Server)]` macro to define the server instance.
1663#[proc_macro_attribute]
1664pub fn response_middleware(attr: TokenStream, item: TokenStream) -> TokenStream {
1665    response_middleware_macro(attr, item)
1666}
1667
1668/// Registers a function as a panic hook.
1669///
1670/// This attribute macro registers the decorated function to handle panics that occur
1671/// during request processing. This macro requires the `#[hyperlane(server: Server)]` macro to be used to define the server instance.
1672///
1673/// # Note
1674///
1675/// If an order parameter is not specified, the hook will have a higher priority than hooks with a specified order.
1676///
1677/// # Usage
1678///
1679/// ```rust
1680/// use hyperlane::*;
1681/// use hyperlane_macros::*;
1682///
1683/// #[panic_hook]
1684/// #[panic_hook(1)]
1685/// #[panic_hook("2")]
1686/// async fn handle_panic(ctx: Context) {
1687///     // Panic handling logic
1688/// }
1689/// ```
1690///
1691/// # Dependencies
1692///
1693/// This macro depends on the `#[hyperlane(server: Server)]` macro to define the server instance.
1694#[proc_macro_attribute]
1695pub fn panic_hook(attr: TokenStream, item: TokenStream) -> TokenStream {
1696    panic_hook_macro(attr, item)
1697}
1698
1699/// Injects a list of macros before the decorated function.
1700///
1701/// The macros are applied in head-insertion order, meaning the first macro in the list
1702/// is the outermost macro.
1703///
1704/// # Usage
1705///
1706/// ```rust
1707/// use hyperlane::*;
1708/// use hyperlane_macros::*;
1709///
1710/// #[prologue_hooks(post, send)]
1711/// async fn handler(ctx: Context) {
1712///     // ...
1713/// }
1714/// ```
1715#[proc_macro_attribute]
1716pub fn prologue_hooks(attr: TokenStream, item: TokenStream) -> TokenStream {
1717    prologue_hooks_macro(attr, item)
1718}
1719
1720/// Injects a list of macros after the decorated function.
1721///
1722/// The macros are applied in tail-insertion order, meaning the last macro in the list
1723/// is the outermost macro.
1724///
1725/// # Usage
1726///
1727/// ```rust
1728/// use hyperlane::*;
1729/// use hyperlane_macros::*;
1730///
1731/// #[epilogue_hooks(post, send)]
1732/// async fn handler(ctx: Context) {
1733///     // ...
1734/// }
1735/// ```
1736#[proc_macro_attribute]
1737pub fn epilogue_hooks(attr: TokenStream, item: TokenStream) -> TokenStream {
1738    epilogue_hooks_macro(attr, item)
1739}
1740
1741/// Sends only the response body with data after function execution.
1742///
1743/// This attribute macro ensures that only the response body is automatically sent
1744/// to the client after the function completes, handling request headers separately,
1745/// with the specified data.
1746///
1747/// # Usage
1748///
1749/// ```rust
1750/// use hyperlane::*;
1751/// use hyperlane_macros::*;
1752///
1753/// #[send_body_with_data("Response body content")]
1754/// async fn send_body_with_data_handler(ctx: Context) {
1755///     // Response body is automatically sent with the specified data after function returns
1756/// }
1757/// ```
1758///
1759/// The macro accepts data to send and should be applied to async functions
1760/// that accept a `Context` parameter.
1761#[proc_macro_attribute]
1762pub fn send_body_with_data(attr: TokenStream, item: TokenStream) -> TokenStream {
1763    send_body_with_data_macro(attr, item, Position::Epilogue)
1764}
1765
1766/// Wraps function body with WebSocket stream processing.
1767///
1768/// This attribute macro generates code that wraps the function body with a check to see if
1769/// data can be read from a WebSocket stream. The function body is only executed
1770/// if data is successfully read from the stream.
1771///
1772/// # Arguments
1773///
1774/// - `TokenStream`: The buffer to read from the WebSocket stream.
1775/// - `TokenStream`: The function item to be modified
1776///
1777/// # Returns
1778///
1779/// Returns a TokenStream containing the modified function with WebSocket stream processing logic.
1780///
1781/// # Examples
1782///
1783/// Basic usage with buffer size:
1784/// ```rust
1785/// #[ws_from_stream(1024)]
1786/// async fn handle_data(ctx: Context) {
1787///     // Process data from stream with 1024 byte buffer
1788/// }
1789/// ```
1790///
1791/// Using a variable name for the data:
1792/// ```rust
1793/// #[ws_from_stream(data)]
1794/// async fn handle_data(ctx: Context) {
1795///     // Data will be available in the `data` variable
1796/// }
1797/// ```
1798///
1799/// Using both buffer size and variable name:
1800/// ```rust
1801/// #[ws_from_stream(1024, payload)]
1802/// async fn handle_large_data(ctx: Context) {
1803///     // Process large data with 1024 byte buffer, available in `payload` variable
1804/// }
1805/// ```
1806///
1807/// Reversing buffer size and variable name:
1808/// ```rust
1809/// #[ws_from_stream(payload, 1024)]
1810/// async fn handle_reversed_data(ctx: Context) {
1811///     // Process data with 1024 byte buffer, available in `payload` variable
1812/// }
1813/// ```
1814#[proc_macro_attribute]
1815pub fn ws_from_stream(attr: TokenStream, item: TokenStream) -> TokenStream {
1816    ws_from_stream_macro(attr, item)
1817}
1818
1819/// Wraps function body with HTTP stream processing.
1820///
1821/// This attribute macro generates code that wraps the function body with a check to see if
1822/// data can be read from an HTTP stream. The function body is only executed
1823/// if data is successfully read from the stream.
1824///
1825/// # Arguments
1826///
1827/// - `TokenStream`: The buffer to read from the HTTP stream.
1828/// - `TokenStream`: The function item to be modified
1829///
1830/// # Returns
1831///
1832/// Returns a TokenStream containing the modified function with HTTP stream processing logic.
1833///
1834/// # Examples
1835///
1836/// Basic usage with buffer size:
1837/// ```rust
1838/// #[http_from_stream(1024)]
1839/// async fn handle_data(ctx: Context) {
1840///     // Process data from stream with 1024 byte buffer
1841/// }
1842/// ```
1843///
1844/// Using a variable name for the data:
1845/// ```rust
1846/// #[http_from_stream(data)]
1847/// async fn handle_data(ctx: Context) {
1848///     // Data will be available in the `data` variable
1849/// }
1850/// ```
1851///
1852/// Using both buffer size and variable name:
1853/// ```rust
1854/// #[http_from_stream(1024, payload)]
1855/// async fn handle_large_data(ctx: Context) {
1856///     // Process large data with 1024 byte buffer, available in `payload` variable
1857/// }
1858/// ```
1859#[proc_macro_attribute]
1860pub fn http_from_stream(attr: TokenStream, item: TokenStream) -> TokenStream {
1861    http_from_stream_macro(attr, item)
1862}