hyperlane_macros/lib.rs
1//! # Hyperlane Macros
2//!
3//! A comprehensive collection of procedural macros for building HTTP servers with enhanced functionality.
4//! This crate provides attribute macros that simplify HTTP request handling, protocol validation,
5//! response management, and request data extraction.
6//!
7//! ## Features
8//!
9//! - **HTTP Method Handlers**: Macros for all standard HTTP methods (GET, POST, PUT, DELETE, etc.)
10//! - **Protocol Validation**: Support for HTTP versions, WebSocket, TLS, and HTTP/2 cleartext
11//! - **Response Management**: Status code setting, reason phrase customization, and response sending
12//! - **Request Data Extraction**: Body parsing, header extraction, query parameters, and route parameters
13//! - **Filter Mechanisms**: Unknown method, upgrade, and version filtering
14//! - **Hook System**: Pre and post execution hooks for request processing
15//! - **Stream Management**: Connection state handling for aborted and closed streams
16//!
17//! ## Dependencies
18//!
19//! To use this crate, add the following dependencies to your `Cargo.toml`:
20//!
21//! ```toml
22//! [dependencies]
23//! hyperlane-macros = "*"
24//! hyperlane = "*"
25//! serde = { version = "*", features = ["derive"] }
26//! ```
27//!
28//! ## Usage Guidelines
29//!
30//! All macros are designed to be used as attribute macros on async functions that accept a `Context` parameter.
31//! Multiple macros can be combined on a single function to create complex request handling logic.
32//! When using hook macros (`pre_hook`, `post_hook`), avoid combining them with other macros on the same function
33//! to prevent unexpected behavior during macro expansion.
34
35mod aborted;
36mod closed;
37mod common;
38mod filter;
39mod flush;
40mod hook;
41mod http;
42mod protocol;
43mod request;
44mod response;
45mod send;
46
47pub(crate) use aborted::*;
48pub(crate) use closed::*;
49pub(crate) use common::*;
50pub(crate) use filter::*;
51pub(crate) use flush::*;
52pub(crate) use hook::*;
53pub(crate) use http::*;
54pub(crate) use protocol::*;
55pub(crate) use request::*;
56pub(crate) use response::*;
57pub(crate) use send::*;
58
59pub(crate) use proc_macro::TokenStream;
60pub(crate) use proc_macro2::TokenStream as TokenStream2;
61pub(crate) use quote::quote;
62pub(crate) use syn::{
63 parse::{Parse, ParseStream},
64 punctuated::Punctuated,
65 *,
66};
67
68/// Restricts function execution to HTTP GET requests only.
69///
70/// This attribute macro ensures the decorated function only executes when the incoming request
71/// uses the GET HTTP method. Requests with other methods will be filtered out.
72///
73/// # Usage
74///
75/// ```rust
76/// use hyperlane_macros::*;
77/// use hyperlane::*;
78///
79/// #[get]
80/// async fn handle_get(ctx: Context) {
81/// // Function body
82/// }
83/// ```
84///
85/// The macro takes no parameters and should be applied directly to async functions
86/// that accept a `Context` parameter.
87#[proc_macro_attribute]
88pub fn get(_attr: TokenStream, item: TokenStream) -> TokenStream {
89 get_handler(item)
90}
91
92/// Restricts function execution to HTTP POST requests only.
93///
94/// This attribute macro ensures the decorated function only executes when the incoming request
95/// uses the POST HTTP method. Requests with other methods will be filtered out.
96///
97/// # Usage
98///
99/// ```rust
100/// use hyperlane_macros::*;
101/// use hyperlane::*;
102///
103/// #[post]
104/// async fn handle_post(ctx: Context) {
105/// // Function body
106/// }
107/// ```
108///
109/// The macro takes no parameters and should be applied directly to async functions
110/// that accept a `Context` parameter.
111#[proc_macro_attribute]
112pub fn post(_attr: TokenStream, item: TokenStream) -> TokenStream {
113 post_handler(item)
114}
115
116/// Restricts function execution to HTTP PUT requests only.
117///
118/// This attribute macro ensures the decorated function only executes when the incoming request
119/// uses the PUT HTTP method. Requests with other methods will be filtered out.
120///
121/// # Usage
122///
123/// ```rust
124/// use hyperlane_macros::*;
125/// use hyperlane::*;
126///
127/// #[put]
128/// async fn handle_put(ctx: Context) {
129/// // Function body
130/// }
131/// ```
132///
133/// The macro takes no parameters and should be applied directly to async functions
134/// that accept a `Context` parameter.
135#[proc_macro_attribute]
136pub fn put(_attr: TokenStream, item: TokenStream) -> TokenStream {
137 put_handler(item)
138}
139
140/// Restricts function execution to HTTP DELETE requests only.
141///
142/// This attribute macro ensures the decorated function only executes when the incoming request
143/// uses the DELETE HTTP method. Requests with other methods will be filtered out.
144///
145/// # Usage
146///
147/// ```rust
148/// use hyperlane_macros::*;
149/// use hyperlane::*;
150///
151/// #[delete]
152/// async fn handle_delete(ctx: Context) {
153/// // Function body
154/// }
155/// ```
156///
157/// The macro takes no parameters and should be applied directly to async functions
158/// that accept a `Context` parameter.
159#[proc_macro_attribute]
160pub fn delete(_attr: TokenStream, item: TokenStream) -> TokenStream {
161 delete_handler(item)
162}
163
164/// Restricts function execution to HTTP PATCH requests only.
165///
166/// This attribute macro ensures the decorated function only executes when the incoming request
167/// uses the PATCH HTTP method. Requests with other methods will be filtered out.
168///
169/// # Usage
170///
171/// ```rust
172/// use hyperlane_macros::*;
173/// use hyperlane::*;
174///
175/// #[patch]
176/// async fn handle_patch(ctx: Context) {
177/// // Function body
178/// }
179/// ```
180///
181/// The macro takes no parameters and should be applied directly to async functions
182/// that accept a `Context` parameter.
183#[proc_macro_attribute]
184pub fn patch(_attr: TokenStream, item: TokenStream) -> TokenStream {
185 patch_handler(item)
186}
187
188/// Restricts function execution to HTTP HEAD requests only.
189///
190/// This attribute macro ensures the decorated function only executes when the incoming request
191/// uses the HEAD HTTP method. Requests with other methods will be filtered out.
192///
193/// # Usage
194///
195/// ```rust
196/// use hyperlane_macros::*;
197/// use hyperlane::*;
198///
199/// #[head]
200/// async fn handle_head(ctx: Context) {
201/// // Function body
202/// }
203/// ```
204///
205/// The macro takes no parameters and should be applied directly to async functions
206/// that accept a `Context` parameter.
207#[proc_macro_attribute]
208pub fn head(_attr: TokenStream, item: TokenStream) -> TokenStream {
209 head_handler(item)
210}
211
212/// Restricts function execution to HTTP OPTIONS requests only.
213///
214/// This attribute macro ensures the decorated function only executes when the incoming request
215/// uses the OPTIONS HTTP method. Requests with other methods will be filtered out.
216///
217/// # Usage
218///
219/// ```rust
220/// use hyperlane_macros::*;
221/// use hyperlane::*;
222///
223/// #[options]
224/// async fn handle_options(ctx: Context) {
225/// // Function body
226/// }
227/// ```
228///
229/// The macro takes no parameters and should be applied directly to async functions
230/// that accept a `Context` parameter.
231#[proc_macro_attribute]
232pub fn options(_attr: TokenStream, item: TokenStream) -> TokenStream {
233 options_handler(item)
234}
235
236/// Restricts function execution to HTTP CONNECT requests only.
237///
238/// This attribute macro ensures the decorated function only executes when the incoming request
239/// uses the CONNECT HTTP method. Requests with other methods will be filtered out.
240///
241/// # Usage
242///
243/// ```rust
244/// use hyperlane_macros::*;
245/// use hyperlane::*;
246///
247/// #[connect]
248/// async fn handle_connect(ctx: Context) {
249/// // Function body
250/// }
251/// ```
252///
253/// The macro takes no parameters and should be applied directly to async functions
254/// that accept a `Context` parameter.
255#[proc_macro_attribute]
256pub fn connect(_attr: TokenStream, item: TokenStream) -> TokenStream {
257 connect_handler(item)
258}
259
260/// Restricts function execution to HTTP TRACE requests only.
261///
262/// This attribute macro ensures the decorated function only executes when the incoming request
263/// uses the TRACE HTTP method. Requests with other methods will be filtered out.
264///
265/// # Usage
266///
267/// ```rust
268/// use hyperlane_macros::*;
269/// use hyperlane::*;
270///
271/// #[trace]
272/// async fn handle_trace(ctx: Context) {
273/// // Function body
274/// }
275/// ```
276///
277/// The macro takes no parameters and should be applied directly to async functions
278/// that accept a `Context` parameter.
279#[proc_macro_attribute]
280pub fn trace(_attr: TokenStream, item: TokenStream) -> TokenStream {
281 trace_handler(item)
282}
283
284/// Allows function to handle multiple HTTP methods.
285///
286/// This attribute macro configures the decorated function to execute for any of the specified
287/// HTTP methods. Methods should be provided as a comma-separated list.
288///
289/// # Usage
290///
291/// ```rust
292/// use hyperlane_macros::*;
293/// use hyperlane::*;
294///
295/// #[methods(get, post)]
296/// async fn handle_get_post(ctx: Context) {
297/// // Function body
298/// }
299///
300/// #[methods(put, patch, delete)]
301/// async fn handle_modifications(ctx: Context) {
302/// // Function body
303/// }
304/// ```
305///
306/// The macro accepts a comma-separated list of HTTP method names (lowercase) and should be
307/// applied to async functions that accept a `Context` parameter.
308#[proc_macro_attribute]
309pub fn methods(attr: TokenStream, item: TokenStream) -> TokenStream {
310 methods_macro(attr, item)
311}
312
313/// Restricts function execution to WebSocket upgrade requests only.
314///
315/// This attribute macro ensures the decorated function only executes when the incoming request
316/// is a valid WebSocket upgrade request with proper headers and protocol negotiation.
317///
318/// # Usage
319///
320/// ```rust
321/// use hyperlane_macros::*;
322/// use hyperlane::*;
323///
324/// #[ws]
325/// async fn handle_websocket(ctx: Context) {
326/// // WebSocket handling logic
327/// }
328/// ```
329///
330/// The macro takes no parameters and should be applied directly to async functions
331/// that accept a `Context` parameter.
332#[proc_macro_attribute]
333pub fn ws(_attr: TokenStream, item: TokenStream) -> TokenStream {
334 ws_macro(item)
335}
336
337/// Restricts function execution to standard HTTP requests only.
338///
339/// This attribute macro ensures the decorated function only executes for standard HTTP requests,
340/// excluding WebSocket upgrades and other protocol upgrade requests.
341///
342/// # Usage
343///
344/// ```rust
345/// use hyperlane_macros::*;
346/// use hyperlane::*;
347///
348/// #[http]
349/// async fn handle_http(ctx: Context) {
350/// // HTTP request handling logic
351/// }
352/// ```
353///
354/// The macro takes no parameters and should be applied directly to async functions
355/// that accept a `Context` parameter.
356#[proc_macro_attribute]
357pub fn http(_attr: TokenStream, item: TokenStream) -> TokenStream {
358 http_macro(item)
359}
360
361/// Sets the HTTP status code for the response.
362///
363/// This attribute macro configures the HTTP status code that will be sent with the response.
364/// The status code should be provided as a numeric value.
365///
366/// # Usage
367///
368/// ```rust
369/// use hyperlane_macros::*;
370/// use hyperlane::*;
371///
372/// #[status_code(200)]
373/// async fn success_handler(ctx: Context) {
374/// // Response will have status code 200
375/// }
376///
377/// #[status_code(404)]
378/// async fn not_found_handler(ctx: Context) {
379/// // Response will have status code 404
380/// }
381///
382/// #[status_code(500)]
383/// async fn error_handler(ctx: Context) {
384/// // Response will have status code 500
385/// }
386/// ```
387///
388/// The macro accepts a numeric HTTP status code (e.g., 200, 404, 500) and should be
389/// applied to async functions that accept a `Context` parameter.
390#[proc_macro_attribute]
391pub fn status_code(attr: TokenStream, item: TokenStream) -> TokenStream {
392 code_macro(attr, item)
393}
394
395/// Sets the HTTP reason phrase for the response.
396///
397/// This attribute macro configures the HTTP reason phrase that accompanies the status code.
398/// The reason phrase should be provided as a string literal.
399///
400/// # Usage
401///
402/// ```rust
403/// use hyperlane_macros::*;
404/// use hyperlane::*;
405///
406/// #[reason_phrase("OK")]
407/// async fn success_handler(ctx: Context) {
408/// // Response will have reason phrase "OK"
409/// }
410///
411/// #[reason_phrase("Not Found")]
412/// async fn not_found_handler(ctx: Context) {
413/// // Response will have reason phrase "Not Found"
414/// }
415///
416/// #[reason_phrase("Internal Server Error")]
417/// async fn error_handler(ctx: Context) {
418/// // Response will have reason phrase "Internal Server Error"
419/// }
420/// ```
421///
422/// The macro accepts a string literal for the reason phrase and should be
423/// applied to async functions that accept a `Context` parameter.
424#[proc_macro_attribute]
425pub fn reason_phrase(attr: TokenStream, item: TokenStream) -> TokenStream {
426 reason_phrase_macro(attr, item)
427}
428
429/// Automatically sends the complete response after function execution.
430///
431/// This attribute macro ensures that the response (headers and body) is automatically sent
432/// to the client after the function completes execution.
433///
434/// # Usage
435///
436/// ```rust
437/// use hyperlane_macros::*;
438/// use hyperlane::*;
439///
440/// #[send]
441/// async fn auto_send_handler(ctx: Context) {
442/// let _ = ctx.set_response_body("Hello World").await;
443/// // Response is automatically sent after function returns
444/// }
445/// ```
446///
447/// The macro takes no parameters and should be applied directly to async functions
448/// that accept a `Context` parameter.
449#[proc_macro_attribute]
450pub fn send(_attr: TokenStream, item: TokenStream) -> TokenStream {
451 send_macro(item)
452}
453
454/// Automatically sends only the response body after function execution.
455///
456/// This attribute macro ensures that only the response body is automatically sent
457/// to the client after the function completes, handling headers separately.
458///
459/// # Usage
460///
461/// ```rust
462/// use hyperlane_macros::*;
463/// use hyperlane::*;
464///
465/// #[send_body]
466/// async fn auto_send_body_handler(ctx: Context) {
467/// let _ = ctx.set_response_body("Response body content").await;
468/// // Only response body is automatically sent after function returns
469/// }
470/// ```
471///
472/// The macro takes no parameters and should be applied directly to async functions
473/// that accept a `Context` parameter.
474#[proc_macro_attribute]
475pub fn send_body(_attr: TokenStream, item: TokenStream) -> TokenStream {
476 send_body_macro(item)
477}
478
479/// Sends the complete response exactly once after function execution.
480///
481/// This attribute macro ensures that the response is sent exactly once to the client,
482/// preventing multiple response transmissions for single-use scenarios.
483///
484/// # Usage
485///
486/// ```rust
487/// use hyperlane_macros::*;
488/// use hyperlane::*;
489///
490/// #[send_once]
491/// async fn send_once_handler(ctx: Context) {
492/// let _ = ctx.set_response_body("One-time response").await;
493/// // Response is sent exactly once after function returns
494/// }
495/// ```
496///
497/// The macro takes no parameters and should be applied directly to async functions
498/// that accept a `Context` parameter.
499#[proc_macro_attribute]
500pub fn send_once(_attr: TokenStream, item: TokenStream) -> TokenStream {
501 send_once_macro(item)
502}
503
504/// Sends only the response body exactly once after function execution.
505///
506/// This attribute macro ensures that the response body is sent exactly once to the client,
507/// preventing multiple body transmissions for single-use scenarios.
508///
509/// # Usage
510///
511/// ```rust
512/// use hyperlane_macros::*;
513/// use hyperlane::*;
514///
515/// #[send_once_body]
516/// async fn send_once_body_handler(ctx: Context) {
517/// let _ = ctx.set_response_body("One-time body content").await;
518/// // Response body is sent exactly once after function returns
519/// }
520/// ```
521///
522/// The macro takes no parameters and should be applied directly to async functions
523/// that accept a `Context` parameter.
524#[proc_macro_attribute]
525pub fn send_once_body(_attr: TokenStream, item: TokenStream) -> TokenStream {
526 send_once_body_macro(item)
527}
528
529/// Flushes the response stream after function execution.
530///
531/// This attribute macro ensures that the response stream is flushed to guarantee immediate
532/// data transmission, forcing any buffered response data to be sent to the client.
533///
534/// # Usage
535///
536/// ```rust
537/// use hyperlane_macros::*;
538/// use hyperlane::*;
539///
540/// #[flush]
541/// async fn flush_handler(ctx: Context) {
542/// let _ = ctx.set_response_body("Immediate response").await;
543/// // Response stream is flushed after function returns
544/// }
545/// ```
546///
547/// The macro takes no parameters and should be applied directly to async functions
548/// that accept a `Context` parameter.
549#[proc_macro_attribute]
550pub fn flush(_attr: TokenStream, item: TokenStream) -> TokenStream {
551 flush_macro(item)
552}
553
554/// Handles aborted request scenarios.
555///
556/// This attribute macro configures the function to handle cases where the client has
557/// aborted the request, providing appropriate handling for interrupted or cancelled requests.
558///
559/// # Usage
560///
561/// ```rust
562/// use hyperlane_macros::*;
563/// use hyperlane::*;
564///
565/// #[aborted]
566/// async fn handle_aborted(ctx: Context) {
567/// // Handle aborted request logic
568/// }
569/// ```
570///
571/// The macro takes no parameters and should be applied directly to async functions
572/// that accept a `Context` parameter.
573#[proc_macro_attribute]
574pub fn aborted(_attr: TokenStream, item: TokenStream) -> TokenStream {
575 aborted_macro(item)
576}
577
578/// Handles closed connection scenarios.
579///
580/// This attribute macro configures the function to handle cases where the connection
581/// has been closed, providing appropriate handling for terminated or disconnected connections.
582///
583/// # Usage
584///
585/// ```rust
586/// use hyperlane_macros::*;
587/// use hyperlane::*;
588///
589/// #[closed]
590/// async fn handle_closed(ctx: Context) {
591/// // Handle closed connection logic
592/// }
593/// ```
594///
595/// The macro takes no parameters and should be applied directly to async functions
596/// that accept a `Context` parameter.
597#[proc_macro_attribute]
598pub fn closed(_attr: TokenStream, item: TokenStream) -> TokenStream {
599 closed_macro(item)
600}
601
602/// Restricts function execution to HTTP/2 Cleartext (h2c) requests only.
603///
604/// This attribute macro ensures the decorated function only executes for HTTP/2 cleartext
605/// requests that use the h2c upgrade mechanism.
606///
607/// # Usage
608///
609/// ```rust
610/// use hyperlane_macros::*;
611/// use hyperlane::*;
612///
613/// #[h2c]
614/// async fn handle_h2c(ctx: Context) {
615/// // Handle HTTP/2 cleartext requests
616/// }
617/// ```
618///
619/// The macro takes no parameters and should be applied directly to async functions
620/// that accept a `Context` parameter.
621#[proc_macro_attribute]
622pub fn h2c(_attr: TokenStream, item: TokenStream) -> TokenStream {
623 h2c_macro(item)
624}
625
626/// Restricts function execution to HTTP/0.9 requests only.
627///
628/// This attribute macro ensures the decorated function only executes for HTTP/0.9
629/// protocol requests, the earliest version of the HTTP protocol.
630///
631/// # Usage
632///
633/// ```rust
634/// use hyperlane_macros::*;
635/// use hyperlane::*;
636///
637/// #[http0_9]
638/// async fn handle_http09(ctx: Context) {
639/// // Handle HTTP/0.9 requests
640/// }
641/// ```
642///
643/// The macro takes no parameters and should be applied directly to async functions
644/// that accept a `Context` parameter.
645#[proc_macro_attribute]
646pub fn http0_9(_attr: TokenStream, item: TokenStream) -> TokenStream {
647 http0_9_macro(item)
648}
649
650/// Restricts function execution to HTTP/1.0 requests only.
651///
652/// This attribute macro ensures the decorated function only executes for HTTP/1.0
653/// protocol requests.
654///
655/// # Usage
656///
657/// ```rust
658/// use hyperlane_macros::*;
659/// use hyperlane::*;
660///
661/// #[http1_0]
662/// async fn handle_http10(ctx: Context) {
663/// // Handle HTTP/1.0 requests
664/// }
665/// ```
666///
667/// The macro takes no parameters and should be applied directly to async functions
668/// that accept a `Context` parameter.
669#[proc_macro_attribute]
670pub fn http1_0(_attr: TokenStream, item: TokenStream) -> TokenStream {
671 http1_0_macro(item)
672}
673
674/// Restricts function execution to HTTP/1.1 requests only.
675///
676/// This attribute macro ensures the decorated function only executes for HTTP/1.1
677/// protocol requests.
678///
679/// # Usage
680///
681/// ```rust
682/// use hyperlane_macros::*;
683/// use hyperlane::*;
684///
685/// #[http1_1]
686/// async fn handle_http11(ctx: Context) {
687/// // Handle HTTP/1.1 requests
688/// }
689/// ```
690///
691/// The macro takes no parameters and should be applied directly to async functions
692/// that accept a `Context` parameter.
693#[proc_macro_attribute]
694pub fn http1_1(_attr: TokenStream, item: TokenStream) -> TokenStream {
695 http1_1_macro(item)
696}
697
698/// Restricts function execution to HTTP/1.1 or higher protocol versions.
699///
700/// This attribute macro ensures the decorated function only executes for HTTP/1.1
701/// or newer protocol versions, including HTTP/2, HTTP/3, and future versions.
702///
703/// # Usage
704///
705/// ```rust
706/// use hyperlane_macros::*;
707/// use hyperlane::*;
708///
709/// #[http1_1_or_higher]
710/// async fn handle_modern_http(ctx: Context) {
711/// // Handle HTTP/1.1, HTTP/2, HTTP/3, etc.
712/// }
713/// ```
714///
715/// The macro takes no parameters and should be applied directly to async functions
716/// that accept a `Context` parameter.
717#[proc_macro_attribute]
718pub fn http1_1_or_higher(_attr: TokenStream, item: TokenStream) -> TokenStream {
719 http1_1_or_higher_macro(item)
720}
721
722/// Restricts function execution to HTTP/2 requests only.
723///
724/// This attribute macro ensures the decorated function only executes for HTTP/2
725/// protocol requests.
726///
727/// # Usage
728///
729/// ```rust
730/// use hyperlane_macros::*;
731/// use hyperlane::*;
732///
733/// #[http2]
734/// async fn handle_http2(ctx: Context) {
735/// // Handle HTTP/2 requests
736/// }
737/// ```
738///
739/// The macro takes no parameters and should be applied directly to async functions
740/// that accept a `Context` parameter.
741#[proc_macro_attribute]
742pub fn http2(_attr: TokenStream, item: TokenStream) -> TokenStream {
743 http2_macro(item)
744}
745
746/// Restricts function execution to HTTP/3 requests only.
747///
748/// This attribute macro ensures the decorated function only executes for HTTP/3
749/// protocol requests, the latest version of the HTTP protocol.
750///
751/// # Usage
752///
753/// ```rust
754/// use hyperlane_macros::*;
755/// use hyperlane::*;
756///
757/// #[http3]
758/// async fn handle_http3(ctx: Context) {
759/// // Handle HTTP/3 requests
760/// }
761/// ```
762///
763/// The macro takes no parameters and should be applied directly to async functions
764/// that accept a `Context` parameter.
765#[proc_macro_attribute]
766pub fn http3(_attr: TokenStream, item: TokenStream) -> TokenStream {
767 http3_macro(item)
768}
769
770/// Restricts function execution to TLS-encrypted requests only.
771///
772/// This attribute macro ensures the decorated function only executes for requests
773/// that use TLS/SSL encryption on the connection.
774///
775/// # Usage
776///
777/// ```rust
778/// use hyperlane_macros::*;
779/// use hyperlane::*;
780///
781/// #[tls]
782/// async fn handle_secure(ctx: Context) {
783/// // Handle TLS-encrypted requests only
784/// }
785/// ```
786///
787/// The macro takes no parameters and should be applied directly to async functions
788/// that accept a `Context` parameter.
789#[proc_macro_attribute]
790pub fn tls(_attr: TokenStream, item: TokenStream) -> TokenStream {
791 tls_macro(item)
792}
793
794/// Handles requests with unknown or non-standard HTTP methods.
795///
796/// This attribute macro configures the function to handle requests that use
797/// unrecognized or unsupported HTTP methods, providing a fallback for non-standard methods.
798///
799/// # Usage
800///
801/// ```rust
802/// use hyperlane_macros::*;
803/// use hyperlane::*;
804///
805/// #[filter_unknown_method]
806/// async fn handle_unknown_method(ctx: Context) {
807/// // Handle requests with unknown HTTP methods
808/// }
809/// ```
810///
811/// The macro takes no parameters and should be applied directly to async functions
812/// that accept a `Context` parameter.
813#[proc_macro_attribute]
814pub fn filter_unknown_method(_attr: TokenStream, item: TokenStream) -> TokenStream {
815 filter_unknown_method_macro(item)
816}
817
818/// Handles requests with unknown or non-standard upgrade protocols.
819///
820/// This attribute macro configures the function to handle requests that specify
821/// unrecognized upgrade protocols, providing a fallback for non-standard upgrade headers.
822///
823/// # Usage
824///
825/// ```rust
826/// use hyperlane_macros::*;
827/// use hyperlane::*;
828///
829/// #[filter_unknown_upgrade]
830/// async fn handle_unknown_upgrade(ctx: Context) {
831/// // Handle requests with unknown upgrade protocols
832/// }
833/// ```
834///
835/// The macro takes no parameters and should be applied directly to async functions
836/// that accept a `Context` parameter.
837#[proc_macro_attribute]
838pub fn filter_unknown_upgrade(_attr: TokenStream, item: TokenStream) -> TokenStream {
839 filter_unknown_upgrade_macro(item)
840}
841
842/// Handles requests with unknown or non-standard HTTP versions.
843///
844/// This attribute macro configures the function to handle requests that use
845/// unrecognized HTTP protocol versions, providing a fallback for non-standard versions.
846///
847/// # Usage
848///
849/// ```rust
850/// use hyperlane_macros::*;
851/// use hyperlane::*;
852///
853/// #[filter_unknown_version]
854/// async fn handle_unknown_version(ctx: Context) {
855/// // Handle requests with unknown HTTP versions
856/// }
857/// ```
858///
859/// The macro takes no parameters and should be applied directly to async functions
860/// that accept a `Context` parameter.
861#[proc_macro_attribute]
862pub fn filter_unknown_version(_attr: TokenStream, item: TokenStream) -> TokenStream {
863 filter_unknown_version_macro(item)
864}
865
866/// Handles requests with any unknown characteristics.
867///
868/// This attribute macro combines filtering for unknown methods, upgrade protocols, and HTTP versions,
869/// providing comprehensive handling for requests with any unrecognized characteristics.
870///
871/// # Usage
872///
873/// ```rust
874/// use hyperlane_macros::*;
875/// use hyperlane::*;
876///
877/// #[filter_unknown]
878/// async fn handle_all_unknown(ctx: Context) {
879/// // Handle requests with any unknown characteristics
880/// }
881/// ```
882///
883/// The macro takes no parameters and should be applied directly to async functions
884/// that accept a `Context` parameter.
885#[proc_macro_attribute]
886pub fn filter_unknown(_attr: TokenStream, item: TokenStream) -> TokenStream {
887 filter_unknown_macro(item)
888}
889
890/// Executes a specified function before the main handler function.
891///
892/// This attribute macro configures a pre-execution hook that runs before the main function logic.
893/// The specified hook function will be called first, followed by the main function execution.
894///
895/// # Usage
896///
897/// ```rust
898/// use hyperlane_macros::*;
899/// use hyperlane::*;
900///
901/// #[get]
902/// async fn pre_handler(ctx: Context) {
903/// // Pre-execution logic
904/// }
905///
906/// #[pre_hook(pre_handler)]
907/// async fn main_handler(ctx: Context) {
908/// // Main function logic (runs after pre_handler)
909/// }
910/// ```
911///
912/// The macro accepts a function name as parameter. Both the hook function and main function
913/// must accept a `Context` parameter. Avoid combining this macro with other macros on the
914/// same function to prevent macro expansion conflicts.
915#[proc_macro_attribute]
916pub fn pre_hook(attr: TokenStream, item: TokenStream) -> TokenStream {
917 pre_hook_macro(attr, item)
918}
919
920/// Executes a specified function after the main handler function.
921///
922/// This attribute macro configures a post-execution hook that runs after the main function logic.
923/// The main function will execute first, followed by the specified hook function.
924///
925/// # Usage
926///
927/// ```rust
928/// use hyperlane_macros::*;
929/// use hyperlane::*;
930///
931/// #[send]
932/// async fn post_handler(ctx: Context) {
933/// // Post-execution logic
934/// }
935///
936/// #[post_hook(post_handler)]
937/// async fn main_handler(ctx: Context) {
938/// // Main function logic (runs before post_handler)
939/// }
940/// ```
941///
942/// The macro accepts a function name as parameter. Both the hook function and main function
943/// must accept a `Context` parameter. Avoid combining this macro with other macros on the
944/// same function to prevent macro expansion conflicts.
945#[proc_macro_attribute]
946pub fn post_hook(attr: TokenStream, item: TokenStream) -> TokenStream {
947 post_hook_macro(attr, item)
948}
949
950/// Parses the request body into a specified variable and type.
951///
952/// This attribute macro extracts and deserializes the request body content into a variable
953/// with the specified type. The body content is typically parsed as JSON.
954///
955/// # Usage
956///
957/// ```rust
958/// use hyperlane_macros::*;
959/// use hyperlane::*;
960/// use serde::Deserialize;
961///
962/// #[derive(Deserialize, Clone)]
963/// struct UserData {
964/// name: String,
965/// age: u32,
966/// }
967///
968/// #[body(user_data: UserData)]
969/// async fn handle_user(ctx: Context) {
970/// if let Ok(data) = user_data {
971/// // Use the parsed user data
972/// }
973/// }
974/// ```
975///
976/// The macro accepts a variable name and type in the format `variable_name: Type`.
977/// The variable will be available in the function scope as a `Result<Type, Error>`.
978#[proc_macro_attribute]
979pub fn body(attr: TokenStream, item: TokenStream) -> TokenStream {
980 body_macro(attr, item)
981}
982
983/// Extracts a specific attribute value into a variable.
984///
985/// This attribute macro retrieves a specific attribute by key and makes it available
986/// as a typed variable from the request context.
987///
988/// # Usage
989///
990/// ```rust
991/// use hyperlane_macros::*;
992/// use hyperlane::*;
993/// use serde::Deserialize;
994///
995/// const USER_KEY: &str = "user_data";
996///
997/// #[derive(Deserialize, Clone)]
998/// struct User {
999/// id: u64,
1000/// name: String,
1001/// }
1002///
1003/// #[attribute(USER_KEY => user: User)]
1004/// async fn handle_with_attribute(ctx: Context) {
1005/// if let Some(user_data) = user {
1006/// // Use the extracted attribute
1007/// }
1008/// }
1009/// ```
1010///
1011/// The macro accepts a key-to-variable mapping in the format `key => variable_name: Type`.
1012/// The variable will be available as an `Option<Type>` in the function scope.
1013#[proc_macro_attribute]
1014pub fn attribute(attr: TokenStream, item: TokenStream) -> TokenStream {
1015 attribute_macro(attr, item)
1016}
1017
1018/// Extracts all attributes into a HashMap variable.
1019///
1020/// This attribute macro retrieves all available attributes from the request context
1021/// and makes them available as a HashMap for comprehensive attribute access.
1022///
1023/// # Usage
1024///
1025/// ```rust
1026/// use hyperlane_macros::*;
1027/// use hyperlane::*;
1028///
1029/// #[attributes(all_attrs)]
1030/// async fn handle_with_all_attributes(ctx: Context) {
1031/// for (key, value) in all_attrs {
1032/// // Process each attribute
1033/// }
1034/// }
1035/// ```
1036///
1037/// The macro accepts a variable name that will contain a HashMap of all attributes.
1038/// The variable will be available as a HashMap in the function scope.
1039#[proc_macro_attribute]
1040pub fn attributes(attr: TokenStream, item: TokenStream) -> TokenStream {
1041 attributes_macro(attr, item)
1042}
1043
1044/// Extracts a specific route parameter into a variable.
1045///
1046/// This attribute macro retrieves a specific route parameter by key and makes it
1047/// available as a variable. Route parameters are extracted from the URL path segments.
1048///
1049/// # Usage
1050///
1051/// ```rust
1052/// use hyperlane_macros::*;
1053/// use hyperlane::*;
1054///
1055/// // For route like "/users/{id}"
1056/// #[route_param("id" => user_id)]
1057/// async fn get_user(ctx: Context) {
1058/// if let Some(id) = user_id {
1059/// // Use the route parameter
1060/// }
1061/// }
1062/// ```
1063///
1064/// The macro accepts a key-to-variable mapping in the format `"key" => variable_name`.
1065/// The variable will be available as an `Option<String>` in the function scope.
1066#[proc_macro_attribute]
1067pub fn route_param(attr: TokenStream, item: TokenStream) -> TokenStream {
1068 route_param_macro(attr, item)
1069}
1070
1071/// Extracts all route parameters into a collection variable.
1072///
1073/// This attribute macro retrieves all available route parameters from the URL path
1074/// and makes them available as a collection for comprehensive route parameter access.
1075///
1076/// # Usage
1077///
1078/// ```rust
1079/// use hyperlane_macros::*;
1080/// use hyperlane::*;
1081///
1082/// // For route like "/users/{id}/posts/{post_id}"
1083/// #[route_params(params)]
1084/// async fn handle_nested_route(ctx: Context) {
1085/// for (key, value) in params {
1086/// // Process each route parameter
1087/// }
1088/// }
1089/// ```
1090///
1091/// The macro accepts a variable name that will contain all route parameters.
1092/// The variable will be available as a collection in the function scope.
1093#[proc_macro_attribute]
1094pub fn route_params(attr: TokenStream, item: TokenStream) -> TokenStream {
1095 route_params_macro(attr, item)
1096}
1097
1098/// Extracts a specific query parameter into a variable.
1099///
1100/// This attribute macro retrieves a specific query parameter by key and makes it
1101/// available as a variable. Query parameters are extracted from the URL query string.
1102///
1103/// # Usage
1104///
1105/// ```rust
1106/// use hyperlane_macros::*;
1107/// use hyperlane::*;
1108///
1109/// // For URL like "/search?q=rust&limit=10"
1110/// #[query("q" => search_term)]
1111/// async fn search(ctx: Context) {
1112/// if let Some(term) = search_term {
1113/// // Use the query parameter
1114/// }
1115/// }
1116/// ```
1117///
1118/// The macro accepts a key-to-variable mapping in the format `"key" => variable_name`.
1119/// The variable will be available as an `Option<String>` in the function scope.
1120#[proc_macro_attribute]
1121pub fn query(attr: TokenStream, item: TokenStream) -> TokenStream {
1122 query_macro(attr, item)
1123}
1124
1125/// Extracts all query parameters into a collection variable.
1126///
1127/// This attribute macro retrieves all available query parameters from the URL query string
1128/// and makes them available as a collection for comprehensive query parameter access.
1129///
1130/// # Usage
1131///
1132/// ```rust
1133/// use hyperlane_macros::*;
1134/// use hyperlane::*;
1135///
1136/// // For URL like "/search?q=rust&limit=10&sort=date"
1137/// #[querys(all_params)]
1138/// async fn search_with_params(ctx: Context) {
1139/// for (key, value) in all_params {
1140/// // Process each query parameter
1141/// }
1142/// }
1143/// ```
1144///
1145/// The macro accepts a variable name that will contain all query parameters.
1146/// The variable will be available as a collection in the function scope.
1147#[proc_macro_attribute]
1148pub fn querys(attr: TokenStream, item: TokenStream) -> TokenStream {
1149 querys_macro(attr, item)
1150}
1151
1152/// Extracts a specific HTTP header into a variable.
1153///
1154/// This attribute macro retrieves a specific HTTP header by name and makes it
1155/// available as a variable. Header values are extracted from the request headers collection.
1156///
1157/// # Usage
1158///
1159/// ```rust
1160/// use hyperlane_macros::*;
1161/// use hyperlane::*;
1162///
1163/// #[header(HOST => host_header)]
1164/// async fn handle_with_host(ctx: Context) {
1165/// if let Some(host) = host_header {
1166/// // Use the host header value
1167/// }
1168/// }
1169///
1170/// #[header("Content-Type" => content_type)]
1171/// async fn handle_with_content_type(ctx: Context) {
1172/// if let Some(ct) = content_type {
1173/// // Use the content type header
1174/// }
1175/// }
1176/// ```
1177///
1178/// The macro accepts a header name-to-variable mapping in the format `HEADER_NAME => variable_name`
1179/// or `"Header-Name" => variable_name`. The variable will be available as an `Option<String>`.
1180#[proc_macro_attribute]
1181pub fn header(attr: TokenStream, item: TokenStream) -> TokenStream {
1182 header_macro(attr, item)
1183}
1184
1185/// Extracts all HTTP headers into a collection variable.
1186///
1187/// This attribute macro retrieves all available HTTP headers from the request
1188/// and makes them available as a collection for comprehensive header access.
1189///
1190/// # Usage
1191///
1192/// ```rust
1193/// use hyperlane_macros::*;
1194/// use hyperlane::*;
1195///
1196/// #[headers(all_headers)]
1197/// async fn handle_with_all_headers(ctx: Context) {
1198/// for (name, value) in all_headers {
1199/// // Process each header
1200/// }
1201/// }
1202/// ```
1203///
1204/// The macro accepts a variable name that will contain all HTTP headers.
1205/// The variable will be available as a collection in the function scope.
1206#[proc_macro_attribute]
1207pub fn headers(attr: TokenStream, item: TokenStream) -> TokenStream {
1208 headers_macro(attr, item)
1209}