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