hyperlane_macros/lib.rs
1//! hyperlane-macros
2//!
3//! A comprehensive collection of procedural macros for building
4//! HTTP servers with enhanced functionality. This crate provides
5//! attribute macros that simplify HTTP request handling, protocol
6//! validation, response management, and request data extraction.
7
8mod aborted;
9mod closed;
10mod common;
11mod filter;
12mod flush;
13mod from_stream;
14mod hook;
15mod host;
16mod hyperlane;
17mod inject;
18mod method;
19mod referer;
20mod reject;
21mod request;
22mod request_middleware;
23mod response;
24mod response_middleware;
25mod route;
26mod send;
27mod stream;
28mod upgrade;
29mod version;
30
31use {
32 aborted::*, closed::*, common::*, filter::*, flush::*, from_stream::*, hook::*, host::*,
33 hyperlane::*, inject::*, method::*, referer::*, reject::*, request::*, request_middleware::*,
34 response::*, response_middleware::*, route::*, send::*, stream::*, upgrade::*, version::*,
35};
36
37use {
38 ::hyperlane::inventory,
39 proc_macro::TokenStream,
40 proc_macro2::TokenStream as TokenStream2,
41 quote::quote,
42 syn::{
43 Ident, Token,
44 parse::{Parse, ParseStream, Parser, Result},
45 punctuated::Punctuated,
46 token::Comma,
47 *,
48 },
49};
50
51inventory::collect!(InjectableMacro);
52
53/// Wraps function body with WebSocket stream processing.
54///
55/// This attribute macro generates code that wraps the function body with a check to see if
56/// data can be read from a WebSocket stream. The function body is only executed
57/// if data is successfully read from the stream.
58///
59/// # Arguments
60///
61/// - `TokenStream`: Optional variable name to store the read request data.
62/// - `TokenStream`: The function item to be modified
63///
64/// # Returns
65///
66/// Returns a TokenStream containing the modified function with WebSocket stream processing logic.
67///
68/// # Examples
69///
70/// Using no parameters:
71///
72/// ```rust
73/// use hyperlane::*;
74/// use hyperlane_macros::*;
75///
76/// #[route("/ws_upgrade_type")]
77/// struct Websocket;
78///
79/// impl ServerHook for Websocket {
80/// async fn new(_ctx: &mut Context) -> Self {
81/// Self
82/// }
83///
84/// #[ws_upgrade_type]
85/// #[ws_from_stream]
86/// async fn handle(self, ctx: &mut Context) {
87/// let body: &RequestBody = ctx.get_request().get_body();
88/// let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(&body);
89/// ctx.send_body_list_with_data(&body_list).await;
90/// }
91/// }
92/// ```
93///
94/// Using variable name to store request data:
95///
96/// ```rust
97/// use hyperlane::*;
98/// use hyperlane_macros::*;
99///
100/// #[route("/ws_upgrade_type")]
101/// struct Websocket;
102///
103/// impl ServerHook for Websocket {
104/// async fn new(_ctx: &mut Context) -> Self {
105/// Self
106/// }
107///
108/// #[ws_upgrade_type]
109/// #[ws_from_stream(request)]
110/// async fn handle(self, ctx: &mut Context) {
111/// let body: &RequestBody = request.get_body();
112/// let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(body);
113/// ctx.send_body_list_with_data(&body_list).await;
114/// }
115/// }
116///
117/// impl Websocket {
118/// #[ws_from_stream(request)]
119/// async fn ws_from_stream_with_ref_self(&self, ctx: &mut Context) {}
120/// }
121///
122/// #[ws_from_stream]
123/// async fn standalone_ws_from_stream_handler(ctx: &mut Context) {}
124/// ```
125#[proc_macro_attribute]
126pub fn ws_from_stream(attr: TokenStream, item: TokenStream) -> TokenStream {
127 ws_from_stream_macro(attr, item)
128}
129
130/// Wraps function body with HTTP stream processing.
131///
132/// This attribute macro generates code that wraps the function body with a check to see if
133/// data can be read from an HTTP stream. The function body is only executed
134/// if data is successfully read from the stream.
135///
136/// # Arguments
137///
138/// - `TokenStream`: Optional variable name to store the read request data.
139/// - `TokenStream`: The function item to be modified
140///
141/// # Returns
142///
143/// Returns a TokenStream containing the modified function with HTTP stream processing logic.
144///
145/// # Examples
146///
147/// Using no parameters:
148///
149/// ```rust
150/// use hyperlane::*;
151/// use hyperlane_macros::*;
152///
153/// #[route("/http_from_stream")]
154/// struct HttpFromStreamTest;
155///
156/// impl ServerHook for HttpFromStreamTest {
157/// async fn new(_ctx: &mut Context) -> Self {
158/// Self
159/// }
160///
161/// #[http_from_stream]
162/// async fn handle(self, ctx: &mut Context) {}
163/// }
164/// ```
165///
166/// Using with variable name:
167///
168/// ```rust
169/// use hyperlane::*;
170/// use hyperlane_macros::*;
171///
172/// #[route("/http_from_stream")]
173/// struct HttpFromStreamTest;
174///
175/// impl ServerHook for HttpFromStreamTest {
176/// async fn new(_ctx: &mut Context) -> Self {
177/// Self
178/// }
179///
180/// #[http_from_stream(_request)]
181/// async fn handle(self, ctx: &mut Context) {}
182/// }
183///
184/// impl HttpFromStreamTest {
185/// #[http_from_stream(_request)]
186/// async fn http_from_stream_with_ref_self(&self, ctx: &mut Context) {}
187/// }
188///
189/// #[http_from_stream]
190/// async fn standalone_http_from_stream_handler(ctx: &mut Context) {}
191/// ```
192#[proc_macro_attribute]
193pub fn http_from_stream(attr: TokenStream, item: TokenStream) -> TokenStream {
194 http_from_stream_macro(attr, item)
195}
196
197/// Restricts function execution to HTTP GET requests only.
198///
199/// This attribute macro ensures the decorated function only executes when the incoming request
200/// uses the GET HTTP method. Requests with other methods will be filtered out.
201///
202/// # Usage
203///
204/// ```rust
205/// use hyperlane::*;
206/// use hyperlane_macros::*;
207///
208/// #[route("/get_method")]
209/// struct Get;
210///
211/// impl ServerHook for Get {
212/// async fn new(_ctx: &mut Context) -> Self {
213/// Self
214/// }
215///
216/// #[prologue_macros(get_method, response_body("get_method"))]
217/// async fn handle(self, ctx: &mut Context) {}
218/// }
219///
220/// impl Get {
221/// #[get_method]
222/// async fn get_with_ref_self(&self, ctx: &mut Context) {}
223/// }
224///
225/// #[get_method]
226/// async fn standalone_get_handler(ctx: &mut Context) {}
227/// ```
228///
229/// The macro takes no parameters and should be applied directly to async functions
230/// that accept a `&mut Context` parameter.
231#[proc_macro_attribute]
232pub fn get_method(_attr: TokenStream, item: TokenStream) -> TokenStream {
233 get_method_handler(item, Position::Prologue)
234}
235
236/// Restricts function execution to HTTP POST requests only.
237///
238/// This attribute macro ensures the decorated function only executes when the incoming request
239/// uses the POST HTTP method. Requests with other methods will be filtered out.
240///
241/// # Usage
242///
243/// ```rust
244/// use hyperlane::*;
245/// use hyperlane_macros::*;
246///
247/// #[route("/post_method")]
248/// struct Post;
249///
250/// impl ServerHook for Post {
251/// async fn new(_ctx: &mut Context) -> Self {
252/// Self
253/// }
254///
255/// #[prologue_macros(post_method, response_body("post_method"))]
256/// async fn handle(self, ctx: &mut Context) {}
257/// }
258///
259/// impl Post {
260/// #[post_method]
261/// async fn post_with_ref_self(&self, ctx: &mut Context) {}
262/// }
263///
264/// #[post_method]
265/// async fn standalone_post_handler(ctx: &mut Context) {}
266/// ```
267///
268/// The macro takes no parameters and should be applied directly to async functions
269/// that accept a `&mut Context` parameter.
270#[proc_macro_attribute]
271pub fn post_method(_attr: TokenStream, item: TokenStream) -> TokenStream {
272 post_method_handler(item, Position::Prologue)
273}
274
275/// Restricts function execution to HTTP PUT requests only.
276///
277/// This attribute macro ensures the decorated function only executes when the incoming request
278/// uses the PUT HTTP method. Requests with other methods will be filtered out.
279///
280/// # Usage
281///
282/// ```rust
283/// use hyperlane::*;
284/// use hyperlane_macros::*;
285///
286/// #[route("/put_method")]
287/// struct Put;
288///
289/// impl ServerHook for Put {
290/// async fn new(_ctx: &mut Context) -> Self {
291/// Self
292/// }
293///
294/// #[prologue_macros(put_method, response_body("put_method"))]
295/// async fn handle(self, ctx: &mut Context) {}
296/// }
297///
298/// impl Put {
299/// #[put_method]
300/// async fn put_with_ref_self(&self, ctx: &mut Context) {}
301/// }
302///
303/// #[put_method]
304/// async fn standalone_put_handler(ctx: &mut Context) {}
305/// ```
306///
307/// The macro takes no parameters and should be applied directly to async functions
308/// that accept a `&mut Context` parameter.
309#[proc_macro_attribute]
310pub fn put_method(_attr: TokenStream, item: TokenStream) -> TokenStream {
311 put_method_handler(item, Position::Prologue)
312}
313
314/// Restricts function execution to HTTP DELETE requests only.
315///
316/// This attribute macro ensures the decorated function only executes when the incoming request
317/// uses the DELETE HTTP method. Requests with other methods will be filtered out.
318///
319/// # Usage
320///
321/// ```rust
322/// use hyperlane::*;
323/// use hyperlane_macros::*;
324///
325/// #[route("/delete_method")]
326/// struct Delete;
327///
328/// impl ServerHook for Delete {
329/// async fn new(_ctx: &mut Context) -> Self {
330/// Self
331/// }
332///
333/// #[prologue_macros(delete_method, response_body("delete_method"))]
334/// async fn handle(self, ctx: &mut Context) {}
335/// }
336///
337/// impl Delete {
338/// #[delete_method]
339/// async fn delete_with_ref_self(&self, ctx: &mut Context) {}
340/// }
341///
342/// #[delete_method]
343/// async fn standalone_delete_handler(ctx: &mut Context) {}
344/// ```
345///
346/// The macro takes no parameters and should be applied directly to async functions
347/// that accept a `&mut Context` parameter.
348#[proc_macro_attribute]
349pub fn delete_method(_attr: TokenStream, item: TokenStream) -> TokenStream {
350 delete_method_handler(item, Position::Prologue)
351}
352
353/// Restricts function execution to HTTP PATCH requests only.
354///
355/// This attribute macro ensures the decorated function only executes when the incoming request
356/// uses the PATCH HTTP method. Requests with other methods will be filtered out.
357///
358/// # Usage
359///
360/// ```rust
361/// use hyperlane::*;
362/// use hyperlane_macros::*;
363///
364/// #[route("/patch_method")]
365/// struct Patch;
366///
367/// impl ServerHook for Patch {
368/// async fn new(_ctx: &mut Context) -> Self {
369/// Self
370/// }
371///
372/// #[prologue_macros(patch_method, response_body("patch_method"))]
373/// async fn handle(self, ctx: &mut Context) {}
374/// }
375///
376/// impl Patch {
377/// #[patch_method]
378/// async fn patch_with_ref_self(&self, ctx: &mut Context) {}
379/// }
380///
381/// #[patch_method]
382/// async fn standalone_patch_handler(ctx: &mut Context) {}
383/// ```
384///
385/// The macro takes no parameters and should be applied directly to async functions
386/// that accept a `&mut Context` parameter.
387#[proc_macro_attribute]
388pub fn patch_method(_attr: TokenStream, item: TokenStream) -> TokenStream {
389 patch_method_handler(item, Position::Prologue)
390}
391
392/// Restricts function execution to HTTP HEAD requests only.
393///
394/// This attribute macro ensures the decorated function only executes when the incoming request
395/// uses the HEAD HTTP method. Requests with other methods will be filtered out.
396///
397/// # Usage
398///
399/// ```rust
400/// use hyperlane::*;
401/// use hyperlane_macros::*;
402///
403/// #[route("/head_method")]
404/// struct Head;
405///
406/// impl ServerHook for Head {
407/// async fn new(_ctx: &mut Context) -> Self {
408/// Self
409/// }
410///
411/// #[prologue_macros(head_method, response_body("head_method"))]
412/// async fn handle(self, ctx: &mut Context) {}
413/// }
414///
415/// impl Head {
416/// #[head_method]
417/// async fn head_with_ref_self(&self, ctx: &mut Context) {}
418/// }
419///
420/// #[head_method]
421/// async fn standalone_head_handler(ctx: &mut Context) {}
422/// ```
423///
424/// The macro takes no parameters and should be applied directly to async functions
425/// that accept a `&mut Context` parameter.
426#[proc_macro_attribute]
427pub fn head_method(_attr: TokenStream, item: TokenStream) -> TokenStream {
428 head_method_handler(item, Position::Prologue)
429}
430
431/// Restricts function execution to HTTP OPTIONS requests only.
432///
433/// This attribute macro ensures the decorated function only executes when the incoming request
434/// uses the OPTIONS HTTP method. Requests with other methods will be filtered out.
435///
436/// # Usage
437///
438/// ```rust
439/// use hyperlane::*;
440/// use hyperlane_macros::*;
441///
442/// #[route("/options_method")]
443/// struct Options;
444///
445/// impl ServerHook for Options {
446/// async fn new(_ctx: &mut Context) -> Self {
447/// Self
448/// }
449///
450/// #[prologue_macros(options_method, response_body("options_method"))]
451/// async fn handle(self, ctx: &mut Context) {}
452/// }
453///
454/// impl Options {
455/// #[options_method]
456/// async fn options_with_ref_self(&self, ctx: &mut Context) {}
457/// }
458///
459/// #[options_method]
460/// async fn standalone_options_handler(ctx: &mut Context) {}
461/// ```
462///
463/// The macro takes no parameters and should be applied directly to async functions
464/// that accept a `&mut Context` parameter.
465#[proc_macro_attribute]
466pub fn options_method(_attr: TokenStream, item: TokenStream) -> TokenStream {
467 options_method_handler(item, Position::Prologue)
468}
469
470/// Restricts function execution to HTTP CONNECT requests only.
471///
472/// This attribute macro ensures the decorated function only executes when the incoming request
473/// uses the CONNECT HTTP method. Requests with other methods will be filtered out.
474///
475/// # Usage
476///
477/// ```rust
478/// use hyperlane::*;
479/// use hyperlane_macros::*;
480///
481/// #[route("/connect_method")]
482/// struct Connect;
483///
484/// impl ServerHook for Connect {
485/// async fn new(_ctx: &mut Context) -> Self {
486/// Self
487/// }
488///
489/// #[prologue_macros(connect_method, response_body("connect_method"))]
490/// async fn handle(self, ctx: &mut Context) {}
491/// }
492///
493/// impl Connect {
494/// #[connect_method]
495/// async fn connect_with_ref_self(&self, ctx: &mut Context) {}
496/// }
497///
498/// #[connect_method]
499/// async fn standalone_connect_handler(ctx: &mut Context) {}
500/// ```
501///
502/// The macro takes no parameters and should be applied directly to async functions
503/// that accept a `&mut Context` parameter.
504#[proc_macro_attribute]
505pub fn connect_method(_attr: TokenStream, item: TokenStream) -> TokenStream {
506 connect_method_handler(item, Position::Prologue)
507}
508
509/// Restricts function execution to HTTP TRACE requests only.
510///
511/// This attribute macro ensures the decorated function only executes when the incoming request
512/// uses the TRACE HTTP method. Requests with other methods will be filtered out.
513///
514/// # Usage
515///
516/// ```rust
517/// use hyperlane::*;
518/// use hyperlane_macros::*;
519///
520/// #[route("/trace_method")]
521/// struct Trace;
522///
523/// impl ServerHook for Trace {
524/// async fn new(_ctx: &mut Context) -> Self {
525/// Self
526/// }
527///
528/// #[prologue_macros(trace_method, response_body("trace_method"))]
529/// async fn handle(self, ctx: &mut Context) {}
530/// }
531///
532/// impl Trace {
533/// #[trace_method]
534/// async fn trace_with_ref_self(&self, ctx: &mut Context) {}
535/// }
536///
537/// #[trace_method]
538/// async fn standalone_trace_handler(ctx: &mut Context) {}
539/// ```
540///
541/// The macro takes no parameters and should be applied directly to async functions
542/// that accept a `&mut Context` parameter.
543#[proc_macro_attribute]
544pub fn trace_method(_attr: TokenStream, item: TokenStream) -> TokenStream {
545 trace_method_handler(item, Position::Prologue)
546}
547
548/// Restricts function execution to unknown HTTP methods only.
549///
550/// This attribute macro ensures the decorated function only executes when the incoming request
551/// uses an HTTP method that is not one of the standard methods (GET, POST, PUT, DELETE, PATCH,
552/// HEAD, OPTIONS, CONNECT, TRACE). Requests with standard methods will be filtered out.
553///
554/// # Usage
555///
556/// ```rust
557/// use hyperlane::*;
558/// use hyperlane_macros::*;
559///
560/// #[route("/unknown_method")]
561/// struct UnknownMethod;
562///
563/// impl ServerHook for UnknownMethod {
564/// async fn new(_ctx: &mut Context) -> Self {
565/// Self
566/// }
567///
568/// #[prologue_macros(
569/// clear_response_headers,
570/// filter(ctx.get_request().get_method().is_unknown()),
571/// response_body("unknown_method")
572/// )]
573/// async fn handle(self, ctx: &mut Context) {}
574/// }
575///
576/// impl UnknownMethod {
577/// #[unknown_method]
578/// async fn unknown_method_with_ref_self(&self, ctx: &mut Context) {}
579/// }
580///
581/// #[unknown_method]
582/// async fn standalone_unknown_method_handler(ctx: &mut Context) {}
583/// ```
584///
585/// The macro takes no parameters and should be applied directly to async functions
586/// that accept a `&mut Context` parameter.
587#[proc_macro_attribute]
588pub fn unknown_method(_attr: TokenStream, item: TokenStream) -> TokenStream {
589 unknown_method_handler(item, Position::Prologue)
590}
591
592/// Allows function to handle multiple HTTP methods.
593///
594/// This attribute macro configures the decorated function to execute for any of the specified
595/// HTTP methods. Methods should be provided as a comma-separated list.
596///
597/// # Usage
598///
599/// ```rust
600/// use hyperlane::*;
601/// use hyperlane_macros::*;
602///
603/// #[route("/methods")]
604/// struct GetPost;
605///
606/// impl ServerHook for GetPost {
607/// async fn new(_ctx: &mut Context) -> Self {
608/// Self
609/// }
610///
611/// #[prologue_macros(
612/// http_version,
613/// methods(get, post),
614/// response_body("methods")
615/// )]
616/// async fn handle(self, ctx: &mut Context) {}
617/// }
618///
619/// impl GetPost {
620/// #[methods(get, post)]
621/// async fn methods_with_ref_self(&self, ctx: &mut Context) {}
622/// }
623///
624/// #[methods(get, post)]
625/// async fn standalone_methods_handler(ctx: &mut Context) {}
626/// ```
627///
628/// The macro accepts a comma-separated list of HTTP method names (lowercase) and should be
629/// applied to async functions that accept a `&mut Context` parameter.
630#[proc_macro_attribute]
631pub fn methods(attr: TokenStream, item: TokenStream) -> TokenStream {
632 methods_macro(attr, item, Position::Prologue)
633}
634
635/// Restricts function execution to HTTP/0.9 requests only.
636///
637/// This attribute macro ensures the decorated function only executes for HTTP/0.9
638/// protocol requests, the earliest version of the HTTP protocol.
639///
640/// # Usage
641///
642/// ```rust
643/// use hyperlane::*;
644/// use hyperlane_macros::*;
645///
646/// #[route("/http0_9_version")]
647/// struct Http09;
648///
649/// impl ServerHook for Http09 {
650/// async fn new(_ctx: &mut Context) -> Self {
651/// Self
652/// }
653///
654/// #[prologue_macros(http0_9_version, response_body("http0_9_version"))]
655/// async fn handle(self, ctx: &mut Context) {}
656/// }
657///
658/// impl Http09 {
659/// #[http0_9_version]
660/// async fn http0_9_version_with_ref_self(&self, ctx: &mut Context) {}
661/// }
662///
663/// #[http0_9_version]
664/// async fn standalone_http0_9_version_handler(ctx: &mut Context) {}
665/// ```
666///
667/// The macro takes no parameters and should be applied directly to async functions
668/// that accept a `&mut Context` parameter.
669#[proc_macro_attribute]
670pub fn http0_9_version(_attr: TokenStream, item: TokenStream) -> TokenStream {
671 http0_9_version_macro(item, Position::Prologue)
672}
673
674/// Restricts function execution to HTTP/1.0 requests only.
675///
676/// This attribute macro ensures the decorated function only executes for HTTP/1.0
677/// protocol requests.
678///
679/// # Usage
680///
681/// ```rust
682/// use hyperlane::*;
683/// use hyperlane_macros::*;
684///
685/// #[route("/http1_0_version")]
686/// struct Http10;
687///
688/// impl ServerHook for Http10 {
689/// async fn new(_ctx: &mut Context) -> Self {
690/// Self
691/// }
692///
693/// #[prologue_macros(http1_0_version, response_body("http1_0_version"))]
694/// async fn handle(self, ctx: &mut Context) {}
695/// }
696///
697/// impl Http10 {
698/// #[http1_0_version]
699/// async fn http1_0_version_with_ref_self(&self, ctx: &mut Context) {}
700/// }
701///
702/// #[http1_0_version]
703/// async fn standalone_http1_0_version_handler(ctx: &mut Context) {}
704/// ```
705///
706/// The macro takes no parameters and should be applied directly to async functions
707/// that accept a `&mut Context` parameter.
708#[proc_macro_attribute]
709pub fn http1_0_version(_attr: TokenStream, item: TokenStream) -> TokenStream {
710 http1_0_version_macro(item, Position::Prologue)
711}
712
713/// Restricts function execution to HTTP/1.1 requests only.
714///
715/// This attribute macro ensures the decorated function only executes for HTTP/1.1
716/// protocol requests.
717///
718/// # Usage
719///
720/// ```rust
721/// use hyperlane::*;
722/// use hyperlane_macros::*;
723///
724/// #[route("/http1_1_version")]
725/// struct Http11;
726///
727/// impl ServerHook for Http11 {
728/// async fn new(_ctx: &mut Context) -> Self {
729/// Self
730/// }
731///
732/// #[prologue_macros(http1_1_version, response_body("http1_1_version"))]
733/// async fn handle(self, ctx: &mut Context) {}
734/// }
735///
736/// impl Http11 {
737/// #[http1_1_version]
738/// async fn http1_1_with_ref_self(&self, ctx: &mut Context) {}
739/// }
740///
741/// #[http1_1_version]
742/// async fn standalone_http1_1_handler(ctx: &mut Context) {}
743/// ```
744///
745/// The macro takes no parameters and should be applied directly to async functions
746/// that accept a `&mut Context` parameter.
747#[proc_macro_attribute]
748pub fn http1_1_version(_attr: TokenStream, item: TokenStream) -> TokenStream {
749 http1_1_version_macro(item, Position::Prologue)
750}
751
752/// Restricts function execution to HTTP/2 requests only.
753///
754/// This attribute macro ensures the decorated function only executes for HTTP/2
755/// protocol requests.
756///
757/// # Usage
758///
759/// ```rust
760/// use hyperlane::*;
761/// use hyperlane_macros::*;
762///
763/// #[route("/http2_version")]
764/// struct Http2;
765///
766/// impl ServerHook for Http2 {
767/// async fn new(_ctx: &mut Context) -> Self {
768/// Self
769/// }
770///
771/// #[prologue_macros(http2_version, response_body("http2_version"))]
772/// async fn handle(self, ctx: &mut Context) {}
773/// }
774///
775/// impl Http2 {
776/// #[http2_version]
777/// async fn http2_version_with_ref_self(&self, ctx: &mut Context) {}
778/// }
779///
780/// #[http2_version]
781/// async fn standalone_http2_version_handler(ctx: &mut Context) {}
782/// ```
783///
784/// The macro takes no parameters and should be applied directly to async functions
785/// that accept a `&mut Context` parameter.
786#[proc_macro_attribute]
787pub fn http2_version(_attr: TokenStream, item: TokenStream) -> TokenStream {
788 http2_version_macro(item, Position::Prologue)
789}
790
791/// Restricts function execution to HTTP/3 requests only.
792///
793/// This attribute macro ensures the decorated function only executes for HTTP/3
794/// protocol requests, the latest version of the HTTP protocol.
795///
796/// # Usage
797///
798/// ```rust
799/// use hyperlane::*;
800/// use hyperlane_macros::*;
801///
802/// #[route("/http3_version")]
803/// struct Http3;
804///
805/// impl ServerHook for Http3 {
806/// async fn new(_ctx: &mut Context) -> Self {
807/// Self
808/// }
809///
810/// #[prologue_macros(http3_version, response_body("http3_version"))]
811/// async fn handle(self, ctx: &mut Context) {}
812/// }
813///
814/// impl Http3 {
815/// #[http3_version]
816/// async fn http3_version_with_ref_self(&self, ctx: &mut Context) {}
817/// }
818///
819/// #[http3_version]
820/// async fn standalone_http3_version_handler(ctx: &mut Context) {}
821/// ```
822///
823/// The macro takes no parameters and should be applied directly to async functions
824/// that accept a `&mut Context` parameter.
825#[proc_macro_attribute]
826pub fn http3_version(_attr: TokenStream, item: TokenStream) -> TokenStream {
827 http3_version_macro(item, Position::Prologue)
828}
829
830/// Restricts function execution to HTTP/1.1 or higher protocol versions.
831///
832/// This attribute macro ensures the decorated function only executes for HTTP/1.1
833/// or newer protocol versions, including HTTP/2, HTTP/3, and future versions.
834///
835/// # Usage
836///
837/// ```rust
838/// use hyperlane::*;
839/// use hyperlane_macros::*;
840///
841/// #[route("/http1_1_or_higher_version")]
842/// struct Http11OrHigher;
843///
844/// impl ServerHook for Http11OrHigher {
845/// async fn new(_ctx: &mut Context) -> Self {
846/// Self
847/// }
848///
849/// #[prologue_macros(http1_1_or_higher_version, response_body("http1_1_or_higher_version"))]
850/// async fn handle(self, ctx: &mut Context) {}
851/// }
852///
853/// impl Http11OrHigher {
854/// #[http1_1_or_higher_version]
855/// async fn http1_1_or_higher_version_with_ref_self(&self, ctx: &mut Context) {}
856/// }
857///
858/// #[http1_1_or_higher_version]
859/// async fn standalone_http1_1_or_higher_version_handler(ctx: &mut Context) {}
860/// ```
861///
862/// The macro takes no parameters and should be applied directly to async functions
863/// that accept a `&mut Context` parameter.
864#[proc_macro_attribute]
865pub fn http1_1_or_higher_version(_attr: TokenStream, item: TokenStream) -> TokenStream {
866 http1_1_or_higher_version_macro(item, Position::Prologue)
867}
868
869/// Restricts function execution to standard HTTP requests only.
870///
871/// This attribute macro ensures the decorated function only executes for standard HTTP requests,
872/// excluding WebSocket upgrades and other protocol upgrade requests.
873///
874/// # Usage
875///
876/// ```rust
877/// use hyperlane::*;
878/// use hyperlane_macros::*;
879///
880/// #[route("/http")]
881/// struct HttpOnly;
882///
883/// impl ServerHook for HttpOnly {
884/// async fn new(_ctx: &mut Context) -> Self {
885/// Self
886/// }
887///
888/// #[prologue_macros(http_version, response_body("http"))]
889/// async fn handle(self, ctx: &mut Context) {}
890/// }
891///
892/// impl HttpOnly {
893/// #[http_version]
894/// async fn http_with_ref_self(&self, ctx: &mut Context) {}
895/// }
896///
897/// #[http_version]
898/// async fn standalone_http_handler(ctx: &mut Context) {}
899/// ```
900///
901/// The macro takes no parameters and should be applied directly to async functions
902/// that accept a `&mut Context` parameter.
903#[proc_macro_attribute]
904pub fn http_version(_attr: TokenStream, item: TokenStream) -> TokenStream {
905 http_version_macro(item, Position::Prologue)
906}
907
908/// Restricts function execution to requests with unknown HTTP versions only.
909///
910/// This attribute macro ensures the decorated function only executes when the incoming request
911/// uses an unrecognized or non-standard HTTP version (not HTTP/0.9, HTTP/1.0, HTTP/1.1, HTTP/2, or HTTP/3).
912///
913/// # Usage
914///
915/// ```rust
916/// use hyperlane::*;
917/// use hyperlane_macros::*;
918///
919/// #[route("/unknown_version")]
920/// struct UnknownVersionHandler;
921///
922/// impl ServerHook for UnknownVersionHandler {
923/// async fn new(_ctx: &mut Context) -> Self {
924/// Self
925/// }
926///
927/// #[prologue_macros(unknown_version, response_body("unknown version"))]
928/// async fn handle(self, ctx: &mut Context) {}
929/// }
930///
931/// impl UnknownVersionHandler {
932/// #[unknown_version]
933/// async fn handle_unknown_with_ref_self(&self, ctx: &mut Context) {}
934/// }
935///
936/// #[unknown_version]
937/// async fn standalone_unknown_version_handler(ctx: &mut Context) {}
938/// ```
939///
940/// The macro takes no parameters and should be applied directly to async functions
941/// that accept a `&mut Context` parameter.
942#[proc_macro_attribute]
943pub fn unknown_version(_attr: TokenStream, item: TokenStream) -> TokenStream {
944 unknown_version_macro(item, Position::Prologue)
945}
946
947/// Restricts function execution to WebSocket upgrade requests only.
948///
949/// This attribute macro ensures the decorated function only executes when the incoming request
950/// is a valid WebSocket upgrade request with proper request headers and protocol negotiation.
951///
952/// # Usage
953///
954/// ```rust
955/// use hyperlane::*;
956/// use hyperlane_macros::*;
957///
958/// #[route("/ws_upgrade_type")]
959/// struct Websocket;
960///
961/// impl ServerHook for Websocket {
962/// async fn new(_ctx: &mut Context) -> Self {
963/// Self
964/// }
965///
966/// #[ws_upgrade_type]
967/// #[ws_from_stream]
968/// async fn handle(self, ctx: &mut Context) {
969/// let body: &RequestBody = ctx.get_request().get_body();
970/// let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(&body);
971/// ctx.send_body_list_with_data(&body_list).await;
972/// }
973/// }
974///
975/// impl Websocket {
976/// #[ws_upgrade_type]
977/// async fn ws_with_ref_self(&self, ctx: &mut Context) {}
978/// }
979///
980/// #[ws_upgrade_type]
981/// async fn standalone_ws_handler(ctx: &mut Context) {}
982/// ```
983///
984/// The macro takes no parameters and should be applied directly to async functions
985/// that accept a `&mut Context` parameter.
986#[proc_macro_attribute]
987pub fn ws_upgrade_type(_attr: TokenStream, item: TokenStream) -> TokenStream {
988 ws_upgrade_type_macro(item, Position::Prologue)
989}
990
991/// Restricts function execution to HTTP/2 Cleartext (h2c_upgrade_type) requests only.
992///
993/// This attribute macro ensures the decorated function only executes for HTTP/2 cleartext
994/// requests that use the h2c_upgrade_type upgrade mechanism.
995///
996/// # Usage
997///
998/// ```rust
999/// use hyperlane::*;
1000/// use hyperlane_macros::*;
1001///
1002/// #[route("/h2c_upgrade_type")]
1003/// struct H2c;
1004///
1005/// impl ServerHook for H2c {
1006/// async fn new(_ctx: &mut Context) -> Self {
1007/// Self
1008/// }
1009///
1010/// #[prologue_macros(h2c_upgrade_type, response_body("h2c_upgrade_type"))]
1011/// async fn handle(self, ctx: &mut Context) {}
1012/// }
1013///
1014/// impl H2c {
1015/// #[h2c_upgrade_type]
1016/// async fn h2c_upgrade_type_with_ref_self(&self, ctx: &mut Context) {}
1017/// }
1018///
1019/// #[h2c_upgrade_type]
1020/// async fn standalone_h2c_upgrade_type_handler(ctx: &mut Context) {}
1021/// ```
1022///
1023/// The macro takes no parameters and should be applied directly to async functions
1024/// that accept a `&mut Context` parameter.
1025#[proc_macro_attribute]
1026pub fn h2c_upgrade_type(_attr: TokenStream, item: TokenStream) -> TokenStream {
1027 h2c_upgrade_type_macro(item, Position::Prologue)
1028}
1029
1030/// Restricts function execution to TLS-encrypted requests only.
1031///
1032/// This attribute macro ensures the decorated function only executes for requests
1033/// that use TLS/SSL encryption on the connection.
1034///
1035/// # Usage
1036///
1037/// ```rust
1038/// use hyperlane::*;
1039/// use hyperlane_macros::*;
1040///
1041/// #[route("/tls_upgrade_type")]
1042/// struct Tls;
1043///
1044/// impl ServerHook for Tls {
1045/// async fn new(_ctx: &mut Context) -> Self {
1046/// Self
1047/// }
1048///
1049/// #[prologue_macros(tls_upgrade_type, response_body("tls_upgrade_type"))]
1050/// async fn handle(self, ctx: &mut Context) {}
1051/// }
1052///
1053/// impl Tls {
1054/// #[tls_upgrade_type]
1055/// async fn tls_upgrade_type_with_ref_self(&self, ctx: &mut Context) {}
1056/// }
1057///
1058/// #[tls_upgrade_type]
1059/// async fn standalone_tls_upgrade_type_handler(ctx: &mut Context) {}
1060/// ```
1061///
1062/// The macro takes no parameters and should be applied directly to async functions
1063/// that accept a `&mut Context` parameter.
1064#[proc_macro_attribute]
1065pub fn tls_upgrade_type(_attr: TokenStream, item: TokenStream) -> TokenStream {
1066 tls_upgrade_type_macro(item, Position::Prologue)
1067}
1068
1069/// Restricts function execution to requests with unknown protocol upgrade types only.
1070///
1071/// This attribute macro ensures the decorated function only executes when the incoming request
1072/// uses an unrecognized or non-standard protocol upgrade type (not WebSocket, h2c, or TLS).
1073///
1074/// # Usage
1075///
1076/// ```rust
1077/// use hyperlane::*;
1078/// use hyperlane_macros::*;
1079///
1080/// #[route("/unknown_upgrade_type")]
1081/// struct UnknownUpgrade;
1082///
1083/// impl ServerHook for UnknownUpgrade {
1084/// async fn new(_ctx: &mut Context) -> Self {
1085/// Self
1086/// }
1087///
1088/// #[prologue_macros(unknown_upgrade_type, response_body("unknown upgrade type"))]
1089/// async fn handle(self, ctx: &mut Context) {}
1090/// }
1091///
1092/// impl UnknownUpgrade {
1093/// #[unknown_upgrade_type]
1094/// async fn unknown_upgrade_type_with_ref_self(&self, ctx: &mut Context) {}
1095/// }
1096///
1097/// #[unknown_upgrade_type]
1098/// async fn standalone_unknown_upgrade_type_handler(ctx: &mut Context) {}
1099/// ```
1100///
1101/// The macro takes no parameters and should be applied directly to async functions
1102/// that accept a `&mut Context` parameter.
1103#[proc_macro_attribute]
1104pub fn unknown_upgrade_type(_attr: TokenStream, item: TokenStream) -> TokenStream {
1105 unknown_upgrade_type_macro(item, Position::Prologue)
1106}
1107
1108/// Sets the HTTP status code for the response.
1109///
1110/// This attribute macro configures the HTTP status code that will be sent with the response.
1111/// The status code can be provided as a numeric literal or a global constant.
1112///
1113/// # Usage
1114///
1115/// ```rust
1116/// use hyperlane::*;
1117/// use hyperlane_macros::*;
1118///
1119/// const CUSTOM_STATUS_CODE: i32 = 200;
1120///
1121/// #[route("/response_status_code")]
1122/// struct ResponseStatusCode;
1123///
1124/// impl ServerHook for ResponseStatusCode {
1125/// async fn new(_ctx: &mut Context) -> Self {
1126/// Self
1127/// }
1128///
1129/// #[response_status_code(CUSTOM_STATUS_CODE)]
1130/// async fn handle(self, ctx: &mut Context) {}
1131/// }
1132///
1133/// impl ResponseStatusCode {
1134/// #[response_status_code(CUSTOM_STATUS_CODE)]
1135/// async fn response_status_code_with_ref_self(&self, ctx: &mut Context) {}
1136/// }
1137///
1138/// #[response_status_code(200)]
1139/// async fn standalone_response_status_code_handler(ctx: &mut Context) {}
1140/// ```
1141///
1142/// The macro accepts a numeric HTTP status code or a global constant
1143/// and should be applied to async functions that accept a `&mut Context` parameter.
1144#[proc_macro_attribute]
1145pub fn response_status_code(attr: TokenStream, item: TokenStream) -> TokenStream {
1146 response_status_code_macro(attr, item, Position::Prologue)
1147}
1148
1149/// Sets the HTTP reason phrase for the response.
1150///
1151/// This attribute macro configures the HTTP reason phrase that accompanies the status code.
1152/// The reason phrase can be provided as a string literal or a global constant.
1153///
1154/// # Usage
1155///
1156/// ```rust
1157/// use hyperlane::*;
1158/// use hyperlane_macros::*;
1159///
1160/// const CUSTOM_REASON: &str = "Accepted";
1161///
1162/// #[route("/response_reason")]
1163/// struct ResponseReason;
1164///
1165/// impl ServerHook for ResponseReason {
1166/// async fn new(_ctx: &mut Context) -> Self {
1167/// Self
1168/// }
1169///
1170/// #[response_reason_phrase(CUSTOM_REASON)]
1171/// async fn handle(self, ctx: &mut Context) {}
1172/// }
1173///
1174/// impl ResponseReason {
1175/// #[response_reason_phrase(CUSTOM_REASON)]
1176/// async fn response_reason_phrase_with_ref_self(&self, ctx: &mut Context) {}
1177/// }
1178///
1179/// #[response_reason_phrase("OK")]
1180/// async fn standalone_response_reason_phrase_handler(ctx: &mut Context) {}
1181/// ```
1182///
1183/// The macro accepts a string literal or global constant for the reason phrase and should be
1184/// applied to async functions that accept a `&mut Context` parameter.
1185#[proc_macro_attribute]
1186pub fn response_reason_phrase(attr: TokenStream, item: TokenStream) -> TokenStream {
1187 response_reason_phrase_macro(attr, item, Position::Prologue)
1188}
1189
1190/// Sets or replaces a specific HTTP response header.
1191///
1192/// This attribute macro configures a specific HTTP response header that will be sent with the response.
1193/// Both the header name and value can be provided as string literals or global constants.
1194/// Use `"key", "value"` to set a header (add to existing headers) or `"key" => "value"` to replace a header (overwrite existing).
1195///
1196/// # Usage
1197///
1198/// ```rust
1199/// use hyperlane::*;
1200/// use hyperlane_macros::*;
1201///
1202/// const CUSTOM_HEADER_NAME: &str = "X-Custom-Header";
1203/// const CUSTOM_HEADER_VALUE: &str = "custom-value";
1204///
1205/// #[route("/response_header")]
1206/// struct ResponseHeader;
1207///
1208/// impl ServerHook for ResponseHeader {
1209/// async fn new(_ctx: &mut Context) -> Self {
1210/// Self
1211/// }
1212///
1213/// #[response_header(CUSTOM_HEADER_NAME => CUSTOM_HEADER_VALUE)]
1214/// async fn handle(self, ctx: &mut Context) {}
1215/// }
1216///
1217/// impl ResponseHeader {
1218/// #[response_header(CUSTOM_HEADER_NAME => CUSTOM_HEADER_VALUE)]
1219/// async fn response_header_with_ref_self(&self, ctx: &mut Context) {}
1220/// }
1221///
1222/// #[route("/response_header")]
1223/// struct ResponseHeaderTest;
1224///
1225/// impl ServerHook for ResponseHeaderTest {
1226/// async fn new(_ctx: &mut Context) -> Self {
1227/// Self
1228/// }
1229///
1230/// #[response_body("Testing header set and replace operations")]
1231/// #[response_header("X-Add-Header", "add-value")]
1232/// #[response_header("X-Set-Header" => "set-value")]
1233/// async fn handle(self, ctx: &mut Context) {}
1234/// }
1235///
1236/// #[response_header("X-Custom" => "value")]
1237/// async fn standalone_response_header_handler(ctx: &mut Context) {}
1238/// ```
1239///
1240/// The macro accepts header name and header value, both can be string literals or global constants.
1241/// Use `"key", "value"` for setting headers and `"key" => "value"` for replacing headers.
1242/// Should be applied to async functions that accept a `&mut Context` parameter.
1243#[proc_macro_attribute]
1244pub fn response_header(attr: TokenStream, item: TokenStream) -> TokenStream {
1245 response_header_macro(attr, item, Position::Prologue)
1246}
1247
1248/// Sets the HTTP response body.
1249///
1250/// This attribute macro configures the HTTP response body that will be sent with the response.
1251/// The body content can be provided as a string literal or a global constant.
1252///
1253/// # Usage
1254///
1255/// ```rust
1256/// use hyperlane::*;
1257/// use hyperlane_macros::*;
1258///
1259/// const RESPONSE_DATA: &str = "{\"status\": \"success\"}";
1260///
1261/// #[route("/response_body")]
1262/// struct ResponseBody;
1263///
1264/// impl ServerHook for ResponseBody {
1265/// async fn new(_ctx: &mut Context) -> Self {
1266/// Self
1267/// }
1268///
1269/// #[response_body(&RESPONSE_DATA)]
1270/// async fn handle(self, ctx: &mut Context) {}
1271/// }
1272///
1273/// impl ResponseBody {
1274/// #[response_body(&RESPONSE_DATA)]
1275/// async fn response_body_with_ref_self(&self, ctx: &mut Context) {}
1276/// }
1277///
1278/// #[response_body("standalone response body")]
1279/// async fn standalone_response_body_handler(ctx: &mut Context) {}
1280/// ```
1281///
1282/// The macro accepts a string literal or global constant for the response body and should be
1283/// applied to async functions that accept a `&mut Context` parameter.
1284#[proc_macro_attribute]
1285pub fn response_body(attr: TokenStream, item: TokenStream) -> TokenStream {
1286 response_body_macro(attr, item, Position::Prologue)
1287}
1288
1289/// Clears all response headers.
1290///
1291/// This attribute macro clears all response headers from the response.
1292///
1293/// # Usage
1294///
1295/// ```rust
1296/// use hyperlane::*;
1297/// use hyperlane_macros::*;
1298///
1299/// #[route("/clear_response_headers")]
1300/// struct ClearResponseHeaders;
1301///
1302/// impl ServerHook for ClearResponseHeaders {
1303/// async fn new(_ctx: &mut Context) -> Self {
1304/// Self
1305/// }
1306///
1307/// #[prologue_macros(
1308/// clear_response_headers,
1309/// filter(ctx.get_request().get_method().is_unknown()),
1310/// response_body("clear_response_headers")
1311/// )]
1312/// async fn handle(self, ctx: &mut Context) {}
1313/// }
1314///
1315/// impl ClearResponseHeaders {
1316/// #[clear_response_headers]
1317/// async fn clear_response_headers_with_ref_self(&self, ctx: &mut Context) {}
1318/// }
1319///
1320/// #[clear_response_headers]
1321/// async fn standalone_clear_response_headers_handler(ctx: &mut Context) {}
1322/// ```
1323///
1324/// The macro should be applied to async functions that accept a `&mut Context` parameter.
1325#[proc_macro_attribute]
1326pub fn clear_response_headers(_attr: TokenStream, item: TokenStream) -> TokenStream {
1327 clear_response_headers_macro(item, Position::Prologue)
1328}
1329
1330/// Sets the HTTP response version.
1331///
1332/// This attribute macro configures the HTTP response version that will be sent with the response.
1333/// The version can be provided as a variable or code block.
1334///
1335/// # Usage
1336///
1337/// ```rust
1338/// use hyperlane::*;
1339/// use hyperlane_macros::*;
1340///
1341/// #[request_middleware]
1342/// struct RequestMiddleware;
1343///
1344/// impl ServerHook for RequestMiddleware {
1345/// async fn new(_ctx: &mut Context) -> Self {
1346/// Self
1347/// }
1348///
1349/// #[epilogue_macros(
1350/// response_status_code(200),
1351/// response_version(HttpVersion::Http1_1),
1352/// response_header(SERVER => HYPERLANE)
1353/// )]
1354/// async fn handle(self, ctx: &mut Context) {}
1355/// }
1356///
1357/// impl RequestMiddleware {
1358/// #[response_version(HttpVersion::Http2)]
1359/// async fn response_version_with_ref_self(&self, ctx: &mut Context) {}
1360/// }
1361///
1362/// #[response_version(HttpVersion::Http1_0)]
1363/// async fn standalone_response_version_handler(ctx: &mut Context) {}
1364/// ```
1365///
1366/// The macro accepts a variable or code block for the response version and should be
1367/// applied to async functions that accept a `&mut Context` parameter.
1368#[proc_macro_attribute]
1369pub fn response_version(attr: TokenStream, item: TokenStream) -> TokenStream {
1370 response_version_macro(attr, item, Position::Prologue)
1371}
1372
1373/// Handles aborted request scenarios.
1374///
1375/// This attribute macro configures the function to handle cases where the client has
1376/// aborted the request, providing appropriate handling for interrupted or cancelled requests.
1377///
1378/// # Usage
1379///
1380/// ```rust
1381/// use hyperlane::*;
1382/// use hyperlane_macros::*;
1383///
1384/// #[route("/aborted")]
1385/// struct Aborted;
1386///
1387/// impl ServerHook for Aborted {
1388/// async fn new(_ctx: &mut Context) -> Self {
1389/// Self
1390/// }
1391///
1392/// #[aborted]
1393/// async fn handle(self, ctx: &mut Context) {}
1394/// }
1395///
1396/// impl Aborted {
1397/// #[aborted]
1398/// async fn aborted_with_ref_self(&self, ctx: &mut Context) {}
1399/// }
1400///
1401/// #[aborted]
1402/// async fn standalone_aborted_handler(ctx: &mut Context) {}
1403/// ```
1404///
1405/// The macro takes no parameters and should be applied directly to async functions
1406/// that accept a `&mut Context` parameter.
1407#[proc_macro_attribute]
1408pub fn aborted(_attr: TokenStream, item: TokenStream) -> TokenStream {
1409 aborted_macro(item, Position::Prologue)
1410}
1411
1412/// Handles closed connection scenarios.
1413///
1414/// This attribute macro configures the function to handle cases where the connection
1415/// has been closed, providing appropriate handling for terminated or disconnected connections.
1416///
1417/// # Usage
1418///
1419/// ```rust
1420/// use hyperlane::*;
1421/// use hyperlane_macros::*;
1422///
1423/// #[route("/closed")]
1424/// struct ClosedTest;
1425///
1426/// impl ServerHook for ClosedTest {
1427/// async fn new(_ctx: &mut Context) -> Self {
1428/// Self
1429/// }
1430///
1431/// #[closed]
1432/// async fn handle(self, ctx: &mut Context) {}
1433/// }
1434///
1435/// impl ClosedTest {
1436/// #[closed]
1437/// async fn closed_with_ref_self(&self, ctx: &mut Context) {}
1438/// }
1439///
1440/// #[closed]
1441/// async fn standalone_closed_handler(ctx: &mut Context) {}
1442/// ```
1443///
1444/// The macro takes no parameters and should be applied directly to async functions
1445/// that accept a `&mut Context` parameter.
1446#[proc_macro_attribute]
1447pub fn closed(_attr: TokenStream, item: TokenStream) -> TokenStream {
1448 closed_macro(item, Position::Prologue)
1449}
1450
1451/// Filters requests based on a boolean condition.
1452///
1453/// The function continues execution only if the provided code block returns `true`.
1454///
1455/// # Usage
1456///
1457/// ```rust
1458/// use hyperlane::*;
1459/// use hyperlane_macros::*;
1460///
1461/// #[route("/unknown_method")]
1462/// struct UnknownMethod;
1463///
1464/// impl ServerHook for UnknownMethod {
1465/// async fn new(_ctx: &mut Context) -> Self {
1466/// Self
1467/// }
1468///
1469/// #[prologue_macros(
1470/// filter(ctx.get_request().get_method().is_unknown()),
1471/// response_body("unknown_method")
1472/// )]
1473/// async fn handle(self, ctx: &mut Context) {}
1474/// }
1475///
1476/// impl UnknownMethod {
1477/// #[filter(true)]
1478/// async fn filter_with_ref_self(&self, ctx: &mut Context) {}
1479/// }
1480///
1481/// #[filter(true)]
1482/// async fn standalone_filter_handler(ctx: &mut Context) {}
1483/// ```
1484#[proc_macro_attribute]
1485pub fn filter(attr: TokenStream, item: TokenStream) -> TokenStream {
1486 filter_macro(attr, item, Position::Prologue)
1487}
1488
1489/// Rejects requests based on a boolean condition.
1490///
1491/// The function continues execution only if the provided code block returns `false`.
1492///
1493/// # Usage
1494///
1495/// ```rust
1496/// use hyperlane::*;
1497/// use hyperlane_macros::*;
1498///
1499/// #[response_middleware(2)]
1500/// struct ResponseMiddleware2;
1501///
1502/// impl ServerHook for ResponseMiddleware2 {
1503/// async fn new(_ctx: &mut Context) -> Self {
1504/// Self
1505/// }
1506///
1507/// #[prologue_macros(
1508/// reject(ctx.get_request().get_upgrade_type().is_ws())
1509/// )]
1510/// async fn handle(self, ctx: &mut Context) {}
1511/// }
1512///
1513/// impl ResponseMiddleware2 {
1514/// #[reject(false)]
1515/// async fn reject_with_ref_self(&self, ctx: &mut Context) {}
1516/// }
1517///
1518/// #[reject(false)]
1519/// async fn standalone_reject_handler(ctx: &mut Context) {}
1520/// ```
1521#[proc_macro_attribute]
1522pub fn reject(attr: TokenStream, item: TokenStream) -> TokenStream {
1523 reject_macro(attr, item, Position::Prologue)
1524}
1525
1526/// Restricts function execution to requests with a specific host.
1527///
1528/// This attribute macro ensures the decorated function only executes when the incoming request
1529/// has a host header that matches the specified value. Requests with different or missing host headers will be filtered out.
1530///
1531/// # Usage
1532///
1533/// ```rust
1534/// use hyperlane::*;
1535/// use hyperlane_macros::*;
1536///
1537/// #[route("/host")]
1538/// struct Host;
1539///
1540/// impl ServerHook for Host {
1541/// async fn new(_ctx: &mut Context) -> Self {
1542/// Self
1543/// }
1544///
1545/// #[host("localhost")]
1546/// #[prologue_macros(response_body("host string literal: localhost"), send)]
1547/// async fn handle(self, ctx: &mut Context) {}
1548/// }
1549///
1550/// impl Host {
1551/// #[host("localhost")]
1552/// async fn host_with_ref_self(&self, ctx: &mut Context) {}
1553/// }
1554///
1555/// #[host("localhost")]
1556/// async fn standalone_host_handler(ctx: &mut Context) {}
1557/// ```
1558///
1559/// The macro accepts a string literal specifying the expected host value and should be
1560/// applied to async functions that accept a `&mut Context` parameter.
1561#[proc_macro_attribute]
1562pub fn host(attr: TokenStream, item: TokenStream) -> TokenStream {
1563 host_macro(attr, item, Position::Prologue)
1564}
1565
1566/// Reject requests that have no host header.
1567///
1568/// This attribute macro ensures the decorated function only executes when the incoming request
1569/// has a host header present. Requests without a host header will be filtered out.
1570///
1571/// # Usage
1572///
1573/// ```rust
1574/// use hyperlane::*;
1575/// use hyperlane_macros::*;
1576///
1577/// #[route("/reject_host")]
1578/// struct RejectHost;
1579///
1580/// impl ServerHook for RejectHost {
1581/// async fn new(_ctx: &mut Context) -> Self {
1582/// Self
1583/// }
1584///
1585/// #[prologue_macros(
1586/// reject_host("filter.localhost"),
1587/// response_body("host filter string literal")
1588/// )]
1589/// async fn handle(self, ctx: &mut Context) {}
1590/// }
1591///
1592/// impl RejectHost {
1593/// #[reject_host("filter.localhost")]
1594/// async fn reject_host_with_ref_self(&self, ctx: &mut Context) {}
1595/// }
1596///
1597/// #[reject_host("filter.localhost")]
1598/// async fn standalone_reject_host_handler(ctx: &mut Context) {}
1599/// ```
1600///
1601/// The macro takes no parameters and should be applied directly to async functions
1602/// that accept a `&mut Context` parameter.
1603#[proc_macro_attribute]
1604pub fn reject_host(attr: TokenStream, item: TokenStream) -> TokenStream {
1605 reject_host_macro(attr, item, Position::Prologue)
1606}
1607
1608/// Restricts function execution to requests with a specific referer.
1609///
1610/// This attribute macro ensures the decorated function only executes when the incoming request
1611/// has a referer header that matches the specified value. Requests with different or missing referer headers will be filtered out.
1612///
1613/// # Usage
1614///
1615/// ```rust
1616/// use hyperlane::*;
1617/// use hyperlane_macros::*;
1618///
1619/// #[route("/referer")]
1620/// struct Referer;
1621///
1622/// impl ServerHook for Referer {
1623/// async fn new(_ctx: &mut Context) -> Self {
1624/// Self
1625/// }
1626///
1627/// #[prologue_macros(
1628/// referer("http://localhost"),
1629/// response_body("referer string literal: http://localhost")
1630/// )]
1631/// async fn handle(self, ctx: &mut Context) {}
1632/// }
1633///
1634/// impl Referer {
1635/// #[referer("http://localhost")]
1636/// async fn referer_with_ref_self(&self, ctx: &mut Context) {}
1637/// }
1638///
1639/// #[referer("http://localhost")]
1640/// async fn standalone_referer_handler(ctx: &mut Context) {}
1641/// ```
1642///
1643/// The macro accepts a string literal specifying the expected referer value and should be
1644/// applied to async functions that accept a `&mut Context` parameter.
1645#[proc_macro_attribute]
1646pub fn referer(attr: TokenStream, item: TokenStream) -> TokenStream {
1647 referer_macro(attr, item, Position::Prologue)
1648}
1649
1650/// Reject requests that have a specific referer header.
1651///
1652/// This attribute macro ensures the decorated function only executes when the incoming request
1653/// does not have a referer header that matches the specified value. Requests with the matching referer header will be filtered out.
1654///
1655/// # Usage
1656///
1657/// ```rust
1658/// use hyperlane::*;
1659/// use hyperlane_macros::*;
1660///
1661/// #[route("/reject_referer")]
1662/// struct RejectReferer;
1663///
1664/// impl ServerHook for RejectReferer {
1665/// async fn new(_ctx: &mut Context) -> Self {
1666/// Self
1667/// }
1668///
1669/// #[prologue_macros(
1670/// reject_referer("http://localhost"),
1671/// response_body("referer filter string literal")
1672/// )]
1673/// async fn handle(self, ctx: &mut Context) {}
1674/// }
1675///
1676/// impl RejectReferer {
1677/// #[reject_referer("http://localhost")]
1678/// async fn reject_referer_with_ref_self(&self, ctx: &mut Context) {}
1679/// }
1680///
1681/// #[reject_referer("http://localhost")]
1682/// async fn standalone_reject_referer_handler(ctx: &mut Context) {}
1683/// ```
1684///
1685/// The macro accepts a string literal specifying the referer value to filter out and should be
1686/// applied to async functions that accept a `&mut Context` parameter.
1687#[proc_macro_attribute]
1688pub fn reject_referer(attr: TokenStream, item: TokenStream) -> TokenStream {
1689 reject_referer_macro(attr, item, Position::Prologue)
1690}
1691
1692/// Executes multiple specified functions before the main handler function.
1693///
1694/// This attribute macro configures multiple pre-execution hooks that run before the main function logic.
1695/// The specified hook functions will be called in the order provided, followed by the main function execution.
1696///
1697/// # Usage
1698///
1699/// ```rust
1700/// use hyperlane::*;
1701/// use hyperlane_macros::*;
1702///
1703/// struct PrologueHooks;
1704///
1705/// impl ServerHook for PrologueHooks {
1706/// async fn new(_ctx: &mut Context) -> Self {
1707/// Self
1708/// }
1709///
1710/// #[get_method]
1711/// #[http_version]
1712/// async fn handle(self, _ctx: &mut Context) {}
1713/// }
1714///
1715/// async fn prologue_hooks_fn(ctx: &mut Context) {
1716/// let hook = PrologueHooks::new(ctx).await;
1717/// hook.handle(ctx).await;
1718/// }
1719///
1720/// #[route("/hook")]
1721/// struct Hook;
1722///
1723/// impl ServerHook for Hook {
1724/// async fn new(_ctx: &mut Context) -> Self {
1725/// Self
1726/// }
1727///
1728/// #[prologue_hooks(prologue_hooks_fn)]
1729/// #[response_body("Testing hook macro")]
1730/// async fn handle(self, ctx: &mut Context) {}
1731/// }
1732/// ```
1733///
1734/// The macro accepts a comma-separated list of function names as parameters. All hook functions
1735/// and the main function must accept a `Context` parameter. Avoid combining this macro with other
1736/// macros on the same function to prevent macro expansion conflicts.
1737///
1738/// # Advanced Usage with Method Expressions
1739///
1740/// ```rust
1741/// use hyperlane::*;
1742/// use hyperlane_macros::*;
1743///
1744/// #[route("/hooks_expression")]
1745/// struct HooksExpression;
1746///
1747/// impl ServerHook for HooksExpression {
1748/// async fn new(_ctx: &mut Context) -> Self {
1749/// Self
1750/// }
1751///
1752/// #[get_method]
1753/// #[prologue_hooks(HooksExpression::new_hook, HooksExpression::method_hook)]
1754/// #[response_body("hooks expression test")]
1755/// async fn handle(self, ctx: &mut Context) {}
1756/// }
1757///
1758/// impl HooksExpression {
1759/// async fn new_hook(_ctx: &mut Context) {}
1760///
1761/// async fn method_hook(_ctx: &mut Context) {}
1762/// }
1763/// ```
1764#[proc_macro_attribute]
1765pub fn prologue_hooks(attr: TokenStream, item: TokenStream) -> TokenStream {
1766 prologue_hooks_macro(attr, item, Position::Prologue)
1767}
1768
1769/// Executes multiple specified functions after the main handler function.
1770///
1771/// This attribute macro configures multiple post-execution hooks that run after the main function logic.
1772/// The main function will execute first, followed by the specified hook functions in the order provided.
1773///
1774/// # Usage
1775///
1776/// ```rust
1777/// use hyperlane::*;
1778/// use hyperlane_macros::*;
1779///
1780/// struct EpilogueHooks;
1781///
1782/// impl ServerHook for EpilogueHooks {
1783/// async fn new(_ctx: &mut Context) -> Self {
1784/// Self
1785/// }
1786///
1787/// #[response_status_code(200)]
1788/// async fn handle(self, ctx: &mut Context) {}
1789/// }
1790///
1791/// async fn epilogue_hooks_fn(ctx: &mut Context) {
1792/// let hook = EpilogueHooks::new(ctx).await;
1793/// hook.handle(ctx).await;
1794/// }
1795///
1796/// #[route("/hook")]
1797/// struct Hook;
1798///
1799/// impl ServerHook for Hook {
1800/// async fn new(_ctx: &mut Context) -> Self {
1801/// Self
1802/// }
1803///
1804/// #[epilogue_hooks(epilogue_hooks_fn)]
1805/// #[response_body("Testing hook macro")]
1806/// async fn handle(self, ctx: &mut Context) {}
1807/// }
1808///
1809/// ```
1810///
1811/// The macro accepts a comma-separated list of function names as parameters. All hook functions
1812/// and the main function must accept a `Context` parameter. Avoid combining this macro with other
1813/// macros on the same function to prevent macro expansion conflicts.
1814///
1815/// # Advanced Usage with Method Expressions
1816///
1817/// ```rust
1818/// use hyperlane::*;
1819/// use hyperlane_macros::*;
1820///
1821/// #[route("/hooks_expression")]
1822/// struct HooksExpression;
1823///
1824/// impl ServerHook for HooksExpression {
1825/// async fn new(_ctx: &mut Context) -> Self {
1826/// Self
1827/// }
1828///
1829/// #[get_method]
1830/// #[epilogue_hooks(HooksExpression::new_hook, HooksExpression::method_hook)]
1831/// #[response_body("hooks expression test")]
1832/// async fn handle(self, ctx: &mut Context) {}
1833/// }
1834///
1835/// impl HooksExpression {
1836/// async fn new_hook(_ctx: &mut Context) {}
1837///
1838/// async fn method_hook(_ctx: &mut Context) {}
1839/// }
1840/// ```
1841#[proc_macro_attribute]
1842pub fn epilogue_hooks(attr: TokenStream, item: TokenStream) -> TokenStream {
1843 epilogue_hooks_macro(attr, item, Position::Epilogue)
1844}
1845
1846/// Extracts the raw request body into a specified variable.
1847///
1848/// This attribute macro extracts the raw request body content into a variable
1849/// with the fixed type `RequestBody`. The body content is not parsed or deserialized.
1850///
1851/// # Usage
1852///
1853/// ```rust
1854/// use hyperlane::*;
1855/// use hyperlane_macros::*;
1856///
1857/// #[route("/request_body")]
1858/// struct RequestBodyRoute;
1859///
1860/// impl ServerHook for RequestBodyRoute {
1861/// async fn new(_ctx: &mut Context) -> Self {
1862/// Self
1863/// }
1864///
1865/// #[response_body(&format!("raw body: {raw_body:?}"))]
1866/// #[request_body(raw_body)]
1867/// async fn handle(self, ctx: &mut Context) {}
1868/// }
1869///
1870/// impl RequestBodyRoute {
1871/// #[request_body(raw_body)]
1872/// async fn request_body_with_ref_self(&self, ctx: &mut Context) {}
1873/// }
1874///
1875/// #[request_body(raw_body)]
1876/// async fn standalone_request_body_handler(ctx: &mut Context) {}
1877/// ```
1878///
1879/// # Multi-Parameter Usage
1880///
1881/// ```rust
1882/// use hyperlane::*;
1883/// use hyperlane_macros::*;
1884///
1885/// #[route("/multi_body")]
1886/// struct MultiBody;
1887///
1888/// impl ServerHook for MultiBody {
1889/// async fn new(_ctx: &mut Context) -> Self {
1890/// Self
1891/// }
1892///
1893/// #[response_body(&format!("bodies: {body1:?}, {body2:?}"))]
1894/// #[request_body(body1, body2)]
1895/// async fn handle(self, ctx: &mut Context) {}
1896/// }
1897/// ```
1898///
1899/// The macro accepts one or more variable names separated by commas.
1900/// Each variable will be available in the function scope as a `RequestBody` type.
1901#[proc_macro_attribute]
1902pub fn request_body(attr: TokenStream, item: TokenStream) -> TokenStream {
1903 request_body_macro(attr, item, Position::Prologue)
1904}
1905
1906/// Parses the request body as JSON into a specified variable and type with panic on parsing failure.
1907///
1908/// This attribute macro extracts and deserializes the request body content as JSON into a variable
1909/// with the specified type. The body content is parsed as JSON using serde.
1910/// If the request body does not exist or JSON parsing fails, the function will panic with an error message.
1911///
1912/// # Usage
1913///
1914/// ```rust
1915/// use hyperlane::*;
1916/// use hyperlane_macros::*;
1917/// use serde::{Deserialize, Serialize};
1918///
1919/// #[derive(Clone, Debug, Deserialize, Serialize)]
1920/// struct TestData {
1921/// name: String,
1922/// age: u32,
1923/// }
1924///
1925/// #[route("/request_body_json_result")]
1926/// struct RequestBodyJson;
1927///
1928/// impl ServerHook for RequestBodyJson {
1929/// async fn new(_ctx: &mut Context) -> Self {
1930/// Self
1931/// }
1932///
1933/// #[response_body(&format!("request data: {request_data_result:?}"))]
1934/// #[request_body_json_result(request_data_result: TestData)]
1935/// async fn handle(self, ctx: &mut Context) {}
1936/// }
1937///
1938/// impl RequestBodyJson {
1939/// #[request_body_json_result(request_data_result: TestData)]
1940/// async fn request_body_json_with_ref_self(&self, ctx: &mut Context) {}
1941/// }
1942///
1943/// #[request_body_json_result(request_data_result: TestData)]
1944/// async fn standalone_request_body_json_handler(ctx: &mut Context) {}
1945/// ```
1946///
1947/// # Multi-Parameter Usage
1948///
1949/// ```rust
1950/// use hyperlane::*;
1951/// use hyperlane_macros::*;
1952/// use serde::{Deserialize, Serialize};
1953///
1954/// #[derive(Clone, Debug, Deserialize, Serialize)]
1955/// struct User {
1956/// name: String,
1957/// }
1958///
1959/// #[derive(Clone, Debug, Deserialize, Serialize)]
1960/// struct Config {
1961/// debug: bool,
1962/// }
1963///
1964/// #[route("/request_body_json_result")]
1965/// struct TestData;
1966///
1967/// impl ServerHook for TestData {
1968/// async fn new(_ctx: &mut Context) -> Self {
1969/// Self
1970/// }
1971///
1972/// #[response_body(&format!("user: {user:?}, config: {config:?}"))]
1973/// #[request_body_json_result(user: User, config: Config)]
1974/// async fn handle(self, ctx: &mut Context) {}
1975/// }
1976/// ```
1977///
1978/// The macro accepts one or more `variable_name: Type` pairs separated by commas.
1979/// Each variable will be available in the function scope as a `Result<Type, serde_json::Error>`.
1980#[proc_macro_attribute]
1981pub fn request_body_json_result(attr: TokenStream, item: TokenStream) -> TokenStream {
1982 request_body_json_result_macro(attr, item, Position::Prologue)
1983}
1984
1985/// Parses the request body as JSON into a specified variable and type with panic on parsing failure.
1986///
1987/// This attribute macro extracts and deserializes the request body content as JSON into a variable
1988/// with the specified type. The body content is parsed as JSON using serde.
1989/// If the request body does not exist or JSON parsing fails, the function will panic with an error message.
1990///
1991/// # Usage
1992///
1993/// ```rust
1994/// use hyperlane::*;
1995/// use hyperlane_macros::*;
1996/// use serde::{Deserialize, Serialize};
1997///
1998/// #[derive(Clone, Debug, Deserialize, Serialize)]
1999/// struct TestData {
2000/// name: String,
2001/// age: u32,
2002/// }
2003///
2004/// #[route("/request_body_json")]
2005/// struct RequestBodyJson;
2006///
2007/// impl ServerHook for RequestBodyJson {
2008/// async fn new(_ctx: &mut Context) -> Self {
2009/// Self
2010/// }
2011///
2012/// #[response_body(&format!("request data: {request_data_result:?}"))]
2013/// #[request_body_json(request_data_result: TestData)]
2014/// async fn handle(self, ctx: &mut Context) {}
2015/// }
2016///
2017/// impl RequestBodyJson {
2018/// #[request_body_json(request_data_result: TestData)]
2019/// async fn request_body_json_with_ref_self(&self, ctx: &mut Context) {}
2020/// }
2021///
2022/// #[request_body_json(request_data_result: TestData)]
2023/// async fn standalone_request_body_json_handler(ctx: &mut Context) {}
2024/// ```
2025///
2026/// # Multi-Parameter Usage
2027///
2028/// ```rust
2029/// use hyperlane::*;
2030/// use hyperlane_macros::*;
2031/// use serde::{Deserialize, Serialize};
2032///
2033/// #[derive(Clone, Debug, Deserialize, Serialize)]
2034/// struct User {
2035/// name: String,
2036/// }
2037///
2038/// #[derive(Clone, Debug, Deserialize, Serialize)]
2039/// struct Config {
2040/// debug: bool,
2041/// }
2042///
2043/// #[route("/request_body_json")]
2044/// struct TestData;
2045///
2046/// impl ServerHook for TestData {
2047/// async fn new(_ctx: &mut Context) -> Self {
2048/// Self
2049/// }
2050///
2051/// #[response_body(&format!("user: {user:?}, config: {config:?}"))]
2052/// #[request_body_json(user: User, config: Config)]
2053/// async fn handle(self, ctx: &mut Context) {}
2054/// }
2055/// ```
2056///
2057/// The macro accepts one or more `variable_name: Type` pairs separated by commas.
2058/// Each variable will be available in the function scope as a `Result<Type, serde_json::Error>`.
2059///
2060/// # Panics
2061///
2062/// This macro will panic if the request body does not exist or JSON parsing fails.
2063#[proc_macro_attribute]
2064pub fn request_body_json(attr: TokenStream, item: TokenStream) -> TokenStream {
2065 request_body_json_macro(attr, item, Position::Prologue)
2066}
2067
2068/// Extracts a specific attribute value into a variable wrapped in Option type.
2069///
2070/// This attribute macro retrieves a specific attribute by key and makes it available
2071/// as a typed Option variable from the request context. The extracted value is wrapped
2072/// in an Option type to safely handle cases where the attribute may not exist.
2073///
2074/// # Usage
2075///
2076/// ```rust
2077/// use hyperlane::*;
2078/// use hyperlane_macros::*;
2079/// use serde::{Deserialize, Serialize};
2080///
2081/// const TEST_ATTRIBUTE_KEY: &str = "test_attribute_key";
2082///
2083/// #[derive(Clone, Debug, Deserialize, Serialize)]
2084/// struct TestData {
2085/// name: String,
2086/// age: u32,
2087/// }
2088///
2089/// #[route("/attribute_option")]
2090/// struct Attribute;
2091///
2092/// impl ServerHook for Attribute {
2093/// async fn new(_ctx: &mut Context) -> Self {
2094/// Self
2095/// }
2096///
2097/// #[response_body(&format!("request attribute: {request_attribute_option:?}"))]
2098/// #[attribute_option(TEST_ATTRIBUTE_KEY => request_attribute_option: TestData)]
2099/// async fn handle(self, ctx: &mut Context) {}
2100/// }
2101///
2102/// impl Attribute {
2103/// #[attribute_option(TEST_ATTRIBUTE_KEY => request_attribute_option: TestData)]
2104/// async fn attribute_with_ref_self(&self, ctx: &mut Context) {}
2105/// }
2106///
2107/// #[attribute_option(TEST_ATTRIBUTE_KEY => request_attribute_option: TestData)]
2108/// async fn standalone_attribute_handler(ctx: &mut Context) {}
2109/// ```
2110///
2111/// The macro accepts a key-to-variable mapping in the format `key => variable_name: Type`.
2112/// The variable will be available as an `Option<Type>` in the function scope.
2113///
2114/// # Multi-Parameter Usage
2115///
2116/// ```rust
2117/// use hyperlane::*;
2118/// use hyperlane_macros::*;
2119///
2120/// #[route("/attribute_option")]
2121/// struct MultiAttr;
2122///
2123/// impl ServerHook for MultiAttr {
2124/// async fn new(_ctx: &mut Context) -> Self {
2125/// Self
2126/// }
2127///
2128/// #[response_body(&format!("attrs: {attr1:?}, {attr2:?}"))]
2129/// #[attribute_option("key1" => attr1: String, "key2" => attr2: i32)]
2130/// async fn handle(self, ctx: &mut Context) {}
2131/// }
2132/// ```
2133///
2134/// The macro accepts multiple `key => variable_name: Type` tuples separated by commas.
2135#[proc_macro_attribute]
2136pub fn attribute_option(attr: TokenStream, item: TokenStream) -> TokenStream {
2137 attribute_option_macro(attr, item, Position::Prologue)
2138}
2139
2140/// Extracts a specific attribute value into a variable with panic on missing value.
2141///
2142/// This attribute macro retrieves a specific attribute by key and makes it available
2143/// as a typed variable from the request context. If the attribute does not exist,
2144/// the function will panic with an error message indicating the missing attribute.
2145///
2146/// # Usage
2147///
2148/// ```rust
2149/// use hyperlane::*;
2150/// use hyperlane_macros::*;
2151/// use serde::{Deserialize, Serialize};
2152///
2153/// const TEST_ATTRIBUTE_KEY: &str = "test_attribute_key";
2154///
2155/// #[derive(Clone, Debug, Deserialize, Serialize)]
2156/// struct TestData {
2157/// name: String,
2158/// age: u32,
2159/// }
2160///
2161/// #[route("/attribute")]
2162/// struct Attribute;
2163///
2164/// impl ServerHook for Attribute {
2165/// async fn new(_ctx: &mut Context) -> Self {
2166/// Self
2167/// }
2168///
2169/// #[response_body(&format!("request attribute: {request_attribute:?}"))]
2170/// #[attribute(TEST_ATTRIBUTE_KEY => request_attribute: TestData)]
2171/// async fn handle(self, ctx: &mut Context) {}
2172/// }
2173///
2174/// impl Attribute {
2175/// #[attribute(TEST_ATTRIBUTE_KEY => request_attribute: TestData)]
2176/// async fn attribute_with_ref_self(&self, ctx: &mut Context) {}
2177/// }
2178///
2179/// #[attribute(TEST_ATTRIBUTE_KEY => request_attribute: TestData)]
2180/// async fn standalone_attribute_handler(ctx: &mut Context) {}
2181/// ```
2182///
2183/// The macro accepts a key-to-variable mapping in the format `key => variable_name: Type`.
2184/// The variable will be available as an `Type` in the function scope.
2185///
2186/// # Multi-Parameter Usage
2187///
2188/// ```rust
2189/// use hyperlane::*;
2190/// use hyperlane_macros::*;
2191///
2192/// #[route("/attribute")]
2193/// struct MultiAttr;
2194///
2195/// impl ServerHook for MultiAttr {
2196/// async fn new(_ctx: &mut Context) -> Self {
2197/// Self
2198/// }
2199///
2200/// #[response_body(&format!("attrs: {attr1}, {attr2}"))]
2201/// #[attribute("key1" => attr1: String, "key2" => attr2: i32)]
2202/// async fn handle(self, ctx: &mut Context) {}
2203/// }
2204/// ```
2205///
2206/// The macro accepts multiple `key => variable_name: Type` tuples separated by commas.
2207///
2208/// # Panics
2209///
2210/// This macro will panic if the requested attribute does not exist in the request context.
2211#[proc_macro_attribute]
2212pub fn attribute(attr: TokenStream, item: TokenStream) -> TokenStream {
2213 attribute_macro(attr, item, Position::Prologue)
2214}
2215
2216/// Extracts all attributes into a ThreadSafeAttributeStore variable.
2217///
2218/// This attribute macro retrieves all available attributes from the request context
2219/// and makes them available as a ThreadSafeAttributeStore for comprehensive attribute access.
2220///
2221/// # Usage
2222///
2223/// ```rust
2224/// use hyperlane::*;
2225/// use hyperlane_macros::*;
2226///
2227/// #[route("/attributes")]
2228/// struct Attributes;
2229///
2230/// impl ServerHook for Attributes {
2231/// async fn new(_ctx: &mut Context) -> Self {
2232/// Self
2233/// }
2234///
2235/// #[response_body(&format!("request attributes: {request_attributes:?}"))]
2236/// #[attributes(request_attributes)]
2237/// async fn handle(self, ctx: &mut Context) {}
2238/// }
2239///
2240/// impl Attributes {
2241/// #[attributes(request_attributes)]
2242/// async fn attributes_with_ref_self(&self, ctx: &mut Context) {}
2243/// }
2244///
2245/// #[attributes(request_attributes)]
2246/// async fn standalone_attributes_handler(ctx: &mut Context) {}
2247/// ```
2248///
2249/// The macro accepts a variable name that will contain a HashMap of all attributes.
2250/// The variable will be available as a HashMap in the function scope.
2251///
2252/// # Multi-Parameter Usage
2253///
2254/// ```rust
2255/// use hyperlane::*;
2256/// use hyperlane_macros::*;
2257///
2258/// #[route("/multi_attrs")]
2259/// struct MultiAttrs;
2260///
2261/// impl ServerHook for MultiAttrs {
2262/// async fn new(_ctx: &mut Context) -> Self {
2263/// Self
2264/// }
2265///
2266/// #[response_body(&format!("attrs1: {attrs1:?}, attrs2: {attrs2:?}"))]
2267/// #[attributes(attrs1, attrs2)]
2268/// async fn handle(self, ctx: &mut Context) {}
2269/// }
2270/// ```
2271///
2272/// The macro accepts multiple variable names separated by commas.
2273#[proc_macro_attribute]
2274pub fn attributes(attr: TokenStream, item: TokenStream) -> TokenStream {
2275 attributes_macro(attr, item, Position::Prologue)
2276}
2277
2278/// Extracts panic data into a variable wrapped in Option type.
2279///
2280/// This attribute macro retrieves panic information if a panic occurred during handling
2281/// and makes it available as an Option variable. The extracted value is wrapped
2282/// in an Option type to safely handle cases where no panic occurred.
2283///
2284/// # Usage
2285///
2286/// ```rust
2287/// use hyperlane::*;
2288/// use hyperlane_macros::*;
2289///
2290/// #[route("/task_panic_data_option")]
2291/// struct PanicDataOptionTest;
2292///
2293/// impl ServerHook for PanicDataOptionTest {
2294/// async fn new(_ctx: &mut Context) -> Self {
2295/// Self
2296/// }
2297///
2298/// #[response_body(&format!("Panic data: {task_panic_data_option:?}"))]
2299/// #[task_panic_data_option(task_panic_data_option)]
2300/// async fn handle(self, ctx: &mut Context) {}
2301/// }
2302///
2303/// impl PanicDataOptionTest {
2304/// #[task_panic_data_option(task_panic_data_option)]
2305/// async fn task_panic_data_option_with_ref_self(&self, ctx: &mut Context) {}
2306/// }
2307///
2308/// #[task_panic_data_option(task_panic_data_option)]
2309/// async fn standalone_task_panic_data_option_handler(ctx: &mut Context) {}
2310/// ```
2311///
2312/// The macro accepts a variable name that will contain the panic data.
2313/// The variable will be available as an `Option<PanicData>` in the function scope.
2314///
2315/// # Multi-Parameter Usage
2316///
2317/// ```rust
2318/// use hyperlane::*;
2319/// use hyperlane_macros::*;
2320///
2321/// #[route("/task_panic_data_option")]
2322/// struct MultiPanicDataOption;
2323///
2324/// impl ServerHook for MultiPanicDataOption {
2325/// async fn new(_ctx: &mut Context) -> Self {
2326/// Self
2327/// }
2328///
2329/// #[response_body(&format!("panic1: {panic1:?}, panic2: {panic2:?}"))]
2330/// #[task_panic_data_option(panic1, panic2)]
2331/// async fn handle(self, ctx: &mut Context) {}
2332/// }
2333/// ```
2334///
2335/// The macro accepts multiple variable names separated by commas.
2336#[proc_macro_attribute]
2337pub fn task_panic_data_option(attr: TokenStream, item: TokenStream) -> TokenStream {
2338 task_panic_data_option_macro(attr, item, Position::Prologue)
2339}
2340
2341/// Extracts panic data into a variable with panic on missing value.
2342///
2343/// This attribute macro retrieves panic information if a panic occurred during handling
2344/// and makes it available as a variable. If no panic data exists,
2345/// the function will panic with an error message.
2346///
2347/// # Usage
2348///
2349/// ```rust
2350/// use hyperlane::*;
2351/// use hyperlane_macros::*;
2352///
2353/// #[route("/task_panic_data")]
2354/// struct PanicDataTest;
2355///
2356/// impl ServerHook for PanicDataTest {
2357/// async fn new(_ctx: &mut Context) -> Self {
2358/// Self
2359/// }
2360///
2361/// #[response_body(&format!("Panic data: {task_panic_data}"))]
2362/// #[task_panic_data(task_panic_data)]
2363/// async fn handle(self, ctx: &mut Context) {}
2364/// }
2365///
2366/// impl PanicDataTest {
2367/// #[task_panic_data(task_panic_data)]
2368/// async fn task_panic_data_with_ref_self(&self, ctx: &mut Context) {}
2369/// }
2370///
2371/// #[task_panic_data(task_panic_data)]
2372/// async fn standalone_task_panic_data_handler(ctx: &mut Context) {}
2373/// ```
2374///
2375/// The macro accepts a variable name that will contain the panic data.
2376/// The variable will be available as a `PanicData` in the function scope.
2377///
2378/// # Multi-Parameter Usage
2379///
2380/// ```rust
2381/// use hyperlane::*;
2382/// use hyperlane_macros::*;
2383///
2384/// #[route("/task_panic_data")]
2385/// struct MultiPanicData;
2386///
2387/// impl ServerHook for MultiPanicData {
2388/// async fn new(_ctx: &mut Context) -> Self {
2389/// Self
2390/// }
2391///
2392/// #[response_body(&format!("panic1: {panic1}, panic2: {panic2}"))]
2393/// #[task_panic_data(panic1, panic2)]
2394/// async fn handle(self, ctx: &mut Context) {}
2395/// }
2396/// ```
2397///
2398/// The macro accepts multiple variable names separated by commas.
2399///
2400/// # Panics
2401///
2402/// This macro will panic if no panic data exists in the request context.
2403#[proc_macro_attribute]
2404pub fn task_panic_data(attr: TokenStream, item: TokenStream) -> TokenStream {
2405 task_panic_data_macro(attr, item, Position::Prologue)
2406}
2407
2408/// Extracts request error data into a variable wrapped in Option type.
2409///
2410/// This attribute macro retrieves request error information if an error occurred during handling
2411/// and makes it available as an Option variable. The extracted value is wrapped
2412/// in an Option type to safely handle cases where no error occurred.
2413///
2414/// # Usage
2415///
2416/// ```rust
2417/// use hyperlane::*;
2418/// use hyperlane_macros::*;
2419///
2420/// #[route("/request_error_data_option")]
2421/// struct RequestErrorDataOptionTest;
2422///
2423/// impl ServerHook for RequestErrorDataOptionTest {
2424/// async fn new(_ctx: &mut Context) -> Self {
2425/// Self
2426/// }
2427///
2428/// #[response_body(&format!("Request error data: {request_error_data_option:?}"))]
2429/// #[request_error_data_option(request_error_data_option)]
2430/// async fn handle(self, ctx: &mut Context) {}
2431/// }
2432///
2433/// impl RequestErrorDataOptionTest {
2434/// #[request_error_data_option(request_error_data_option)]
2435/// async fn request_error_data_option_with_ref_self(&self, ctx: &mut Context) {}
2436/// }
2437///
2438/// #[request_error_data_option(request_error_data_option)]
2439/// async fn standalone_request_error_data_option_handler(ctx: &mut Context) {}
2440/// ```
2441///
2442/// The macro accepts a variable name that will contain the request error data.
2443/// The variable will be available as an `Option<RequestError>` in the function scope.
2444///
2445/// # Multi-Parameter Usage
2446///
2447/// ```rust
2448/// use hyperlane::*;
2449/// use hyperlane_macros::*;
2450///
2451/// #[route("/request_error_data_option")]
2452/// struct MultiRequestErrorDataOption;
2453///
2454/// impl ServerHook for MultiRequestErrorDataOption {
2455/// async fn new(_ctx: &mut Context) -> Self {
2456/// Self
2457/// }
2458///
2459/// #[response_body(&format!("error1: {error1:?}, error2: {error2:?}"))]
2460/// #[request_error_data_option(error1, error2)]
2461/// async fn handle(self, ctx: &mut Context) {}
2462/// }
2463/// ```
2464///
2465/// The macro accepts multiple variable names separated by commas.
2466#[proc_macro_attribute]
2467pub fn request_error_data_option(attr: TokenStream, item: TokenStream) -> TokenStream {
2468 request_error_data_option_macro(attr, item, Position::Prologue)
2469}
2470
2471/// Extracts request error data into a variable with panic on missing value.
2472///
2473/// This attribute macro retrieves request error information if an error occurred during handling
2474/// and makes it available as a variable. If no error data exists,
2475/// the function will panic with an error message.
2476///
2477/// # Usage
2478///
2479/// ```rust
2480/// use hyperlane::*;
2481/// use hyperlane_macros::*;
2482///
2483/// #[route("/request_error_data")]
2484/// struct RequestErrorDataTest;
2485///
2486/// impl ServerHook for RequestErrorDataTest {
2487/// async fn new(_ctx: &mut Context) -> Self {
2488/// Self
2489/// }
2490///
2491/// #[response_body(&format!("Request error data: {request_error_data}"))]
2492/// #[request_error_data(request_error_data)]
2493/// async fn handle(self, ctx: &mut Context) {}
2494/// }
2495///
2496/// impl RequestErrorDataTest {
2497/// #[request_error_data(request_error_data)]
2498/// async fn request_error_data_with_ref_self(&self, ctx: &mut Context) {}
2499/// }
2500///
2501/// #[request_error_data(request_error_data)]
2502/// async fn standalone_request_error_data_handler(ctx: &mut Context) {}
2503/// ```
2504///
2505/// The macro accepts a variable name that will contain the request error data.
2506/// The variable will be available as a `RequestError` in the function scope.
2507///
2508/// # Multi-Parameter Usage
2509///
2510/// ```rust
2511/// use hyperlane::*;
2512/// use hyperlane_macros::*;
2513///
2514/// #[route("/request_error_data")]
2515/// struct MultiRequestErrorData;
2516///
2517/// impl ServerHook for MultiRequestErrorData {
2518/// async fn new(_ctx: &mut Context) -> Self {
2519/// Self
2520/// }
2521///
2522/// #[response_body(&format!("error1: {error1}, error2: {error2}"))]
2523/// #[request_error_data(error1, error2)]
2524/// async fn handle(self, ctx: &mut Context) {}
2525/// }
2526/// ```
2527///
2528/// The macro accepts multiple variable names separated by commas.
2529///
2530/// # Panics
2531///
2532/// This macro will panic if no request error data exists in the request context.
2533#[proc_macro_attribute]
2534pub fn request_error_data(attr: TokenStream, item: TokenStream) -> TokenStream {
2535 request_error_data_macro(attr, item, Position::Prologue)
2536}
2537
2538/// Extracts a specific route parameter into a variable wrapped in Option type.
2539///
2540/// This attribute macro retrieves a specific route parameter by key and makes it
2541/// available as an Option variable. Route parameters are extracted from the URL path segments
2542/// and wrapped in an Option type to safely handle cases where the parameter may not exist.
2543///
2544/// # Usage
2545///
2546/// ```rust
2547/// use hyperlane::*;
2548/// use hyperlane_macros::*;
2549///
2550/// #[route("/route_param_option/:test")]
2551/// struct RouteParam;
2552///
2553/// impl ServerHook for RouteParam {
2554/// async fn new(_ctx: &mut Context) -> Self {
2555/// Self
2556/// }
2557///
2558/// #[response_body(&format!("route param: {request_route_param:?}"))]
2559/// #[route_param_option("test" => request_route_param)]
2560/// async fn handle(self, ctx: &mut Context) {}
2561/// }
2562///
2563/// impl RouteParam {
2564/// #[route_param_option("test" => request_route_param)]
2565/// async fn route_param_with_ref_self(&self, ctx: &mut Context) {}
2566/// }
2567///
2568/// #[route_param_option("test" => request_route_param)]
2569/// async fn standalone_route_param_handler(ctx: &mut Context) {}
2570/// ```
2571///
2572/// The macro accepts a key-to-variable mapping in the format `"key" => variable_name`.
2573/// The variable will be available as an `Option<String>` in the function scope.
2574///
2575/// # Multi-Parameter Usage
2576///
2577/// ```rust
2578/// use hyperlane::*;
2579/// use hyperlane_macros::*;
2580///
2581/// #[route("/multi_param/:id/:name")]
2582/// struct MultiParam;
2583///
2584/// impl ServerHook for MultiParam {
2585/// async fn new(_ctx: &mut Context) -> Self {
2586/// Self
2587/// }
2588///
2589/// #[response_body(&format!("id: {id:?}, name: {name:?}"))]
2590/// #[route_param_option("id" => id, "name" => name)]
2591/// async fn handle(self, ctx: &mut Context) {}
2592/// }
2593/// ```
2594///
2595/// The macro accepts multiple `"key" => variable_name` pairs separated by commas.
2596#[proc_macro_attribute]
2597pub fn route_param_option(attr: TokenStream, item: TokenStream) -> TokenStream {
2598 route_param_option_macro(attr, item, Position::Prologue)
2599}
2600
2601/// Extracts a specific route parameter into a variable with panic on missing value.
2602///
2603/// This attribute macro retrieves a specific route parameter by key and makes it
2604/// available as a variable. Route parameters are extracted from the URL path segments.
2605/// If the requested route parameter does not exist, the function will panic with an error message.
2606///
2607/// # Usage
2608///
2609/// ```rust
2610/// use hyperlane::*;
2611/// use hyperlane_macros::*;
2612///
2613/// #[route("/route_param/:test")]
2614/// struct RouteParam;
2615///
2616/// impl ServerHook for RouteParam {
2617/// async fn new(_ctx: &mut Context) -> Self {
2618/// Self
2619/// }
2620///
2621/// #[response_body(&format!("route param: {request_route_param:?}"))]
2622/// #[route_param("test" => request_route_param)]
2623/// async fn handle(self, ctx: &mut Context) {}
2624/// }
2625///
2626/// impl RouteParam {
2627/// #[route_param("test" => request_route_param)]
2628/// async fn route_param_with_ref_self(&self, ctx: &mut Context) {}
2629/// }
2630///
2631/// #[route_param("test" => request_route_param)]
2632/// async fn standalone_route_param_handler(ctx: &mut Context) {}
2633/// ```
2634///
2635/// The macro accepts a key-to-variable mapping in the format `"key" => variable_name`.
2636/// The variable will be available as an `String` in the function scope.
2637///
2638///
2639/// # Multi-Parameter Usage
2640///
2641/// ```rust
2642/// use hyperlane::*;
2643/// use hyperlane_macros::*;
2644///
2645/// #[route("/multi_param/:id/:name")]
2646/// struct MultiParam;
2647///
2648/// impl ServerHook for MultiParam {
2649/// async fn new(_ctx: &mut Context) -> Self {
2650/// Self
2651/// }
2652///
2653/// #[response_body(&format!("id: {id:?}, name: {name:?}"))]
2654/// #[route_param("id" => id, "name" => name)]
2655/// async fn handle(self, ctx: &mut Context) {}
2656/// }
2657/// ```
2658///
2659/// The macro accepts multiple `"key" => variable_name` pairs separated by commas.
2660///
2661/// # Panics
2662///
2663/// This macro will panic if the requested route parameter does not exist in the URL path.
2664#[proc_macro_attribute]
2665pub fn route_param(attr: TokenStream, item: TokenStream) -> TokenStream {
2666 route_param_macro(attr, item, Position::Prologue)
2667}
2668
2669/// Extracts all route parameters into a collection variable.
2670///
2671/// This attribute macro retrieves all available route parameters from the URL path
2672/// and makes them available as a collection for comprehensive route parameter access.
2673///
2674/// # Usage
2675///
2676/// ```rust
2677/// use hyperlane::*;
2678/// use hyperlane_macros::*;
2679///
2680/// #[route("/route_params/:test")]
2681/// struct RouteParams;
2682///
2683/// impl ServerHook for RouteParams {
2684/// async fn new(_ctx: &mut Context) -> Self {
2685/// Self
2686/// }
2687///
2688/// #[response_body(&format!("request route params: {request_route_params:?}"))]
2689/// #[route_params(request_route_params)]
2690/// async fn handle(self, ctx: &mut Context) {}
2691/// }
2692///
2693/// impl RouteParams {
2694/// #[route_params(request_route_params)]
2695/// async fn route_params_with_ref_self(&self, ctx: &mut Context) {}
2696/// }
2697///
2698/// #[route_params(request_route_params)]
2699/// async fn standalone_route_params_handler(ctx: &mut Context) {}
2700/// ```
2701///
2702/// The macro accepts a variable name that will contain all route parameters.
2703/// The variable will be available as a RouteParams type in the function scope.
2704///
2705/// # Multi-Parameter Usage
2706///
2707/// ```rust
2708/// use hyperlane::*;
2709/// use hyperlane_macros::*;
2710///
2711/// #[route("/multi_params/:id")]
2712/// struct MultiParams;
2713///
2714/// impl ServerHook for MultiParams {
2715/// async fn new(_ctx: &mut Context) -> Self {
2716/// Self
2717/// }
2718///
2719/// #[response_body(&format!("params1: {params1:?}, params2: {params2:?}"))]
2720/// #[route_params(params1, params2)]
2721/// async fn handle(self, ctx: &mut Context) {}
2722/// }
2723/// ```
2724///
2725/// The macro accepts multiple variable names separated by commas.
2726#[proc_macro_attribute]
2727pub fn route_params(attr: TokenStream, item: TokenStream) -> TokenStream {
2728 route_params_macro(attr, item, Position::Prologue)
2729}
2730
2731/// Extracts a specific request query parameter into a variable wrapped in Option type.
2732///
2733/// This attribute macro retrieves a specific request query parameter by key and makes it
2734/// available as an Option variable. Query parameters are extracted from the URL request query string
2735/// and wrapped in an Option type to safely handle cases where the parameter may not exist.
2736///
2737/// # Usage
2738///
2739/// ```rust
2740/// use hyperlane::*;
2741/// use hyperlane_macros::*;
2742///
2743/// #[route("/request_query_option")]
2744/// struct RequestQuery;
2745///
2746/// impl ServerHook for RequestQuery {
2747/// async fn new(_ctx: &mut Context) -> Self {
2748/// Self
2749/// }
2750///
2751/// #[prologue_macros(
2752/// request_query_option("test" => request_query_option),
2753/// response_body(&format!("request query: {request_query_option:?}")),
2754/// send
2755/// )]
2756/// async fn handle(self, ctx: &mut Context) {}
2757/// }
2758///
2759/// impl RequestQuery {
2760/// #[request_query_option("test" => request_query_option)]
2761/// async fn request_query_with_ref_self(&self, ctx: &mut Context) {}
2762/// }
2763///
2764/// #[request_query_option("test" => request_query_option)]
2765/// async fn standalone_request_query_handler(ctx: &mut Context) {}
2766/// ```
2767///
2768/// The macro accepts a key-to-variable mapping in the format `"key" => variable_name`.
2769/// The variable will be available as an `Option<RequestQuerysValue>` in the function scope.
2770///
2771/// Supports multiple parameters: `#[request_query_option("k1" => v1, "k2" => v2)]`
2772#[proc_macro_attribute]
2773pub fn request_query_option(attr: TokenStream, item: TokenStream) -> TokenStream {
2774 request_query_option_macro(attr, item, Position::Prologue)
2775}
2776
2777/// Extracts a specific request query parameter into a variable with panic on missing value.
2778///
2779/// This attribute macro retrieves a specific request query parameter by key and makes it
2780/// available as a variable. Query parameters are extracted from the URL request query string.
2781/// If the requested query parameter does not exist, the function will panic with an error message.
2782///
2783/// # Usage
2784///
2785/// ```rust
2786/// use hyperlane::*;
2787/// use hyperlane_macros::*;
2788///
2789/// #[route("/request_query")]
2790/// struct RequestQuery;
2791///
2792/// impl ServerHook for RequestQuery {
2793/// async fn new(_ctx: &mut Context) -> Self {
2794/// Self
2795/// }
2796///
2797/// #[prologue_macros(
2798/// request_query("test" => request_query),
2799/// response_body(&format!("request query: {request_query}")),
2800/// send
2801/// )]
2802/// async fn handle(self, ctx: &mut Context) {}
2803/// }
2804///
2805/// impl RequestQuery {
2806/// #[request_query("test" => request_query)]
2807/// async fn request_query_with_ref_self(&self, ctx: &mut Context) {}
2808/// }
2809///
2810/// #[request_query("test" => request_query)]
2811/// async fn standalone_request_query_handler(ctx: &mut Context) {}
2812/// ```
2813///
2814/// The macro accepts a key-to-variable mapping in the format `"key" => variable_name`.
2815/// The variable will be available as an `RequestQuerysValue` in the function scope.
2816///
2817/// Supports multiple parameters: `#[request_query("k1" => v1, "k2" => v2)]`
2818///
2819/// # Panics
2820///
2821/// This macro will panic if the requested query parameter does not exist in the URL query string.
2822#[proc_macro_attribute]
2823pub fn request_query(attr: TokenStream, item: TokenStream) -> TokenStream {
2824 request_query_macro(attr, item, Position::Prologue)
2825}
2826
2827/// Extracts all request query parameters into a RequestQuerys variable.
2828///
2829/// This attribute macro retrieves all available request query parameters from the URL request query string
2830/// and makes them available as a RequestQuerys for comprehensive request query parameter access.
2831///
2832/// # Usage
2833///
2834/// ```rust
2835/// use hyperlane::*;
2836/// use hyperlane_macros::*;
2837///
2838/// #[route("/request_querys")]
2839/// struct RequestQuerys;
2840///
2841/// impl ServerHook for RequestQuerys {
2842/// async fn new(_ctx: &mut Context) -> Self {
2843/// Self
2844/// }
2845///
2846/// #[prologue_macros(
2847/// request_querys(request_querys),
2848/// response_body(&format!("request querys: {request_querys:?}")),
2849/// send
2850/// )]
2851/// async fn handle(self, ctx: &mut Context) {}
2852/// }
2853///
2854/// impl RequestQuerys {
2855/// #[request_querys(request_querys)]
2856/// async fn request_querys_with_ref_self(&self, ctx: &mut Context) {}
2857/// }
2858///
2859/// #[request_querys(request_querys)]
2860/// async fn standalone_request_querys_handler(ctx: &mut Context) {}
2861/// ```
2862///
2863/// The macro accepts a variable name that will contain all request query parameters.
2864/// The variable will be available as a collection in the function scope.
2865///
2866/// Supports multiple parameters: `#[request_querys(querys1, querys2)]`
2867#[proc_macro_attribute]
2868pub fn request_querys(attr: TokenStream, item: TokenStream) -> TokenStream {
2869 request_querys_macro(attr, item, Position::Prologue)
2870}
2871
2872/// Extracts a specific HTTP request header into a variable wrapped in Option type.
2873///
2874/// This attribute macro retrieves a specific HTTP request header by name and makes it
2875/// available as an Option variable. Header values are extracted from the request request headers collection
2876/// and wrapped in an Option type to safely handle cases where the header may not exist.
2877///
2878/// # Usage
2879///
2880/// ```rust
2881/// use hyperlane::*;
2882/// use hyperlane_macros::*;
2883///
2884/// #[route("/request_header_option")]
2885/// struct RequestHeader;
2886///
2887/// impl ServerHook for RequestHeader {
2888/// async fn new(_ctx: &mut Context) -> Self {
2889/// Self
2890/// }
2891///
2892/// #[prologue_macros(
2893/// request_header_option(HOST => request_header_option),
2894/// response_body(&format!("request header: {request_header_option:?}")),
2895/// send
2896/// )]
2897/// async fn handle(self, ctx: &mut Context) {}
2898/// }
2899///
2900/// impl RequestHeader {
2901/// #[request_header_option(HOST => request_header_option)]
2902/// async fn request_header_with_ref_self(&self, ctx: &mut Context) {}
2903/// }
2904///
2905/// #[request_header_option(HOST => request_header_option)]
2906/// async fn standalone_request_header_handler(ctx: &mut Context) {}
2907/// ```
2908///
2909/// The macro accepts a request header name-to-variable mapping in the format `HEADER_NAME => variable_name`
2910/// or `"Header-Name" => variable_name`. The variable will be available as an `Option<RequestHeadersValueItem>`.
2911#[proc_macro_attribute]
2912pub fn request_header_option(attr: TokenStream, item: TokenStream) -> TokenStream {
2913 request_header_option_macro(attr, item, Position::Prologue)
2914}
2915
2916/// Extracts a specific HTTP request header into a variable with panic on missing value.
2917///
2918/// This attribute macro retrieves a specific HTTP request header by name and makes it
2919/// available as a variable. Header values are extracted from the request request headers collection.
2920/// If the requested header does not exist, the function will panic with an error message.
2921///
2922/// # Usage
2923///
2924/// ```rust
2925/// use hyperlane::*;
2926/// use hyperlane_macros::*;
2927///
2928/// #[route("/request_header")]
2929/// struct RequestHeader;
2930///
2931/// impl ServerHook for RequestHeader {
2932/// async fn new(_ctx: &mut Context) -> Self {
2933/// Self
2934/// }
2935///
2936/// #[prologue_macros(
2937/// request_header(HOST => request_header),
2938/// response_body(&format!("request header: {request_header}")),
2939/// send
2940/// )]
2941/// async fn handle(self, ctx: &mut Context) {}
2942/// }
2943///
2944/// impl RequestHeader {
2945/// #[request_header(HOST => request_header)]
2946/// async fn request_header_with_ref_self(&self, ctx: &mut Context) {}
2947/// }
2948///
2949/// #[request_header(HOST => request_header)]
2950/// async fn standalone_request_header_handler(ctx: &mut Context) {}
2951/// ```
2952///
2953/// The macro accepts a request header name-to-variable mapping in the format `HEADER_NAME => variable_name`
2954/// or `"Header-Name" => variable_name`. The variable will be available as an `RequestHeadersValueItem`.
2955///
2956/// # Panics
2957///
2958/// This macro will panic if the requested header does not exist in the HTTP request headers.
2959#[proc_macro_attribute]
2960pub fn request_header(attr: TokenStream, item: TokenStream) -> TokenStream {
2961 request_header_macro(attr, item, Position::Prologue)
2962}
2963
2964/// Extracts all HTTP request headers into a collection variable.
2965///
2966/// This attribute macro retrieves all available HTTP request headers from the request
2967/// and makes them available as a collection for comprehensive request header access.
2968///
2969/// # Usage
2970///
2971/// ```rust
2972/// use hyperlane::*;
2973/// use hyperlane_macros::*;
2974///
2975/// #[route("/request_headers")]
2976/// struct RequestHeaders;
2977///
2978/// impl ServerHook for RequestHeaders {
2979/// async fn new(_ctx: &mut Context) -> Self {
2980/// Self
2981/// }
2982///
2983/// #[prologue_macros(
2984/// request_headers(request_headers),
2985/// response_body(&format!("request headers: {request_headers:?}")),
2986/// send
2987/// )]
2988/// async fn handle(self, ctx: &mut Context) {}
2989/// }
2990///
2991/// impl RequestHeaders {
2992/// #[request_headers(request_headers)]
2993/// async fn request_headers_with_ref_self(&self, ctx: &mut Context) {}
2994/// }
2995///
2996/// #[request_headers(request_headers)]
2997/// async fn standalone_request_headers_handler(ctx: &mut Context) {}
2998/// ```
2999///
3000/// The macro accepts a variable name that will contain all HTTP request headers.
3001/// The variable will be available as a RequestHeaders type in the function scope.
3002#[proc_macro_attribute]
3003pub fn request_headers(attr: TokenStream, item: TokenStream) -> TokenStream {
3004 request_headers_macro(attr, item, Position::Prologue)
3005}
3006
3007/// Extracts a specific cookie value or all cookies into a variable wrapped in Option type.
3008///
3009/// This attribute macro supports two syntaxes:
3010/// 1. `cookie(key => variable_name)` - Extract a specific cookie value by key, wrapped in Option
3011/// 2. `cookie(variable_name)` - Extract all cookies as a raw string, wrapped in Option
3012///
3013/// # Usage
3014///
3015/// ```rust
3016/// use hyperlane::*;
3017/// use hyperlane_macros::*;
3018///
3019/// #[route("/cookie")]
3020/// struct Cookie;
3021///
3022/// impl ServerHook for Cookie {
3023/// async fn new(_ctx: &mut Context) -> Self {
3024/// Self
3025/// }
3026///
3027/// #[response_body(&format!("Session cookie: {session_cookie1_option:?}, {session_cookie2_option:?}"))]
3028/// #[request_cookie_option("test1" => session_cookie1_option, "test2" => session_cookie2_option)]
3029/// async fn handle(self, ctx: &mut Context) {}
3030/// }
3031///
3032/// impl Cookie {
3033/// #[response_body(&format!("Session cookie: {session_cookie1_option:?}, {session_cookie2_option:?}"))]
3034/// #[request_cookie_option("test1" => session_cookie1_option, "test2" => session_cookie2_option)]
3035/// async fn request_cookie_with_ref_self(&self, ctx: &mut Context) {}
3036/// }
3037///
3038/// #[response_body(&format!("Session cookie: {session_cookie1_option:?}, {session_cookie2_option:?}"))]
3039/// #[request_cookie_option("test1" => session_cookie1_option, "test2" => session_cookie2_option)]
3040/// async fn standalone_request_cookie_handler(ctx: &mut Context) {}
3041/// ```
3042///
3043/// For specific cookie extraction, the variable will be available as `Option<String>`.
3044/// For all cookies extraction, the variable will be available as `String`.
3045#[proc_macro_attribute]
3046pub fn request_cookie_option(attr: TokenStream, item: TokenStream) -> TokenStream {
3047 request_cookie_option_macro(attr, item, Position::Prologue)
3048}
3049
3050/// Extracts a specific cookie value or all cookies into a variable with panic on missing value.
3051///
3052/// This attribute macro supports two syntaxes:
3053/// 1. `cookie(key => variable_name)` - Extract a specific cookie value by key, panics if missing
3054/// 2. `cookie(variable_name)` - Extract all cookies as a raw string, panics if missing
3055///
3056/// # Usage
3057///
3058/// ```rust
3059/// use hyperlane::*;
3060/// use hyperlane_macros::*;
3061///
3062/// #[route("/cookie")]
3063/// struct Cookie;
3064///
3065/// impl ServerHook for Cookie {
3066/// async fn new(_ctx: &mut Context) -> Self {
3067/// Self
3068/// }
3069///
3070/// #[response_body(&format!("Session cookie: {session_cookie1}, {session_cookie2}"))]
3071/// #[request_cookie("test1" => session_cookie1, "test2" => session_cookie2)]
3072/// async fn handle(self, ctx: &mut Context) {}
3073/// }
3074///
3075/// impl Cookie {
3076/// #[response_body(&format!("Session cookie: {session_cookie1}, {session_cookie2}"))]
3077/// #[request_cookie("test1" => session_cookie1, "test2" => session_cookie2)]
3078/// async fn request_cookie_with_ref_self(&self, ctx: &mut Context) {}
3079/// }
3080///
3081/// #[response_body(&format!("Session cookie: {session_cookie1}, {session_cookie2}"))]
3082/// #[request_cookie("test1" => session_cookie1, "test2" => session_cookie2)]
3083/// async fn standalone_request_cookie_handler(ctx: &mut Context) {}
3084/// ```
3085///
3086/// For specific cookie extraction, the variable will be available as `String`.
3087/// For all cookies extraction, the variable will be available as `String`.
3088///
3089/// # Panics
3090///
3091/// This macro will panic if the requested cookie does not exist in the HTTP request headers.
3092#[proc_macro_attribute]
3093pub fn request_cookie(attr: TokenStream, item: TokenStream) -> TokenStream {
3094 request_cookie_macro(attr, item, Position::Prologue)
3095}
3096
3097/// Extracts all cookies as a raw string into a variable.
3098///
3099/// This attribute macro retrieves the entire Cookie header from the request and makes it
3100/// available as a String variable. If no Cookie header is present, an empty string is used.
3101///
3102/// # Usage
3103///
3104/// ```rust
3105/// use hyperlane::*;
3106/// use hyperlane_macros::*;
3107///
3108/// #[route("/cookies")]
3109/// struct Cookies;
3110///
3111/// impl ServerHook for Cookies {
3112/// async fn new(_ctx: &mut Context) -> Self {
3113/// Self
3114/// }
3115///
3116/// #[response_body(&format!("All cookies: {cookie_value:?}"))]
3117/// #[request_cookies(cookie_value)]
3118/// async fn handle(self, ctx: &mut Context) {}
3119/// }
3120///
3121/// impl Cookies {
3122/// #[request_cookies(cookie_value)]
3123/// async fn request_cookies_with_ref_self(&self, ctx: &mut Context) {}
3124/// }
3125///
3126/// #[request_cookies(cookie_value)]
3127/// async fn standalone_request_cookies_handler(ctx: &mut Context) {}
3128/// ```
3129///
3130/// The macro accepts a variable name that will contain all cookies.
3131/// The variable will be available as a Cookies type in the function scope.
3132///
3133/// # Multi-Parameter Usage
3134///
3135/// ```rust
3136/// use hyperlane::*;
3137/// use hyperlane_macros::*;
3138///
3139/// #[route("/multi_cookies")]
3140/// struct MultiCookies;
3141///
3142/// impl ServerHook for MultiCookies {
3143/// async fn new(_ctx: &mut Context) -> Self {
3144/// Self
3145/// }
3146///
3147/// #[response_body(&format!("cookies1: {cookies1:?}, cookies2: {cookies2:?}"))]
3148/// #[request_cookies(cookies1, cookies2)]
3149/// async fn handle(self, ctx: &mut Context) {}
3150/// }
3151/// ```
3152#[proc_macro_attribute]
3153pub fn request_cookies(attr: TokenStream, item: TokenStream) -> TokenStream {
3154 request_cookies_macro(attr, item, Position::Prologue)
3155}
3156
3157/// Extracts the HTTP request version into a variable.
3158///
3159/// This attribute macro retrieves the HTTP version from the request and makes it
3160/// available as a variable. The version represents the HTTP protocol version used.
3161///
3162/// # Usage
3163///
3164/// ```rust
3165/// use hyperlane::*;
3166/// use hyperlane_macros::*;
3167///
3168/// #[route("/request_version")]
3169/// struct RequestVersionTest;
3170///
3171/// impl ServerHook for RequestVersionTest {
3172/// async fn new(_ctx: &mut Context) -> Self {
3173/// Self
3174/// }
3175///
3176/// #[response_body(&format!("HTTP Version: {http_version}"))]
3177/// #[request_version(http_version)]
3178/// async fn handle(self, ctx: &mut Context) {}
3179/// }
3180///
3181/// impl RequestVersionTest {
3182/// #[request_version(http_version)]
3183/// async fn request_version_with_ref_self(&self, ctx: &mut Context) {}
3184/// }
3185///
3186/// #[request_version(http_version)]
3187/// async fn standalone_request_version_handler(ctx: &mut Context) {}
3188/// ```
3189///
3190/// The macro accepts a variable name that will contain the HTTP request version.
3191/// The variable will be available as a RequestVersion type in the function scope.
3192#[proc_macro_attribute]
3193pub fn request_version(attr: TokenStream, item: TokenStream) -> TokenStream {
3194 request_version_macro(attr, item, Position::Prologue)
3195}
3196
3197/// Extracts the HTTP request path into a variable.
3198///
3199/// This attribute macro retrieves the request path from the HTTP request and makes it
3200/// available as a variable. The path represents the URL path portion of the request.
3201///
3202/// # Usage
3203///
3204/// ```rust
3205/// use hyperlane::*;
3206/// use hyperlane_macros::*;
3207///
3208/// #[route("/request_path")]
3209/// struct RequestPathTest;
3210///
3211/// impl ServerHook for RequestPathTest {
3212/// async fn new(_ctx: &mut Context) -> Self {
3213/// Self
3214/// }
3215///
3216/// #[response_body(&format!("Request Path: {request_path}"))]
3217/// #[request_path(request_path)]
3218/// async fn handle(self, ctx: &mut Context) {}
3219/// }
3220///
3221/// impl RequestPathTest {
3222/// #[request_path(request_path)]
3223/// async fn request_path_with_ref_self(&self, ctx: &mut Context) {}
3224/// }
3225///
3226/// #[request_path(request_path)]
3227/// async fn standalone_request_path_handler(ctx: &mut Context) {}
3228/// ```
3229///
3230/// The macro accepts a variable name that will contain the HTTP request path.
3231/// The variable will be available as a RequestPath type in the function scope.
3232#[proc_macro_attribute]
3233pub fn request_path(attr: TokenStream, item: TokenStream) -> TokenStream {
3234 request_path_macro(attr, item, Position::Prologue)
3235}
3236
3237/// Creates a new instance of a specified type with a given variable name.
3238///
3239/// This attribute macro generates an instance initialization at the beginning of the function.
3240///
3241/// # Usage
3242///
3243/// ```rust,no_run
3244/// use hyperlane::*;
3245/// use hyperlane_macros::*;
3246///
3247/// #[hyperlane(server: Server)]
3248/// #[hyperlane(server_config: ServerConfig)]
3249/// #[tokio::main]
3250/// async fn main() {
3251/// server_config.set_nodelay(Some(false));
3252/// server.server_config(server_config);
3253/// let server_hook: ServerControlHook = server.run().await.unwrap_or_default();
3254/// server_hook.wait().await;
3255/// }
3256/// ```
3257///
3258/// Using in impl block method:
3259///
3260/// ```rust
3261/// use hyperlane::*;
3262/// use hyperlane_macros::*;
3263///
3264/// struct ServerInitializer;
3265///
3266/// impl ServerInitializer {
3267/// #[hyperlane(server: Server)]
3268/// #[hyperlane(server_config: ServerConfig)]
3269/// async fn initialize_server_1() -> Server {
3270/// server
3271/// }
3272///
3273/// #[hyperlane(server: Server)]
3274/// #[hyperlane(server_config: ServerConfig)]
3275/// async fn initialize_server_2(self) -> Server {
3276/// server
3277/// }
3278///
3279/// #[hyperlane(server: Server)]
3280/// #[hyperlane(server_config: ServerConfig)]
3281/// async fn initialize_server_3(&self) -> Server {
3282/// server
3283/// }
3284/// }
3285/// ```
3286///
3287/// The macro accepts a `variable_name: Type` pair.
3288/// The variable will be available as an instance of the specified type in the function scope.
3289#[proc_macro_attribute]
3290pub fn hyperlane(attr: TokenStream, item: TokenStream) -> TokenStream {
3291 hyperlane_macro(attr, item)
3292}
3293
3294/// Registers a function as a route handler.
3295///
3296/// This attribute macro registers the decorated function as a route handler for a given path.
3297/// This macro requires the `#[hyperlane(server: Server)]` macro to be used to define the server instance.
3298///
3299/// # Usage
3300///
3301/// ```rust
3302/// use hyperlane::*;
3303/// use hyperlane_macros::*;
3304///
3305/// #[route("/response")]
3306/// struct Response;
3307///
3308/// impl ServerHook for Response {
3309/// async fn new(_ctx: &mut Context) -> Self {
3310/// Self
3311/// }
3312///
3313/// #[response_body("response")]
3314/// async fn handle(self, ctx: &mut Context) {}
3315/// }
3316/// ```
3317///
3318/// # Parameters
3319///
3320/// - `path`: String literal defining the route path
3321///
3322/// # Dependencies
3323///
3324/// This macro depends on the `#[hyperlane(server: Server)]` macro to define the server instance.
3325#[proc_macro_attribute]
3326pub fn route(attr: TokenStream, item: TokenStream) -> TokenStream {
3327 route_macro(attr, item)
3328}
3329
3330/// Registers a function as a request middleware.
3331///
3332/// This attribute macro registers the decorated function to be executed as a middleware
3333/// for incoming requests. This macro requires the `#[hyperlane(server: Server)]` macro to be used to define the server instance.
3334///
3335/// # Note
3336///
3337/// If an order parameter is not specified, the hook will have a higher priority than hooks with a specified order.
3338///
3339/// # Usage
3340///
3341/// ```rust
3342/// use hyperlane::*;
3343/// use hyperlane_macros::*;
3344///
3345/// #[request_middleware]
3346/// struct RequestMiddleware;
3347///
3348/// impl ServerHook for RequestMiddleware {
3349/// async fn new(_ctx: &mut Context) -> Self {
3350/// Self
3351/// }
3352///
3353/// #[epilogue_macros(
3354/// response_status_code(200),
3355/// response_version(HttpVersion::Http1_1),
3356/// response_header(SERVER => HYPERLANE)
3357/// )]
3358/// async fn handle(self, ctx: &mut Context) {}
3359/// }
3360/// ```
3361///
3362/// # Dependencies
3363///
3364/// This macro depends on the `#[hyperlane(server: Server)]` macro to define the server instance.
3365#[proc_macro_attribute]
3366pub fn request_middleware(attr: TokenStream, item: TokenStream) -> TokenStream {
3367 request_middleware_macro(attr, item)
3368}
3369
3370/// Registers a function as a response middleware.
3371///
3372/// This attribute macro registers the decorated function to be executed as a middleware
3373/// for outgoing responses. This macro requires the `#[hyperlane(server: Server)]` macro to be used to define the server instance.
3374///
3375/// # Note
3376///
3377/// If an order parameter is not specified, the hook will have a higher priority than hooks with a specified order.
3378///
3379/// # Usage
3380///
3381/// ```rust
3382/// use hyperlane::*;
3383/// use hyperlane_macros::*;
3384///
3385/// #[response_middleware]
3386/// struct ResponseMiddleware1;
3387///
3388/// impl ServerHook for ResponseMiddleware1 {
3389/// async fn new(_ctx: &mut Context) -> Self {
3390/// Self
3391/// }
3392///
3393/// async fn handle(self, ctx: &mut Context) {}
3394/// }
3395/// ```
3396///
3397/// # Dependencies
3398///
3399/// This macro depends on the `#[hyperlane(server: Server)]` macro to define the server instance.
3400#[proc_macro_attribute]
3401pub fn response_middleware(attr: TokenStream, item: TokenStream) -> TokenStream {
3402 response_middleware_macro(attr, item)
3403}
3404
3405/// Registers a function as a panic hook.
3406///
3407/// This attribute macro registers the decorated function to handle panics that occur
3408/// during request processing. This macro requires the `#[hyperlane(server: Server)]` macro to be used to define the server instance.
3409///
3410/// # Note
3411///
3412/// If an order parameter is not specified, the hook will have a higher priority than hooks with a specified order.
3413///
3414/// # Usage
3415///
3416/// ```rust
3417/// use hyperlane::*;
3418/// use hyperlane_macros::*;
3419///
3420/// #[task_panic]
3421/// #[task_panic(1)]
3422/// #[task_panic("2")]
3423/// struct PanicHook;
3424///
3425/// impl ServerHook for PanicHook {
3426/// async fn new(_ctx: &mut Context) -> Self {
3427/// Self
3428/// }
3429///
3430/// #[epilogue_macros(response_body("task_panic"), send)]
3431/// async fn handle(self, ctx: &mut Context) {}
3432/// }
3433/// ```
3434///
3435/// # Dependencies
3436///
3437/// This macro depends on the `#[hyperlane(server: Server)]` macro to define the server instance.
3438#[proc_macro_attribute]
3439pub fn task_panic(attr: TokenStream, item: TokenStream) -> TokenStream {
3440 task_panic_macro(attr, item)
3441}
3442
3443/// Registers a function as a request error hook.
3444///
3445/// This attribute macro registers the decorated function to handle request errors that occur
3446/// during request processing. This macro requires the `#[hyperlane(server: Server)]` macro to be used to define the server instance.
3447///
3448/// # Note
3449///
3450/// If an order parameter is not specified, the hook will have a higher priority than hooks with a specified order.
3451///
3452/// # Usage
3453///
3454/// ```rust
3455/// use hyperlane::*;
3456/// use hyperlane_macros::*;
3457///
3458/// #[request_error]
3459/// #[request_error(1)]
3460/// #[request_error("2")]
3461/// struct RequestErrorHook;
3462///
3463/// impl ServerHook for RequestErrorHook {
3464/// async fn new(_ctx: &mut Context) -> Self {
3465/// Self
3466/// }
3467///
3468/// #[epilogue_macros(response_body("request_error"), send)]
3469/// async fn handle(self, ctx: &mut Context) {}
3470/// }
3471/// ```
3472///
3473/// # Dependencies
3474///
3475/// This macro depends on the `#[hyperlane(server: Server)]` macro to define the server instance.
3476#[proc_macro_attribute]
3477pub fn request_error(attr: TokenStream, item: TokenStream) -> TokenStream {
3478 request_error_macro(attr, item)
3479}
3480
3481/// Injects a list of macros before the decorated function.
3482///
3483/// The macros are applied in head-insertion order, meaning the first macro in the list
3484/// is the outermost macro.
3485///
3486/// # Usage
3487///
3488/// ```rust
3489/// use hyperlane::*;
3490/// use hyperlane_macros::*;
3491///
3492/// #[route("/prologue_macros")]
3493/// struct PrologueMacros;
3494///
3495/// impl ServerHook for PrologueMacros {
3496/// async fn new(_ctx: &mut Context) -> Self {
3497/// Self
3498/// }
3499///
3500/// #[prologue_macros(post_method, response_body("prologue_macros"), send)]
3501/// async fn handle(self, ctx: &mut Context) {}
3502/// }
3503/// ```
3504#[proc_macro_attribute]
3505pub fn prologue_macros(attr: TokenStream, item: TokenStream) -> TokenStream {
3506 prologue_macros_macro(attr, item)
3507}
3508
3509/// Injects a list of macros after the decorated function.
3510///
3511/// The macros are applied in tail-insertion order, meaning the last macro in the list
3512/// is the outermost macro.
3513///
3514/// # Usage
3515///
3516/// ```rust
3517/// use hyperlane::*;
3518/// use hyperlane_macros::*;
3519///
3520/// #[response_middleware(2)]
3521/// struct ResponseMiddleware2;
3522///
3523/// impl ServerHook for ResponseMiddleware2 {
3524/// async fn new(_ctx: &mut Context) -> Self {
3525/// Self
3526/// }
3527///
3528/// #[epilogue_macros(try_send, flush)]
3529/// async fn handle(self, ctx: &mut Context) {}
3530/// }
3531/// ```
3532#[proc_macro_attribute]
3533pub fn epilogue_macros(attr: TokenStream, item: TokenStream) -> TokenStream {
3534 epilogue_macros_macro(attr, item)
3535}
3536
3537/// Automatically tries to send the complete response after function execution.
3538///
3539/// This attribute macro ensures that the response (request headers and body) is automatically tried to be sent
3540/// to the client after the function completes execution.
3541///
3542/// # Usage
3543///
3544/// ```rust
3545/// use hyperlane::*;
3546/// use hyperlane_macros::*;
3547///
3548/// #[route("/try_send")]
3549/// struct TrySendTest;
3550///
3551/// impl ServerHook for TrySendTest {
3552/// async fn new(_ctx: &mut Context) -> Self {
3553/// Self
3554/// }
3555///
3556/// #[epilogue_macros(try_send)]
3557/// async fn handle(self, ctx: &mut Context) {}
3558/// }
3559///
3560/// impl TrySendTest {
3561/// #[try_send]
3562/// async fn try_send_with_ref_self(&self, ctx: &mut Context) {}
3563/// }
3564///
3565/// #[try_send]
3566/// async fn standalone_try_send_handler(ctx: &mut Context) {}
3567/// ```
3568///
3569/// The macro takes no parameters and should be applied directly to async functions
3570/// that accept a `&mut Context` parameter.
3571#[proc_macro_attribute]
3572pub fn try_send(_attr: TokenStream, item: TokenStream) -> TokenStream {
3573 try_send_macro(item, Position::Epilogue)
3574}
3575
3576/// Automatically sends the complete response after function execution.
3577///
3578/// This attribute macro ensures that the response (request headers and body) is automatically sent
3579/// to the client after the function completes execution.
3580///
3581/// # Usage
3582///
3583/// ```rust
3584/// use hyperlane::*;
3585/// use hyperlane_macros::*;
3586///
3587/// #[route("/send")]
3588/// struct SendTest;
3589///
3590/// impl ServerHook for SendTest {
3591/// async fn new(_ctx: &mut Context) -> Self {
3592/// Self
3593/// }
3594///
3595/// #[epilogue_macros(send)]
3596/// async fn handle(self, ctx: &mut Context) {}
3597/// }
3598///
3599/// impl SendTest {
3600/// #[send]
3601/// async fn send_with_ref_self(&self, ctx: &mut Context) {}
3602/// }
3603///
3604/// #[send]
3605/// async fn standalone_send_handler(ctx: &mut Context) {}
3606/// ```
3607///
3608/// The macro takes no parameters and should be applied directly to async functions
3609/// that accept a `&mut Context` parameter.
3610///
3611/// # Panics
3612///
3613/// This macro will panic if the send operation fails.
3614#[proc_macro_attribute]
3615pub fn send(_attr: TokenStream, item: TokenStream) -> TokenStream {
3616 send_macro(item, Position::Epilogue)
3617}
3618
3619/// Automatically tries to send only the response body after function execution.
3620///
3621/// This attribute macro ensures that only the response body is automatically tried to be sent
3622/// to the client after the function completes, handling request headers separately.
3623///
3624/// # Usage
3625///
3626/// ```rust
3627/// use hyperlane::*;
3628/// use hyperlane_macros::*;
3629///
3630/// #[route("/try_send_body")]
3631/// struct TrySendBodyTest;
3632///
3633/// impl ServerHook for TrySendBodyTest {
3634/// async fn new(_ctx: &mut Context) -> Self {
3635/// Self
3636/// }
3637///
3638/// #[epilogue_macros(try_send_body)]
3639/// async fn handle(self, ctx: &mut Context) {}
3640/// }
3641///
3642/// impl TrySendBodyTest {
3643/// #[try_send_body]
3644/// async fn try_send_body_with_ref_self(&self, ctx: &mut Context) {}
3645/// }
3646///
3647/// #[try_send_body]
3648/// async fn standalone_try_send_body_handler(ctx: &mut Context) {}
3649/// ```
3650///
3651/// The macro takes no parameters and should be applied directly to async functions
3652/// that accept a `&mut Context` parameter.
3653#[proc_macro_attribute]
3654pub fn try_send_body(_attr: TokenStream, item: TokenStream) -> TokenStream {
3655 try_send_body_macro(item, Position::Epilogue)
3656}
3657
3658/// Automatically sends only the response body after function execution.
3659///
3660/// This attribute macro ensures that only the response body is automatically sent
3661/// to the client after the function completes, handling request headers separately.
3662///
3663/// # Usage
3664///
3665/// ```rust
3666/// use hyperlane::*;
3667/// use hyperlane_macros::*;
3668///
3669/// #[route("/send_body")]
3670/// struct SendBodyTest;
3671///
3672/// impl ServerHook for SendBodyTest {
3673/// async fn new(_ctx: &mut Context) -> Self {
3674/// Self
3675/// }
3676///
3677/// #[epilogue_macros(send_body)]
3678/// async fn handle(self, ctx: &mut Context) {}
3679/// }
3680///
3681/// impl SendBodyTest {
3682/// #[send_body]
3683/// async fn send_body_with_ref_self(&self, ctx: &mut Context) {}
3684/// }
3685///
3686/// #[send_body]
3687/// async fn standalone_send_body_handler(ctx: &mut Context) {}
3688/// ```
3689///
3690/// The macro takes no parameters and should be applied directly to async functions
3691/// that accept a `&mut Context` parameter.
3692///
3693/// # Panics
3694///
3695/// This macro will panic if the send body operation fails.
3696#[proc_macro_attribute]
3697pub fn send_body(_attr: TokenStream, item: TokenStream) -> TokenStream {
3698 send_body_macro(item, Position::Epilogue)
3699}
3700
3701/// Tries to send only the response body with data after function execution.
3702///
3703/// This attribute macro ensures that only the response body is automatically tried to be sent
3704/// to the client after the function completes, handling request headers separately,
3705/// with the specified data.
3706///
3707/// # Usage
3708///
3709/// ```rust
3710/// use hyperlane::*;
3711/// use hyperlane_macros::*;
3712///
3713/// #[route("/try_send_body_with_data")]
3714/// struct TrySendBodyWithData;
3715///
3716/// impl ServerHook for TrySendBodyWithData {
3717/// async fn new(_ctx: &mut Context) -> Self {
3718/// Self
3719/// }
3720///
3721/// #[epilogue_macros(try_send_body_with_data("Response body content"))]
3722/// async fn handle(self, ctx: &mut Context) {}
3723/// }
3724/// ```
3725///
3726/// The macro accepts data to send and should be applied to async functions
3727/// that accept a `&mut Context` parameter.
3728#[proc_macro_attribute]
3729pub fn try_send_body_with_data(attr: TokenStream, item: TokenStream) -> TokenStream {
3730 try_send_body_with_data_macro(attr, item, Position::Epilogue)
3731}
3732
3733/// Sends only the response body with data after function execution.
3734///
3735/// This attribute macro ensures that only the response body is automatically sent
3736/// to the client after the function completes, handling request headers separately,
3737/// with the specified data.
3738///
3739/// # Usage
3740///
3741/// ```rust
3742/// use hyperlane::*;
3743/// use hyperlane_macros::*;
3744///
3745/// #[route("/send_body_with_data")]
3746/// struct SendBodyWithData;
3747///
3748/// impl ServerHook for SendBodyWithData {
3749/// async fn new(_ctx: &mut Context) -> Self {
3750/// Self
3751/// }
3752///
3753/// #[epilogue_macros(send_body_with_data("Response body content"))]
3754/// async fn handle(self, ctx: &mut Context) {}
3755/// }
3756/// ```
3757///
3758/// The macro accepts data to send and should be applied to async functions
3759/// that accept a `&mut Context` parameter.
3760///
3761/// # Panics
3762///
3763/// This macro will panic if the send body with data operation fails.
3764#[proc_macro_attribute]
3765pub fn send_body_with_data(attr: TokenStream, item: TokenStream) -> TokenStream {
3766 send_body_with_data_macro(attr, item, Position::Epilogue)
3767}
3768
3769/// Tries to flush the response stream after function execution.
3770///
3771/// This attribute macro ensures that the response stream is tried to be flushed to guarantee immediate
3772/// data transmission, forcing any buffered response data to be sent to the client. This will not panic on failure.
3773///
3774/// # Usage
3775///
3776/// ```rust
3777/// use hyperlane::*;
3778/// use hyperlane_macros::*;
3779///
3780/// #[route("/try_flush")]
3781/// struct TryFlushTest;
3782///
3783/// impl ServerHook for TryFlushTest {
3784/// async fn new(_ctx: &mut Context) -> Self {
3785/// Self
3786/// }
3787///
3788/// #[epilogue_macros(try_flush)]
3789/// async fn handle(self, ctx: &mut Context) {}
3790/// }
3791///
3792/// impl TryFlushTest {
3793/// #[try_flush]
3794/// async fn try_flush_with_ref_self(&self, ctx: &mut Context) {}
3795/// }
3796///
3797/// #[try_flush]
3798/// async fn standalone_try_flush_handler(ctx: &mut Context) {}
3799/// ```
3800///
3801/// The macro takes no parameters and should be applied directly to async functions
3802/// that accept a `&mut Context` parameter.
3803#[proc_macro_attribute]
3804pub fn try_flush(_attr: TokenStream, item: TokenStream) -> TokenStream {
3805 try_flush_macro(item, Position::Prologue)
3806}
3807
3808/// Flushes the response stream after function execution.
3809///
3810/// This attribute macro ensures that the response stream is flushed to guarantee immediate
3811/// data transmission, forcing any buffered response data to be sent to the client.
3812///
3813/// # Usage
3814///
3815/// ```rust
3816/// use hyperlane::*;
3817/// use hyperlane_macros::*;
3818///
3819/// #[route("/flush")]
3820/// struct FlushTest;
3821///
3822/// impl ServerHook for FlushTest {
3823/// async fn new(_ctx: &mut Context) -> Self {
3824/// Self
3825/// }
3826///
3827/// #[epilogue_macros(flush)]
3828/// async fn handle(self, ctx: &mut Context) {}
3829/// }
3830///
3831/// impl FlushTest {
3832/// #[flush]
3833/// async fn flush_with_ref_self(&self, ctx: &mut Context) {}
3834/// }
3835///
3836/// #[flush]
3837/// async fn standalone_flush_handler(ctx: &mut Context) {}
3838/// ```
3839///
3840/// The macro takes no parameters and should be applied directly to async functions
3841/// that accept a `&mut Context` parameter.
3842///
3843/// # Panics
3844///
3845/// This macro will panic if the flush operation fails.
3846#[proc_macro_attribute]
3847pub fn flush(_attr: TokenStream, item: TokenStream) -> TokenStream {
3848 flush_macro(item, Position::Prologue)
3849}