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