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