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