hyperlane_macros/lib.rs
1//! hyperlane-macros
2//!
3//! A comprehensive collection of procedural macros for building
4//! HTTP servers with enhanced functionality. This crate provides
5//! attribute macros that simplify HTTP request handling, protocol
6//! validation, response management, and request data extraction.
7
8mod aborted;
9mod closed;
10mod common;
11mod filter;
12mod flush;
13mod from_stream;
14mod hook;
15mod host;
16mod http;
17mod hyperlane;
18mod inject;
19mod protocol;
20mod referer;
21mod reject;
22mod request;
23mod request_middleware;
24mod response;
25mod response_middleware;
26mod route;
27mod send;
28mod stream;
29
30pub(crate) use aborted::*;
31pub(crate) use closed::*;
32pub(crate) use common::*;
33pub(crate) use filter::*;
34pub(crate) use flush::*;
35pub(crate) use from_stream::*;
36pub(crate) use hook::*;
37pub(crate) use host::*;
38pub(crate) use http::*;
39pub(crate) use hyperlane::*;
40pub(crate) use inject::*;
41pub(crate) use protocol::*;
42pub(crate) use referer::*;
43pub(crate) use reject::*;
44pub(crate) use request::*;
45pub(crate) use request_middleware::*;
46pub(crate) use response::*;
47pub(crate) use response_middleware::*;
48pub(crate) use route::*;
49pub(crate) use send::*;
50pub(crate) use stream::*;
51
52pub(crate) use ::hyperlane::inventory;
53pub(crate) use proc_macro::TokenStream;
54pub(crate) use proc_macro2::TokenStream as TokenStream2;
55pub(crate) use quote::quote;
56pub(crate) use syn::{
57 Ident, Token,
58 parse::{Parse, ParseStream, Parser, Result},
59 punctuated::Punctuated,
60 token::Comma,
61 *,
62};
63
64inventory::collect!(InjectableMacro);
65
66/// Restricts function execution to HTTP GET requests only.
67///
68/// This attribute macro ensures the decorated function only executes when the incoming request
69/// uses the GET HTTP method. Requests with other methods will be filtered out.
70///
71/// # Usage
72///
73/// ```rust
74/// use hyperlane::*;
75/// use hyperlane_macros::*;
76///
77/// #[route("/get")]
78/// struct Get;
79///
80/// impl ServerHook for Get {
81/// async fn new(_ctx: &Context) -> Self {
82/// Self
83/// }
84///
85/// #[prologue_macros(get, response_body("get"))]
86/// async fn handle(self, ctx: &Context) {}
87/// }
88///
89/// impl Get {
90/// #[get]
91/// async fn get_with_ref_self(&self, ctx: &Context) {}
92/// }
93///
94/// #[get]
95/// async fn standalone_get_handler(ctx: &Context) {}
96/// ```
97///
98/// The macro takes no parameters and should be applied directly to async functions
99/// that accept a `&Context` parameter.
100#[proc_macro_attribute]
101pub fn get(_attr: TokenStream, item: TokenStream) -> TokenStream {
102 get_handler(item, Position::Prologue)
103}
104
105/// Restricts function execution to HTTP POST requests only.
106///
107/// This attribute macro ensures the decorated function only executes when the incoming request
108/// uses the POST HTTP method. Requests with other methods will be filtered out.
109///
110/// # Usage
111///
112/// ```rust
113/// use hyperlane::*;
114/// use hyperlane_macros::*;
115///
116/// #[route("/post")]
117/// struct Post;
118///
119/// impl ServerHook for Post {
120/// async fn new(_ctx: &Context) -> Self {
121/// Self
122/// }
123///
124/// #[prologue_macros(post, response_body("post"))]
125/// async fn handle(self, ctx: &Context) {}
126/// }
127///
128/// impl Post {
129/// #[post]
130/// async fn post_with_ref_self(&self, ctx: &Context) {}
131/// }
132///
133/// #[post]
134/// async fn standalone_post_handler(ctx: &Context) {}
135/// ```
136///
137/// The macro takes no parameters and should be applied directly to async functions
138/// that accept a `&Context` parameter.
139#[proc_macro_attribute]
140pub fn post(_attr: TokenStream, item: TokenStream) -> TokenStream {
141 epilogue_handler(item, Position::Prologue)
142}
143
144/// Restricts function execution to HTTP PUT requests only.
145///
146/// This attribute macro ensures the decorated function only executes when the incoming request
147/// uses the PUT HTTP method. Requests with other methods will be filtered out.
148///
149/// # Usage
150///
151/// ```rust
152/// use hyperlane::*;
153/// use hyperlane_macros::*;
154///
155/// #[route("/put")]
156/// struct Put;
157///
158/// impl ServerHook for Put {
159/// async fn new(_ctx: &Context) -> Self {
160/// Self
161/// }
162///
163/// #[prologue_macros(put, response_body("put"))]
164/// async fn handle(self, ctx: &Context) {}
165/// }
166///
167/// impl Put {
168/// #[put]
169/// async fn put_with_ref_self(&self, ctx: &Context) {}
170/// }
171///
172/// #[put]
173/// async fn standalone_put_handler(ctx: &Context) {}
174/// ```
175///
176/// The macro takes no parameters and should be applied directly to async functions
177/// that accept a `&Context` parameter.
178#[proc_macro_attribute]
179pub fn put(_attr: TokenStream, item: TokenStream) -> TokenStream {
180 put_handler(item, Position::Prologue)
181}
182
183/// Restricts function execution to HTTP DELETE requests only.
184///
185/// This attribute macro ensures the decorated function only executes when the incoming request
186/// uses the DELETE HTTP method. Requests with other methods will be filtered out.
187///
188/// # Usage
189///
190/// ```rust
191/// use hyperlane::*;
192/// use hyperlane_macros::*;
193///
194/// #[route("/delete")]
195/// struct Delete;
196///
197/// impl ServerHook for Delete {
198/// async fn new(_ctx: &Context) -> Self {
199/// Self
200/// }
201///
202/// #[prologue_macros(delete, response_body("delete"))]
203/// async fn handle(self, ctx: &Context) {}
204/// }
205///
206/// impl Delete {
207/// #[delete]
208/// async fn delete_with_ref_self(&self, ctx: &Context) {}
209/// }
210///
211/// #[delete]
212/// async fn standalone_delete_handler(ctx: &Context) {}
213/// ```
214///
215/// The macro takes no parameters and should be applied directly to async functions
216/// that accept a `&Context` parameter.
217#[proc_macro_attribute]
218pub fn delete(_attr: TokenStream, item: TokenStream) -> TokenStream {
219 delete_handler(item, Position::Prologue)
220}
221
222/// Restricts function execution to HTTP PATCH requests only.
223///
224/// This attribute macro ensures the decorated function only executes when the incoming request
225/// uses the PATCH HTTP method. Requests with other methods will be filtered out.
226///
227/// # Usage
228///
229/// ```rust
230/// use hyperlane::*;
231/// use hyperlane_macros::*;
232///
233/// #[route("/patch")]
234/// struct Patch;
235///
236/// impl ServerHook for Patch {
237/// async fn new(_ctx: &Context) -> Self {
238/// Self
239/// }
240///
241/// #[prologue_macros(patch, response_body("patch"))]
242/// async fn handle(self, ctx: &Context) {}
243/// }
244///
245/// impl Patch {
246/// #[patch]
247/// async fn patch_with_ref_self(&self, ctx: &Context) {}
248/// }
249///
250/// #[patch]
251/// async fn standalone_patch_handler(ctx: &Context) {}
252/// ```
253///
254/// The macro takes no parameters and should be applied directly to async functions
255/// that accept a `&Context` parameter.
256#[proc_macro_attribute]
257pub fn patch(_attr: TokenStream, item: TokenStream) -> TokenStream {
258 patch_handler(item, Position::Prologue)
259}
260
261/// Restricts function execution to HTTP HEAD requests only.
262///
263/// This attribute macro ensures the decorated function only executes when the incoming request
264/// uses the HEAD HTTP method. Requests with other methods will be filtered out.
265///
266/// # Usage
267///
268/// ```rust
269/// use hyperlane::*;
270/// use hyperlane_macros::*;
271///
272/// #[route("/head")]
273/// struct Head;
274///
275/// impl ServerHook for Head {
276/// async fn new(_ctx: &Context) -> Self {
277/// Self
278/// }
279///
280/// #[prologue_macros(head, response_body("head"))]
281/// async fn handle(self, ctx: &Context) {}
282/// }
283///
284/// impl Head {
285/// #[head]
286/// async fn head_with_ref_self(&self, ctx: &Context) {}
287/// }
288///
289/// #[head]
290/// async fn standalone_head_handler(ctx: &Context) {}
291/// ```
292///
293/// The macro takes no parameters and should be applied directly to async functions
294/// that accept a `&Context` parameter.
295#[proc_macro_attribute]
296pub fn head(_attr: TokenStream, item: TokenStream) -> TokenStream {
297 head_handler(item, Position::Prologue)
298}
299
300/// Restricts function execution to HTTP OPTIONS requests only.
301///
302/// This attribute macro ensures the decorated function only executes when the incoming request
303/// uses the OPTIONS HTTP method. Requests with other methods will be filtered out.
304///
305/// # Usage
306///
307/// ```rust
308/// use hyperlane::*;
309/// use hyperlane_macros::*;
310///
311/// #[route("/options")]
312/// struct Options;
313///
314/// impl ServerHook for Options {
315/// async fn new(_ctx: &Context) -> Self {
316/// Self
317/// }
318///
319/// #[prologue_macros(options, response_body("options"))]
320/// async fn handle(self, ctx: &Context) {}
321/// }
322///
323/// impl Options {
324/// #[options]
325/// async fn options_with_ref_self(&self, ctx: &Context) {}
326/// }
327///
328/// #[options]
329/// async fn standalone_options_handler(ctx: &Context) {}
330/// ```
331///
332/// The macro takes no parameters and should be applied directly to async functions
333/// that accept a `&Context` parameter.
334#[proc_macro_attribute]
335pub fn options(_attr: TokenStream, item: TokenStream) -> TokenStream {
336 options_handler(item, Position::Prologue)
337}
338
339/// Restricts function execution to HTTP CONNECT requests only.
340///
341/// This attribute macro ensures the decorated function only executes when the incoming request
342/// uses the CONNECT 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("/connect")]
351/// struct Connect;
352///
353/// impl ServerHook for Connect {
354/// async fn new(_ctx: &Context) -> Self {
355/// Self
356/// }
357///
358/// #[prologue_macros(connect, response_body("connect"))]
359/// async fn handle(self, ctx: &Context) {}
360/// }
361///
362/// impl Connect {
363/// #[connect]
364/// async fn connect_with_ref_self(&self, ctx: &Context) {}
365/// }
366///
367/// #[connect]
368/// async fn standalone_connect_handler(ctx: &Context) {}
369/// ```
370///
371/// The macro takes no parameters and should be applied directly to async functions
372/// that accept a `&Context` parameter.
373#[proc_macro_attribute]
374pub fn connect(_attr: TokenStream, item: TokenStream) -> TokenStream {
375 connect_handler(item, Position::Prologue)
376}
377
378/// Restricts function execution to HTTP TRACE requests only.
379///
380/// This attribute macro ensures the decorated function only executes when the incoming request
381/// uses the TRACE HTTP method. Requests with other methods will be filtered out.
382///
383/// # Usage
384///
385/// ```rust
386/// use hyperlane::*;
387/// use hyperlane_macros::*;
388///
389/// #[route("/trace")]
390/// struct Trace;
391///
392/// impl ServerHook for Trace {
393/// async fn new(_ctx: &Context) -> Self {
394/// Self
395/// }
396///
397/// #[prologue_macros(trace, response_body("trace"))]
398/// async fn handle(self, ctx: &Context) {}
399/// }
400///
401/// impl Trace {
402/// #[trace]
403/// async fn trace_with_ref_self(&self, ctx: &Context) {}
404/// }
405///
406/// #[trace]
407/// async fn standalone_trace_handler(ctx: &Context) {}
408/// ```
409///
410/// The macro takes no parameters and should be applied directly to async functions
411/// that accept a `&Context` parameter.
412#[proc_macro_attribute]
413pub fn trace(_attr: TokenStream, item: TokenStream) -> TokenStream {
414 trace_handler(item, Position::Prologue)
415}
416
417/// Allows function to handle multiple HTTP methods.
418///
419/// This attribute macro configures the decorated function to execute for any of the specified
420/// HTTP methods. Methods should be provided as a comma-separated list.
421///
422/// # Usage
423///
424/// ```rust
425/// use hyperlane::*;
426/// use hyperlane_macros::*;
427///
428/// #[route("/get_post")]
429/// struct GetPost;
430///
431/// impl ServerHook for GetPost {
432/// async fn new(_ctx: &Context) -> Self {
433/// Self
434/// }
435///
436/// #[prologue_macros(
437/// http,
438/// methods(get, post),
439/// response_body("get_post")
440/// )]
441/// async fn handle(self, ctx: &Context) {}
442/// }
443///
444/// impl GetPost {
445/// #[methods(get, post)]
446/// async fn methods_with_ref_self(&self, ctx: &Context) {}
447/// }
448///
449/// #[methods(get, post)]
450/// async fn standalone_methods_handler(ctx: &Context) {}
451/// ```
452///
453/// The macro accepts a comma-separated list of HTTP method names (lowercase) and should be
454/// applied to async functions that accept a `&Context` parameter.
455#[proc_macro_attribute]
456pub fn methods(attr: TokenStream, item: TokenStream) -> TokenStream {
457 methods_macro(attr, item, Position::Prologue)
458}
459
460/// Restricts function execution to WebSocket upgrade requests only.
461///
462/// This attribute macro ensures the decorated function only executes when the incoming request
463/// is a valid WebSocket upgrade request with proper request headers and protocol negotiation.
464///
465/// # Usage
466///
467/// ```rust
468/// use hyperlane::*;
469/// use hyperlane_macros::*;
470///
471/// #[route("/ws")]
472/// struct Websocket;
473///
474/// impl ServerHook for Websocket {
475/// async fn new(_ctx: &Context) -> Self {
476/// Self
477/// }
478///
479/// #[ws]
480/// #[ws_from_stream]
481/// async fn handle(self, ctx: &Context) {
482/// let body: RequestBody = ctx.get_request_body().await;
483/// let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(&body);
484/// ctx.send_body_list_with_data(&body_list).await.unwrap();
485/// }
486/// }
487///
488/// impl Websocket {
489/// #[ws]
490/// async fn ws_with_ref_self(&self, ctx: &Context) {}
491/// }
492///
493/// #[ws]
494/// async fn standalone_ws_handler(ctx: &Context) {}
495/// ```
496///
497/// The macro takes no parameters and should be applied directly to async functions
498/// that accept a `&Context` parameter.
499#[proc_macro_attribute]
500pub fn ws(_attr: TokenStream, item: TokenStream) -> TokenStream {
501 ws_macro(item, Position::Prologue)
502}
503
504/// Restricts function execution to standard HTTP requests only.
505///
506/// This attribute macro ensures the decorated function only executes for standard HTTP requests,
507/// excluding WebSocket upgrades and other protocol upgrade requests.
508///
509/// # Usage
510///
511/// ```rust
512/// use hyperlane::*;
513/// use hyperlane_macros::*;
514///
515/// #[route("/http")]
516/// struct HttpOnly;
517///
518/// impl ServerHook for HttpOnly {
519/// async fn new(_ctx: &Context) -> Self {
520/// Self
521/// }
522///
523/// #[prologue_macros(http, response_body("http"))]
524/// async fn handle(self, ctx: &Context) {}
525/// }
526///
527/// impl HttpOnly {
528/// #[http]
529/// async fn http_with_ref_self(&self, ctx: &Context) {}
530/// }
531///
532/// #[http]
533/// async fn standalone_http_handler(ctx: &Context) {}
534/// ```
535///
536/// The macro takes no parameters and should be applied directly to async functions
537/// that accept a `&Context` parameter.
538#[proc_macro_attribute]
539pub fn http(_attr: TokenStream, item: TokenStream) -> TokenStream {
540 http_macro(item, Position::Prologue)
541}
542
543/// Sets the HTTP status code for the response.
544///
545/// This attribute macro configures the HTTP status code that will be sent with the response.
546/// The status code can be provided as a numeric literal or a global constant.
547///
548/// # Usage
549///
550/// ```rust
551/// use hyperlane::*;
552/// use hyperlane_macros::*;
553///
554/// const CUSTOM_STATUS_CODE: i32 = 200;
555///
556/// #[route("/response")]
557/// struct Response;
558///
559/// impl ServerHook for Response {
560/// async fn new(_ctx: &Context) -> Self {
561/// Self
562/// }
563///
564/// #[response_status_code(CUSTOM_STATUS_CODE)]
565/// async fn handle(self, ctx: &Context) {}
566/// }
567///
568/// impl Response {
569/// #[response_status_code(CUSTOM_STATUS_CODE)]
570/// async fn response_status_code_with_ref_self(&self, ctx: &Context) {}
571/// }
572///
573/// #[response_status_code(200)]
574/// async fn standalone_response_status_code_handler(ctx: &Context) {}
575/// ```
576///
577/// The macro accepts a numeric HTTP status code or a global constant
578/// and should be applied to async functions that accept a `&Context` parameter.
579#[proc_macro_attribute]
580pub fn response_status_code(attr: TokenStream, item: TokenStream) -> TokenStream {
581 response_status_code_macro(attr, item, Position::Prologue)
582}
583
584/// Sets the HTTP reason phrase for the response.
585///
586/// This attribute macro configures the HTTP reason phrase that accompanies the status code.
587/// The reason phrase can be provided as a string literal or a global constant.
588///
589/// # Usage
590///
591/// ```rust
592/// use hyperlane::*;
593/// use hyperlane_macros::*;
594///
595/// const CUSTOM_REASON: &str = "Accepted";
596///
597/// #[route("/response")]
598/// struct Response;
599///
600/// impl ServerHook for Response {
601/// async fn new(_ctx: &Context) -> Self {
602/// Self
603/// }
604///
605/// #[response_reason_phrase(CUSTOM_REASON)]
606/// async fn handle(self, ctx: &Context) {}
607/// }
608///
609/// impl Response {
610/// #[response_reason_phrase(CUSTOM_REASON)]
611/// async fn response_reason_phrase_with_ref_self(&self, ctx: &Context) {}
612/// }
613///
614/// #[response_reason_phrase("OK")]
615/// async fn standalone_response_reason_phrase_handler(ctx: &Context) {}
616/// ```
617///
618/// The macro accepts a string literal or global constant for the reason phrase and should be
619/// applied to async functions that accept a `&Context` parameter.
620#[proc_macro_attribute]
621pub fn response_reason_phrase(attr: TokenStream, item: TokenStream) -> TokenStream {
622 response_reason_phrase_macro(attr, item, Position::Prologue)
623}
624
625/// Sets or replaces a specific HTTP response header.
626///
627/// This attribute macro configures a specific HTTP response header that will be sent with the response.
628/// Both the header name and value can be provided as string literals or global constants.
629/// Use `"key", "value"` to set a header (add to existing headers) or `"key" => "value"` to replace a header (overwrite existing).
630///
631/// # Usage
632///
633/// ```rust
634/// use hyperlane::*;
635/// use hyperlane_macros::*;
636///
637/// const CUSTOM_HEADER_NAME: &str = "X-Custom-Header";
638/// const CUSTOM_HEADER_VALUE: &str = "custom-value";
639///
640/// #[route("/response")]
641/// struct Response;
642///
643/// impl ServerHook for Response {
644/// async fn new(_ctx: &Context) -> Self {
645/// Self
646/// }
647///
648/// #[response_header(CUSTOM_HEADER_NAME => CUSTOM_HEADER_VALUE)]
649/// async fn handle(self, ctx: &Context) {}
650/// }
651///
652/// impl Response {
653/// #[response_header(CUSTOM_HEADER_NAME => CUSTOM_HEADER_VALUE)]
654/// async fn response_header_with_ref_self(&self, ctx: &Context) {}
655/// }
656///
657/// #[route("/response_header")]
658/// struct ResponseHeaderTest;
659///
660/// impl ServerHook for ResponseHeaderTest {
661/// async fn new(_ctx: &Context) -> Self {
662/// Self
663/// }
664///
665/// #[response_body("Testing header set and replace operations")]
666/// #[response_header("X-Add-Header", "add-value")]
667/// #[response_header("X-Set-Header" => "set-value")]
668/// async fn handle(self, ctx: &Context) {}
669/// }
670///
671/// #[response_header("X-Custom" => "value")]
672/// async fn standalone_response_header_handler(ctx: &Context) {}
673/// ```
674///
675/// The macro accepts header name and header value, both can be string literals or global constants.
676/// Use `"key", "value"` for setting headers and `"key" => "value"` for replacing headers.
677/// Should be applied to async functions that accept a `&Context` parameter.
678#[proc_macro_attribute]
679pub fn response_header(attr: TokenStream, item: TokenStream) -> TokenStream {
680 response_header_macro(attr, item, Position::Prologue)
681}
682
683/// Sets the HTTP response body.
684///
685/// This attribute macro configures the HTTP response body that will be sent with the response.
686/// The body content can be provided as a string literal or a global constant.
687///
688/// # Usage
689///
690/// ```rust
691/// use hyperlane::*;
692/// use hyperlane_macros::*;
693///
694/// const RESPONSE_DATA: &str = "{\"status\": \"success\"}";
695///
696/// #[route("/response")]
697/// struct Response;
698///
699/// impl ServerHook for Response {
700/// async fn new(_ctx: &Context) -> Self {
701/// Self
702/// }
703///
704/// #[response_body(&RESPONSE_DATA)]
705/// async fn handle(self, ctx: &Context) {}
706/// }
707///
708/// impl Response {
709/// #[response_body(&RESPONSE_DATA)]
710/// async fn response_body_with_ref_self(&self, ctx: &Context) {}
711/// }
712///
713/// #[response_body("standalone response body")]
714/// async fn standalone_response_body_handler(ctx: &Context) {}
715/// ```
716///
717/// The macro accepts a string literal or global constant for the response body and should be
718/// applied to async functions that accept a `&Context` parameter.
719#[proc_macro_attribute]
720pub fn response_body(attr: TokenStream, item: TokenStream) -> TokenStream {
721 response_body_macro(attr, item, Position::Prologue)
722}
723
724/// Clears all response headers.
725///
726/// This attribute macro clears all response headers from the response.
727///
728/// # Usage
729///
730/// ```rust
731/// use hyperlane::*;
732/// use hyperlane_macros::*;
733///
734/// #[route("/unknown_method")]
735/// struct UnknownMethod;
736///
737/// impl ServerHook for UnknownMethod {
738/// async fn new(_ctx: &Context) -> Self {
739/// Self
740/// }
741///
742/// #[prologue_macros(
743/// clear_response_headers,
744/// filter(ctx.get_request().await.is_unknown_method()),
745/// response_body("unknown_method")
746/// )]
747/// async fn handle(self, ctx: &Context) {}
748/// }
749///
750/// impl UnknownMethod {
751/// #[clear_response_headers]
752/// async fn clear_response_headers_with_ref_self(&self, ctx: &Context) {}
753/// }
754///
755/// #[clear_response_headers]
756/// async fn standalone_clear_response_headers_handler(ctx: &Context) {}
757/// ```
758///
759/// The macro should be applied to async functions that accept a `&Context` parameter.
760#[proc_macro_attribute]
761pub fn clear_response_headers(_attr: TokenStream, item: TokenStream) -> TokenStream {
762 clear_response_headers_macro(item, Position::Prologue)
763}
764
765/// Sets the HTTP response version.
766///
767/// This attribute macro configures the HTTP response version that will be sent with the response.
768/// The version can be provided as a variable or code block.
769///
770/// # Usage
771///
772/// ```rust
773/// use hyperlane::*;
774/// use hyperlane_macros::*;
775///
776/// #[request_middleware]
777/// struct RequestMiddleware;
778///
779/// impl ServerHook for RequestMiddleware {
780/// async fn new(_ctx: &Context) -> Self {
781/// Self
782/// }
783///
784/// #[epilogue_macros(
785/// response_status_code(200),
786/// response_version(HttpVersion::HTTP1_1),
787/// response_header(SERVER => HYPERLANE)
788/// )]
789/// async fn handle(self, ctx: &Context) {}
790/// }
791/// ```
792///
793/// The macro accepts a variable or code block for the response version and should be
794/// applied to async functions that accept a `&Context` parameter.
795#[proc_macro_attribute]
796pub fn response_version(attr: TokenStream, item: TokenStream) -> TokenStream {
797 response_version_macro(attr, item, Position::Prologue)
798}
799
800/// Automatically sends the complete response after function execution.
801///
802/// This attribute macro ensures that the response (request headers and body) is automatically sent
803/// to the client after the function completes execution.
804///
805/// # Usage
806///
807/// ```rust
808/// use hyperlane::*;
809/// use hyperlane_macros::*;
810///
811/// #[route("/send")]
812/// struct SendTest;
813///
814/// impl ServerHook for SendTest {
815/// async fn new(_ctx: &Context) -> Self {
816/// Self
817/// }
818///
819/// #[epilogue_macros(send)]
820/// async fn handle(self, ctx: &Context) {}
821/// }
822///
823/// impl SendTest {
824/// #[send]
825/// async fn send_with_ref_self(&self, ctx: &Context) {}
826/// }
827///
828/// #[send]
829/// async fn standalone_send_handler(ctx: &Context) {}
830/// ```
831///
832/// The macro takes no parameters and should be applied directly to async functions
833/// that accept a `&Context` parameter.
834#[proc_macro_attribute]
835pub fn send(_attr: TokenStream, item: TokenStream) -> TokenStream {
836 send_macro(item, Position::Epilogue)
837}
838
839/// Automatically sends only the response body after function execution.
840///
841/// This attribute macro ensures that only the response body is automatically sent
842/// to the client after the function completes, handling request headers separately.
843///
844/// # Usage
845///
846/// ```rust
847/// use hyperlane::*;
848/// use hyperlane_macros::*;
849///
850/// #[route("/send_body")]
851/// struct SendBodyTest;
852///
853/// impl ServerHook for SendBodyTest {
854/// async fn new(_ctx: &Context) -> Self {
855/// Self
856/// }
857///
858/// #[epilogue_macros(send_body)]
859/// async fn handle(self, ctx: &Context) {}
860/// }
861///
862/// impl SendBodyTest {
863/// #[send_body]
864/// async fn send_body_with_ref_self(&self, ctx: &Context) {}
865/// }
866///
867/// #[send_body]
868/// async fn standalone_send_body_handler(ctx: &Context) {}
869/// ```
870///
871/// The macro takes no parameters and should be applied directly to async functions
872/// that accept a `&Context` parameter.
873#[proc_macro_attribute]
874pub fn send_body(_attr: TokenStream, item: TokenStream) -> TokenStream {
875 send_body_macro(item, Position::Epilogue)
876}
877
878/// Flushes the response stream after function execution.
879///
880/// This attribute macro ensures that the response stream is flushed to guarantee immediate
881/// data transmission, forcing any buffered response data to be sent to the client.
882///
883/// # Usage
884///
885/// ```rust
886/// use hyperlane::*;
887/// use hyperlane_macros::*;
888///
889/// #[route("/flush")]
890/// struct FlushTest;
891///
892/// impl ServerHook for FlushTest {
893/// async fn new(_ctx: &Context) -> Self {
894/// Self
895/// }
896///
897/// #[epilogue_macros(flush)]
898/// async fn handle(self, ctx: &Context) {}
899/// }
900///
901/// impl FlushTest {
902/// #[flush]
903/// async fn flush_with_ref_self(&self, ctx: &Context) {}
904/// }
905///
906/// #[flush]
907/// async fn standalone_flush_handler(ctx: &Context) {}
908/// ```
909///
910/// The macro takes no parameters and should be applied directly to async functions
911/// that accept a `&Context` parameter.
912#[proc_macro_attribute]
913pub fn flush(_attr: TokenStream, item: TokenStream) -> TokenStream {
914 flush_macro(item, Position::Prologue)
915}
916
917/// Handles aborted request scenarios.
918///
919/// This attribute macro configures the function to handle cases where the client has
920/// aborted the request, providing appropriate handling for interrupted or cancelled requests.
921///
922/// # Usage
923///
924/// ```rust
925/// use hyperlane::*;
926/// use hyperlane_macros::*;
927///
928/// #[route("/aborted")]
929/// struct Aborted;
930///
931/// impl ServerHook for Aborted {
932/// async fn new(_ctx: &Context) -> Self {
933/// Self
934/// }
935///
936/// #[aborted]
937/// async fn handle(self, ctx: &Context) {}
938/// }
939///
940/// impl Aborted {
941/// #[aborted]
942/// async fn aborted_with_ref_self(&self, ctx: &Context) {}
943/// }
944///
945/// #[aborted]
946/// async fn standalone_aborted_handler(ctx: &Context) {}
947/// ```
948///
949/// The macro takes no parameters and should be applied directly to async functions
950/// that accept a `&Context` parameter.
951#[proc_macro_attribute]
952pub fn aborted(_attr: TokenStream, item: TokenStream) -> TokenStream {
953 aborted_macro(item, Position::Prologue)
954}
955
956/// Handles closed connection scenarios.
957///
958/// This attribute macro configures the function to handle cases where the connection
959/// has been closed, providing appropriate handling for terminated or disconnected connections.
960///
961/// # Usage
962///
963/// ```rust
964/// use hyperlane::*;
965/// use hyperlane_macros::*;
966///
967/// #[route("/closed")]
968/// struct ClosedTest;
969///
970/// impl ServerHook for ClosedTest {
971/// async fn new(_ctx: &Context) -> Self {
972/// Self
973/// }
974///
975/// #[closed]
976/// async fn handle(self, ctx: &Context) {}
977/// }
978///
979/// impl ClosedTest {
980/// #[closed]
981/// async fn closed_with_ref_self(&self, ctx: &Context) {}
982/// }
983///
984/// #[closed]
985/// async fn standalone_closed_handler(ctx: &Context) {}
986/// ```
987///
988/// The macro takes no parameters and should be applied directly to async functions
989/// that accept a `&Context` parameter.
990#[proc_macro_attribute]
991pub fn closed(_attr: TokenStream, item: TokenStream) -> TokenStream {
992 closed_macro(item, Position::Prologue)
993}
994
995/// Restricts function execution to HTTP/2 Cleartext (h2c) requests only.
996///
997/// This attribute macro ensures the decorated function only executes for HTTP/2 cleartext
998/// requests that use the h2c upgrade mechanism.
999///
1000/// # Usage
1001///
1002/// ```rust
1003/// use hyperlane::*;
1004/// use hyperlane_macros::*;
1005///
1006/// #[route("/h2c")]
1007/// struct H2c;
1008///
1009/// impl ServerHook for H2c {
1010/// async fn new(_ctx: &Context) -> Self {
1011/// Self
1012/// }
1013///
1014/// #[prologue_macros(h2c, response_body("h2c"))]
1015/// async fn handle(self, ctx: &Context) {}
1016/// }
1017///
1018/// impl H2c {
1019/// #[h2c]
1020/// async fn h2c_with_ref_self(&self, ctx: &Context) {}
1021/// }
1022///
1023/// #[h2c]
1024/// async fn standalone_h2c_handler(ctx: &Context) {}
1025/// ```
1026///
1027/// The macro takes no parameters and should be applied directly to async functions
1028/// that accept a `&Context` parameter.
1029#[proc_macro_attribute]
1030pub fn h2c(_attr: TokenStream, item: TokenStream) -> TokenStream {
1031 h2c_macro(item, Position::Prologue)
1032}
1033
1034/// Restricts function execution to HTTP/0.9 requests only.
1035///
1036/// This attribute macro ensures the decorated function only executes for HTTP/0.9
1037/// protocol requests, the earliest version of the HTTP protocol.
1038///
1039/// # Usage
1040///
1041/// ```rust
1042/// use hyperlane::*;
1043/// use hyperlane_macros::*;
1044///
1045/// #[route("/http0_9")]
1046/// struct Http09;
1047///
1048/// impl ServerHook for Http09 {
1049/// async fn new(_ctx: &Context) -> Self {
1050/// Self
1051/// }
1052///
1053/// #[prologue_macros(http0_9, response_body("http0_9"))]
1054/// async fn handle(self, ctx: &Context) {}
1055/// }
1056///
1057/// impl Http09 {
1058/// #[http0_9]
1059/// async fn http0_9_with_ref_self(&self, ctx: &Context) {}
1060/// }
1061///
1062/// #[http0_9]
1063/// async fn standalone_http0_9_handler(ctx: &Context) {}
1064/// ```
1065///
1066/// The macro takes no parameters and should be applied directly to async functions
1067/// that accept a `&Context` parameter.
1068#[proc_macro_attribute]
1069pub fn http0_9(_attr: TokenStream, item: TokenStream) -> TokenStream {
1070 http0_9_macro(item, Position::Prologue)
1071}
1072
1073/// Restricts function execution to HTTP/1.0 requests only.
1074///
1075/// This attribute macro ensures the decorated function only executes for HTTP/1.0
1076/// protocol requests.
1077///
1078/// # Usage
1079///
1080/// ```rust
1081/// use hyperlane::*;
1082/// use hyperlane_macros::*;
1083///
1084/// #[route("/http1_0")]
1085/// struct Http10;
1086///
1087/// impl ServerHook for Http10 {
1088/// async fn new(_ctx: &Context) -> Self {
1089/// Self
1090/// }
1091///
1092/// #[prologue_macros(http1_0, response_body("http1_0"))]
1093/// async fn handle(self, ctx: &Context) {}
1094/// }
1095///
1096/// impl Http10 {
1097/// #[http1_0]
1098/// async fn http1_0_with_ref_self(&self, ctx: &Context) {}
1099/// }
1100///
1101/// #[http1_0]
1102/// async fn standalone_http1_0_handler(ctx: &Context) {}
1103/// ```
1104///
1105/// The macro takes no parameters and should be applied directly to async functions
1106/// that accept a `&Context` parameter.
1107#[proc_macro_attribute]
1108pub fn http1_0(_attr: TokenStream, item: TokenStream) -> TokenStream {
1109 http1_0_macro(item, Position::Prologue)
1110}
1111
1112/// Restricts function execution to HTTP/1.1 requests only.
1113///
1114/// This attribute macro ensures the decorated function only executes for HTTP/1.1
1115/// protocol requests.
1116///
1117/// # Usage
1118///
1119/// ```rust
1120/// use hyperlane::*;
1121/// use hyperlane_macros::*;
1122///
1123/// #[route("/http1_1")]
1124/// struct Http11;
1125///
1126/// impl ServerHook for Http11 {
1127/// async fn new(_ctx: &Context) -> Self {
1128/// Self
1129/// }
1130///
1131/// #[prologue_macros(http1_1, response_body("http1_1"))]
1132/// async fn handle(self, ctx: &Context) {}
1133/// }
1134///
1135/// impl Http11 {
1136/// #[http1_1]
1137/// async fn http1_1_with_ref_self(&self, ctx: &Context) {}
1138/// }
1139///
1140/// #[http1_1]
1141/// async fn standalone_http1_1_handler(ctx: &Context) {}
1142/// ```
1143///
1144/// The macro takes no parameters and should be applied directly to async functions
1145/// that accept a `&Context` parameter.
1146#[proc_macro_attribute]
1147pub fn http1_1(_attr: TokenStream, item: TokenStream) -> TokenStream {
1148 http1_1_macro(item, Position::Prologue)
1149}
1150
1151/// Restricts function execution to HTTP/1.1 or higher protocol versions.
1152///
1153/// This attribute macro ensures the decorated function only executes for HTTP/1.1
1154/// or newer protocol versions, including HTTP/2, HTTP/3, and future versions.
1155///
1156/// # Usage
1157///
1158/// ```rust
1159/// use hyperlane::*;
1160/// use hyperlane_macros::*;
1161///
1162/// #[route("/http1_1_or_higher")]
1163/// struct Http11OrHigher;
1164///
1165/// impl ServerHook for Http11OrHigher {
1166/// async fn new(_ctx: &Context) -> Self {
1167/// Self
1168/// }
1169///
1170/// #[prologue_macros(http1_1_or_higher, response_body("http1_1_or_higher"))]
1171/// async fn handle(self, ctx: &Context) {}
1172/// }
1173///
1174/// impl Http11OrHigher {
1175/// #[http1_1_or_higher]
1176/// async fn http1_1_or_higher_with_ref_self(&self, ctx: &Context) {}
1177/// }
1178///
1179/// #[http1_1_or_higher]
1180/// async fn standalone_http1_1_or_higher_handler(ctx: &Context) {}
1181/// ```
1182///
1183/// The macro takes no parameters and should be applied directly to async functions
1184/// that accept a `&Context` parameter.
1185#[proc_macro_attribute]
1186pub fn http1_1_or_higher(_attr: TokenStream, item: TokenStream) -> TokenStream {
1187 http1_1_or_higher_macro(item, Position::Prologue)
1188}
1189
1190/// Restricts function execution to HTTP/2 requests only.
1191///
1192/// This attribute macro ensures the decorated function only executes for HTTP/2
1193/// protocol requests.
1194///
1195/// # Usage
1196///
1197/// ```rust
1198/// use hyperlane::*;
1199/// use hyperlane_macros::*;
1200///
1201/// #[route("/http2")]
1202/// struct Http2;
1203///
1204/// impl ServerHook for Http2 {
1205/// async fn new(_ctx: &Context) -> Self {
1206/// Self
1207/// }
1208///
1209/// #[prologue_macros(http2, response_body("http2"))]
1210/// async fn handle(self, ctx: &Context) {}
1211/// }
1212///
1213/// impl Http2 {
1214/// #[http2]
1215/// async fn http2_with_ref_self(&self, ctx: &Context) {}
1216/// }
1217///
1218/// #[http2]
1219/// async fn standalone_http2_handler(ctx: &Context) {}
1220/// ```
1221///
1222/// The macro takes no parameters and should be applied directly to async functions
1223/// that accept a `&Context` parameter.
1224#[proc_macro_attribute]
1225pub fn http2(_attr: TokenStream, item: TokenStream) -> TokenStream {
1226 http2_macro(item, Position::Prologue)
1227}
1228
1229/// Restricts function execution to HTTP/3 requests only.
1230///
1231/// This attribute macro ensures the decorated function only executes for HTTP/3
1232/// protocol requests, the latest version of the HTTP protocol.
1233///
1234/// # Usage
1235///
1236/// ```rust
1237/// use hyperlane::*;
1238/// use hyperlane_macros::*;
1239///
1240/// #[route("/http3")]
1241/// struct Http3;
1242///
1243/// impl ServerHook for Http3 {
1244/// async fn new(_ctx: &Context) -> Self {
1245/// Self
1246/// }
1247///
1248/// #[prologue_macros(http3, response_body("http3"))]
1249/// async fn handle(self, ctx: &Context) {}
1250/// }
1251///
1252/// impl Http3 {
1253/// #[http3]
1254/// async fn http3_with_ref_self(&self, ctx: &Context) {}
1255/// }
1256///
1257/// #[http3]
1258/// async fn standalone_http3_handler(ctx: &Context) {}
1259/// ```
1260///
1261/// The macro takes no parameters and should be applied directly to async functions
1262/// that accept a `&Context` parameter.
1263#[proc_macro_attribute]
1264pub fn http3(_attr: TokenStream, item: TokenStream) -> TokenStream {
1265 http3_macro(item, Position::Prologue)
1266}
1267
1268/// Restricts function execution to TLS-encrypted requests only.
1269///
1270/// This attribute macro ensures the decorated function only executes for requests
1271/// that use TLS/SSL encryption on the connection.
1272///
1273/// # Usage
1274///
1275/// ```rust
1276/// use hyperlane::*;
1277/// use hyperlane_macros::*;
1278///
1279/// #[route("/tls")]
1280/// struct Tls;
1281///
1282/// impl ServerHook for Tls {
1283/// async fn new(_ctx: &Context) -> Self {
1284/// Self
1285/// }
1286///
1287/// #[prologue_macros(tls, response_body("tls"))]
1288/// async fn handle(self, ctx: &Context) {}
1289/// }
1290///
1291/// impl Tls {
1292/// #[tls]
1293/// async fn tls_with_ref_self(&self, ctx: &Context) {}
1294/// }
1295///
1296/// #[tls]
1297/// async fn standalone_tls_handler(ctx: &Context) {}
1298/// ```
1299///
1300/// The macro takes no parameters and should be applied directly to async functions
1301/// that accept a `&Context` parameter.
1302#[proc_macro_attribute]
1303pub fn tls(_attr: TokenStream, item: TokenStream) -> TokenStream {
1304 tls_macro(item, Position::Prologue)
1305}
1306
1307/// Filters requests based on a boolean condition.
1308///
1309/// The function continues execution only if the provided code block returns `true`.
1310///
1311/// # Usage
1312///
1313/// ```rust
1314/// use hyperlane::*;
1315/// use hyperlane_macros::*;
1316///
1317/// #[route("/unknown_method")]
1318/// struct UnknownMethod;
1319///
1320/// impl ServerHook for UnknownMethod {
1321/// async fn new(_ctx: &Context) -> Self {
1322/// Self
1323/// }
1324///
1325/// #[prologue_macros(
1326/// filter(ctx.get_request().await.is_unknown_method()),
1327/// response_body("unknown_method")
1328/// )]
1329/// async fn handle(self, ctx: &Context) {}
1330/// }
1331/// ```
1332#[proc_macro_attribute]
1333pub fn filter(attr: TokenStream, item: TokenStream) -> TokenStream {
1334 filter_macro(attr, item, Position::Prologue)
1335}
1336
1337/// Rejects requests based on a boolean condition.
1338///
1339/// The function continues execution only if the provided code block returns `false`.
1340///
1341/// # Usage
1342///
1343/// ```rust
1344/// use hyperlane::*;
1345/// use hyperlane_macros::*;
1346///
1347/// #[response_middleware(2)]
1348/// struct ResponseMiddleware2;
1349///
1350/// impl ServerHook for ResponseMiddleware2 {
1351/// async fn new(_ctx: &Context) -> Self {
1352/// Self
1353/// }
1354///
1355/// #[prologue_macros(
1356/// reject(ctx.get_request().await.is_ws())
1357/// )]
1358/// async fn handle(self, ctx: &Context) {}
1359/// }
1360/// ```
1361#[proc_macro_attribute]
1362pub fn reject(attr: TokenStream, item: TokenStream) -> TokenStream {
1363 reject_macro(attr, item, Position::Prologue)
1364}
1365
1366/// Restricts function execution to requests with a specific host.
1367///
1368/// This attribute macro ensures the decorated function only executes when the incoming request
1369/// has a host header that matches the specified value. Requests with different or missing host headers will be filtered out.
1370///
1371/// # Usage
1372///
1373/// ```rust
1374/// use hyperlane::*;
1375/// use hyperlane_macros::*;
1376///
1377/// #[route("/host")]
1378/// struct Host;
1379///
1380/// impl ServerHook for Host {
1381/// async fn new(_ctx: &Context) -> Self {
1382/// Self
1383/// }
1384///
1385/// #[host("localhost")]
1386/// #[prologue_macros(response_body("host string literal: localhost"), send)]
1387/// async fn handle(self, ctx: &Context) {}
1388/// }
1389///
1390/// impl Host {
1391/// #[host("localhost")]
1392/// async fn host_with_ref_self(&self, ctx: &Context) {}
1393/// }
1394///
1395/// #[host("localhost")]
1396/// async fn standalone_host_handler(ctx: &Context) {}
1397/// ```
1398///
1399/// The macro accepts a string literal specifying the expected host value and should be
1400/// applied to async functions that accept a `&Context` parameter.
1401#[proc_macro_attribute]
1402pub fn host(attr: TokenStream, item: TokenStream) -> TokenStream {
1403 host_macro(attr, item, Position::Prologue)
1404}
1405
1406/// Reject requests that have no host header.
1407///
1408/// This attribute macro ensures the decorated function only executes when the incoming request
1409/// has a host header present. Requests without a host header will be filtered out.
1410///
1411/// # Usage
1412///
1413/// ```rust
1414/// use hyperlane::*;
1415/// use hyperlane_macros::*;
1416///
1417/// #[route("/reject_host")]
1418/// struct RejectHost;
1419///
1420/// impl ServerHook for RejectHost {
1421/// async fn new(_ctx: &Context) -> Self {
1422/// Self
1423/// }
1424///
1425/// #[prologue_macros(
1426/// reject_host("filter.localhost"),
1427/// response_body("host filter string literal")
1428/// )]
1429/// async fn handle(self, ctx: &Context) {}
1430/// }
1431///
1432/// impl RejectHost {
1433/// #[reject_host("filter.localhost")]
1434/// async fn reject_host_with_ref_self(&self, ctx: &Context) {}
1435/// }
1436///
1437/// #[reject_host("filter.localhost")]
1438/// async fn standalone_reject_host_handler(ctx: &Context) {}
1439/// ```
1440///
1441/// The macro takes no parameters and should be applied directly to async functions
1442/// that accept a `&Context` parameter.
1443#[proc_macro_attribute]
1444pub fn reject_host(attr: TokenStream, item: TokenStream) -> TokenStream {
1445 reject_host_macro(attr, item, Position::Prologue)
1446}
1447
1448/// Restricts function execution to requests with a specific referer.
1449///
1450/// This attribute macro ensures the decorated function only executes when the incoming request
1451/// has a referer header that matches the specified value. Requests with different or missing referer headers will be filtered out.
1452///
1453/// # Usage
1454///
1455/// ```rust
1456/// use hyperlane::*;
1457/// use hyperlane_macros::*;
1458///
1459/// #[route("/referer")]
1460/// struct Referer;
1461///
1462/// impl ServerHook for Referer {
1463/// async fn new(_ctx: &Context) -> Self {
1464/// Self
1465/// }
1466///
1467/// #[prologue_macros(
1468/// referer("http://localhost"),
1469/// response_body("referer string literal: http://localhost")
1470/// )]
1471/// async fn handle(self, ctx: &Context) {}
1472/// }
1473///
1474/// impl Referer {
1475/// #[referer("http://localhost")]
1476/// async fn referer_with_ref_self(&self, ctx: &Context) {}
1477/// }
1478///
1479/// #[referer("http://localhost")]
1480/// async fn standalone_referer_handler(ctx: &Context) {}
1481/// ```
1482///
1483/// The macro accepts a string literal specifying the expected referer value and should be
1484/// applied to async functions that accept a `&Context` parameter.
1485#[proc_macro_attribute]
1486pub fn referer(attr: TokenStream, item: TokenStream) -> TokenStream {
1487 referer_macro(attr, item, Position::Prologue)
1488}
1489
1490/// Reject requests that have a specific referer header.
1491///
1492/// This attribute macro ensures the decorated function only executes when the incoming request
1493/// does not have a referer header that matches the specified value. Requests with the matching referer header will be filtered out.
1494///
1495/// # Usage
1496///
1497/// ```rust
1498/// use hyperlane::*;
1499/// use hyperlane_macros::*;
1500///
1501/// #[route("/reject_referer")]
1502/// struct RejectReferer;
1503///
1504/// impl ServerHook for RejectReferer {
1505/// async fn new(_ctx: &Context) -> Self {
1506/// Self
1507/// }
1508///
1509/// #[prologue_macros(
1510/// reject_referer("http://localhost"),
1511/// response_body("referer filter string literal")
1512/// )]
1513/// async fn handle(self, ctx: &Context) {}
1514/// }
1515///
1516/// impl RejectReferer {
1517/// #[reject_referer("http://localhost")]
1518/// async fn reject_referer_with_ref_self(&self, ctx: &Context) {}
1519/// }
1520///
1521/// #[reject_referer("http://localhost")]
1522/// async fn standalone_reject_referer_handler(ctx: &Context) {}
1523/// ```
1524///
1525/// The macro accepts a string literal specifying the referer value to filter out and should be
1526/// applied to async functions that accept a `&Context` parameter.
1527#[proc_macro_attribute]
1528pub fn reject_referer(attr: TokenStream, item: TokenStream) -> TokenStream {
1529 reject_referer_macro(attr, item, Position::Prologue)
1530}
1531
1532/// Executes multiple specified functions before the main handler function.
1533///
1534/// This attribute macro configures multiple pre-execution hooks that run before the main function logic.
1535/// The specified hook functions will be called in the order provided, followed by the main function execution.
1536///
1537/// # Usage
1538///
1539/// ```rust
1540/// use hyperlane::*;
1541/// use hyperlane_macros::*;
1542///
1543/// struct PrologueHooks;
1544///
1545/// impl ServerHook for PrologueHooks {
1546/// async fn new(_ctx: &Context) -> Self {
1547/// Self
1548/// }
1549///
1550/// #[get]
1551/// #[http]
1552/// async fn handle(self, _ctx: &Context) {}
1553/// }
1554///
1555/// async fn prologue_hooks_fn(ctx: Context) {
1556/// let hook = PrologueHooks::new(&ctx).await;
1557/// hook.handle(&ctx).await;
1558/// }
1559///
1560/// #[route("/hook")]
1561/// struct Hook;
1562///
1563/// impl ServerHook for Hook {
1564/// async fn new(_ctx: &Context) -> Self {
1565/// Self
1566/// }
1567///
1568/// #[prologue_hooks(prologue_hooks_fn)]
1569/// #[response_body("Testing hook macro")]
1570/// async fn handle(self, ctx: &Context) {}
1571/// }
1572/// ```
1573///
1574/// The macro accepts a comma-separated list of function names as parameters. All hook functions
1575/// and the main function must accept a `Context` parameter. Avoid combining this macro with other
1576/// macros on the same function to prevent macro expansion conflicts.
1577#[proc_macro_attribute]
1578pub fn prologue_hooks(attr: TokenStream, item: TokenStream) -> TokenStream {
1579 prologue_hooks_macro(attr, item, Position::Prologue)
1580}
1581
1582/// Executes multiple specified functions after the main handler function.
1583///
1584/// This attribute macro configures multiple post-execution hooks that run after the main function logic.
1585/// The main function will execute first, followed by the specified hook functions in the order provided.
1586///
1587/// # Usage
1588///
1589/// ```rust
1590/// use hyperlane::*;
1591/// use hyperlane_macros::*;
1592///
1593/// struct EpilogueHooks;
1594///
1595/// impl ServerHook for EpilogueHooks {
1596/// async fn new(_ctx: &Context) -> Self {
1597/// Self
1598/// }
1599///
1600/// #[response_status_code(200)]
1601/// async fn handle(self, ctx: &Context) {}
1602/// }
1603///
1604/// async fn epilogue_hooks_fn(ctx: Context) {
1605/// let hook = EpilogueHooks::new(&ctx).await;
1606/// hook.handle(&ctx).await;
1607/// }
1608///
1609/// #[route("/hook")]
1610/// struct Hook;
1611///
1612/// impl ServerHook for Hook {
1613/// async fn new(_ctx: &Context) -> Self {
1614/// Self
1615/// }
1616///
1617/// #[epilogue_hooks(epilogue_hooks_fn)]
1618/// #[response_body("Testing hook macro")]
1619/// async fn handle(self, ctx: &Context) {}
1620/// }
1621/// ```
1622///
1623/// The macro accepts a comma-separated list of function names as parameters. All hook functions
1624/// and the main function must accept a `Context` parameter. Avoid combining this macro with other
1625/// macros on the same function to prevent macro expansion conflicts.
1626#[proc_macro_attribute]
1627pub fn epilogue_hooks(attr: TokenStream, item: TokenStream) -> TokenStream {
1628 epilogue_hooks_macro(attr, item, Position::Epilogue)
1629}
1630
1631/// Extracts the raw request body into a specified variable.
1632///
1633/// This attribute macro extracts the raw request body content into a variable
1634/// with the fixed type `RequestBody`. The body content is not parsed or deserialized.
1635///
1636/// # Usage
1637///
1638/// ```rust
1639/// use hyperlane::*;
1640/// use hyperlane_macros::*;
1641///
1642/// #[route("/request_body")]
1643/// struct RequestBodyRoute;
1644///
1645/// impl ServerHook for RequestBodyRoute {
1646/// async fn new(_ctx: &Context) -> Self {
1647/// Self
1648/// }
1649///
1650/// #[response_body(&format!("raw body: {raw_body:?}"))]
1651/// #[request_body(raw_body)]
1652/// async fn handle(self, ctx: &Context) {}
1653/// }
1654///
1655/// impl RequestBodyRoute {
1656/// #[request_body(raw_body)]
1657/// async fn request_body_with_ref_self(&self, ctx: &Context) {}
1658/// }
1659///
1660/// #[request_body(raw_body)]
1661/// async fn standalone_request_body_handler(ctx: &Context) {}
1662/// ```
1663///
1664/// # Multi-Parameter Usage
1665///
1666/// ```rust
1667/// use hyperlane::*;
1668/// use hyperlane_macros::*;
1669///
1670/// #[route("/multi_body")]
1671/// struct MultiBody;
1672///
1673/// impl ServerHook for MultiBody {
1674/// async fn new(_ctx: &Context) -> Self {
1675/// Self
1676/// }
1677///
1678/// #[response_body(&format!("bodies: {body1:?}, {body2:?}"))]
1679/// #[request_body(body1, body2)]
1680/// async fn handle(self, ctx: &Context) {}
1681/// }
1682/// ```
1683///
1684/// The macro accepts one or more variable names separated by commas.
1685/// Each variable will be available in the function scope as a `RequestBody` type.
1686#[proc_macro_attribute]
1687pub fn request_body(attr: TokenStream, item: TokenStream) -> TokenStream {
1688 request_body_macro(attr, item, Position::Prologue)
1689}
1690
1691/// Parses the request body as JSON into a specified variable and type.
1692///
1693/// This attribute macro extracts and deserializes the request body content as JSON into a variable
1694/// with the specified type. The body content is parsed as JSON using serde.
1695///
1696/// # Usage
1697///
1698/// ```rust
1699/// use hyperlane::*;
1700/// use hyperlane_macros::*;
1701/// use serde::{Deserialize, Serialize};
1702///
1703/// #[derive(Debug, Serialize, Deserialize, Clone)]
1704/// struct TestData {
1705/// name: String,
1706/// age: u32,
1707/// }
1708///
1709/// #[route("/request_body_json")]
1710/// struct RequestBodyJson;
1711///
1712/// impl ServerHook for RequestBodyJson {
1713/// async fn new(_ctx: &Context) -> Self {
1714/// Self
1715/// }
1716///
1717/// #[response_body(&format!("request data: {request_data_result:?}"))]
1718/// #[request_body_json(request_data_result: TestData)]
1719/// async fn handle(self, ctx: &Context) {}
1720/// }
1721///
1722/// impl RequestBodyJson {
1723/// #[request_body_json(request_data_result: TestData)]
1724/// async fn request_body_json_with_ref_self(&self, ctx: &Context) {}
1725/// }
1726///
1727/// #[request_body_json(request_data_result: TestData)]
1728/// async fn standalone_request_body_json_handler(ctx: &Context) {}
1729/// ```
1730///
1731/// # Multi-Parameter Usage
1732///
1733/// ```rust
1734/// use hyperlane::*;
1735/// use hyperlane_macros::*;
1736/// use serde::{Deserialize, Serialize};
1737///
1738/// #[derive(Debug, Serialize, Deserialize, Clone)]
1739/// struct User {
1740/// name: String,
1741/// }
1742///
1743/// #[derive(Debug, Serialize, Deserialize, Clone)]
1744/// struct Config {
1745/// debug: bool,
1746/// }
1747///
1748/// #[route("/multi_json")]
1749/// struct MultiJson;
1750///
1751/// impl ServerHook for MultiJson {
1752/// async fn new(_ctx: &Context) -> Self {
1753/// Self
1754/// }
1755///
1756/// #[response_body(&format!("user: {user:?}, config: {config:?}"))]
1757/// #[request_body_json(user: User, config: Config)]
1758/// async fn handle(self, ctx: &Context) {}
1759/// }
1760/// ```
1761///
1762/// The macro accepts one or more `variable_name: Type` pairs separated by commas.
1763/// Each variable will be available in the function scope as a `Result<Type, JsonError>`.
1764#[proc_macro_attribute]
1765pub fn request_body_json(attr: TokenStream, item: TokenStream) -> TokenStream {
1766 request_body_json_macro(attr, item, Position::Prologue)
1767}
1768
1769/// Extracts a specific attribute value into a variable.
1770///
1771/// This attribute macro retrieves a specific attribute by key and makes it available
1772/// as a typed variable from the request context.
1773///
1774/// # Usage
1775///
1776/// ```rust
1777/// use hyperlane::*;
1778/// use hyperlane_macros::*;
1779/// use serde::{Deserialize, Serialize};
1780///
1781/// const TEST_ATTRIBUTE_KEY: &str = "test_attribute_key";
1782///
1783/// #[derive(Debug, Serialize, Deserialize, Clone)]
1784/// struct TestData {
1785/// name: String,
1786/// age: u32,
1787/// }
1788///
1789/// #[route("/attribute")]
1790/// struct Attribute;
1791///
1792/// impl ServerHook for Attribute {
1793/// async fn new(_ctx: &Context) -> Self {
1794/// Self
1795/// }
1796///
1797/// #[response_body(&format!("request attribute: {request_attribute_option:?}"))]
1798/// #[attribute(TEST_ATTRIBUTE_KEY => request_attribute_option: TestData)]
1799/// async fn handle(self, ctx: &Context) {}
1800/// }
1801///
1802/// impl Attribute {
1803/// #[attribute(TEST_ATTRIBUTE_KEY => request_attribute_option: TestData)]
1804/// async fn attribute_with_ref_self(&self, ctx: &Context) {}
1805/// }
1806///
1807/// #[attribute(TEST_ATTRIBUTE_KEY => request_attribute_option: TestData)]
1808/// async fn standalone_attribute_handler(ctx: &Context) {}
1809/// ```
1810///
1811/// The macro accepts a key-to-variable mapping in the format `key => variable_name: Type`.
1812/// The variable will be available as an `Option<Type>` in the function scope.
1813///
1814/// # Multi-Parameter Usage
1815///
1816/// ```rust
1817/// use hyperlane::*;
1818/// use hyperlane_macros::*;
1819///
1820/// #[route("/multi_attr")]
1821/// struct MultiAttr;
1822///
1823/// impl ServerHook for MultiAttr {
1824/// async fn new(_ctx: &Context) -> Self {
1825/// Self
1826/// }
1827///
1828/// #[response_body(&format!("attrs: {attr1:?}, {attr2:?}"))]
1829/// #[attribute("key1" => attr1: String, "key2" => attr2: i32)]
1830/// async fn handle(self, ctx: &Context) {}
1831/// }
1832/// ```
1833///
1834/// The macro accepts multiple `key => variable_name: Type` tuples separated by commas.
1835#[proc_macro_attribute]
1836pub fn attribute(attr: TokenStream, item: TokenStream) -> TokenStream {
1837 attribute_macro(attr, item, Position::Prologue)
1838}
1839
1840/// Extracts all attributes into a HashMap variable.
1841///
1842/// This attribute macro retrieves all available attributes from the request context
1843/// and makes them available as a HashMap for comprehensive attribute access.
1844///
1845/// # Usage
1846///
1847/// ```rust
1848/// use hyperlane::*;
1849/// use hyperlane_macros::*;
1850///
1851/// #[route("/attributes")]
1852/// struct Attributes;
1853///
1854/// impl ServerHook for Attributes {
1855/// async fn new(_ctx: &Context) -> Self {
1856/// Self
1857/// }
1858///
1859/// #[response_body(&format!("request attributes: {request_attributes:?}"))]
1860/// #[attributes(request_attributes)]
1861/// async fn handle(self, ctx: &Context) {}
1862/// }
1863///
1864/// impl Attributes {
1865/// #[attributes(request_attributes)]
1866/// async fn attributes_with_ref_self(&self, ctx: &Context) {}
1867/// }
1868///
1869/// #[attributes(request_attributes)]
1870/// async fn standalone_attributes_handler(ctx: &Context) {}
1871/// ```
1872///
1873/// The macro accepts a variable name that will contain a HashMap of all attributes.
1874/// The variable will be available as a HashMap in the function scope.
1875///
1876/// # Multi-Parameter Usage
1877///
1878/// ```rust
1879/// use hyperlane::*;
1880/// use hyperlane_macros::*;
1881///
1882/// #[route("/multi_attrs")]
1883/// struct MultiAttrs;
1884///
1885/// impl ServerHook for MultiAttrs {
1886/// async fn new(_ctx: &Context) -> Self {
1887/// Self
1888/// }
1889///
1890/// #[response_body(&format!("attrs1: {attrs1:?}, attrs2: {attrs2:?}"))]
1891/// #[attributes(attrs1, attrs2)]
1892/// async fn handle(self, ctx: &Context) {}
1893/// }
1894/// ```
1895///
1896/// The macro accepts multiple variable names separated by commas.
1897#[proc_macro_attribute]
1898pub fn attributes(attr: TokenStream, item: TokenStream) -> TokenStream {
1899 attributes_macro(attr, item, Position::Prologue)
1900}
1901
1902/// Extracts a specific route parameter into a variable.
1903///
1904/// This attribute macro retrieves a specific route parameter by key and makes it
1905/// available as a variable. Route parameters are extracted from the URL path segments.
1906///
1907/// # Usage
1908///
1909/// ```rust
1910/// use hyperlane::*;
1911/// use hyperlane_macros::*;
1912///
1913/// #[route("/route_param/:test")]
1914/// struct RouteParam;
1915///
1916/// impl ServerHook for RouteParam {
1917/// async fn new(_ctx: &Context) -> Self {
1918/// Self
1919/// }
1920///
1921/// #[response_body(&format!("route param: {request_route_param:?}"))]
1922/// #[route_param("test" => request_route_param)]
1923/// async fn handle(self, ctx: &Context) {}
1924/// }
1925///
1926/// impl RouteParam {
1927/// #[route_param("test" => request_route_param)]
1928/// async fn route_param_with_ref_self(&self, ctx: &Context) {}
1929/// }
1930///
1931/// #[route_param("test" => request_route_param)]
1932/// async fn standalone_route_param_handler(ctx: &Context) {}
1933/// ```
1934///
1935/// The macro accepts a key-to-variable mapping in the format `"key" => variable_name`.
1936/// The variable will be available as an `Option<String>` in the function scope.
1937///
1938/// # Multi-Parameter Usage
1939///
1940/// ```rust
1941/// use hyperlane::*;
1942/// use hyperlane_macros::*;
1943///
1944/// #[route("/multi_param/:id/:name")]
1945/// struct MultiParam;
1946///
1947/// impl ServerHook for MultiParam {
1948/// async fn new(_ctx: &Context) -> Self {
1949/// Self
1950/// }
1951///
1952/// #[response_body(&format!("id: {id:?}, name: {name:?}"))]
1953/// #[route_param("id" => id, "name" => name)]
1954/// async fn handle(self, ctx: &Context) {}
1955/// }
1956/// ```
1957///
1958/// The macro accepts multiple `"key" => variable_name` pairs separated by commas.
1959#[proc_macro_attribute]
1960pub fn route_param(attr: TokenStream, item: TokenStream) -> TokenStream {
1961 route_param_macro(attr, item, Position::Prologue)
1962}
1963
1964/// Extracts all route parameters into a collection variable.
1965///
1966/// This attribute macro retrieves all available route parameters from the URL path
1967/// and makes them available as a collection for comprehensive route parameter access.
1968///
1969/// # Usage
1970///
1971/// ```rust
1972/// use hyperlane::*;
1973/// use hyperlane_macros::*;
1974///
1975/// #[route("/route_params/:test")]
1976/// struct RouteParams;
1977///
1978/// impl ServerHook for RouteParams {
1979/// async fn new(_ctx: &Context) -> Self {
1980/// Self
1981/// }
1982///
1983/// #[response_body(&format!("request route params: {request_route_params:?}"))]
1984/// #[route_params(request_route_params)]
1985/// async fn handle(self, ctx: &Context) {}
1986/// }
1987///
1988/// impl RouteParams {
1989/// #[route_params(request_route_params)]
1990/// async fn route_params_with_ref_self(&self, ctx: &Context) {}
1991/// }
1992///
1993/// #[route_params(request_route_params)]
1994/// async fn standalone_route_params_handler(ctx: &Context) {}
1995/// ```
1996///
1997/// The macro accepts a variable name that will contain all route parameters.
1998/// The variable will be available as a collection in the function scope.
1999///
2000/// # Multi-Parameter Usage
2001///
2002/// ```rust
2003/// use hyperlane::*;
2004/// use hyperlane_macros::*;
2005///
2006/// #[route("/multi_params/:id")]
2007/// struct MultiParams;
2008///
2009/// impl ServerHook for MultiParams {
2010/// async fn new(_ctx: &Context) -> Self {
2011/// Self
2012/// }
2013///
2014/// #[response_body(&format!("params1: {params1:?}, params2: {params2:?}"))]
2015/// #[route_params(params1, params2)]
2016/// async fn handle(self, ctx: &Context) {}
2017/// }
2018/// ```
2019///
2020/// The macro accepts multiple variable names separated by commas.
2021#[proc_macro_attribute]
2022pub fn route_params(attr: TokenStream, item: TokenStream) -> TokenStream {
2023 route_params_macro(attr, item, Position::Prologue)
2024}
2025
2026/// Extracts a specific request query parameter into a variable.
2027///
2028/// This attribute macro retrieves a specific request query parameter by key and makes it
2029/// available as a variable. Query parameters are extracted from the URL request query string.
2030///
2031/// # Usage
2032///
2033/// ```rust
2034/// use hyperlane::*;
2035/// use hyperlane_macros::*;
2036///
2037/// #[route("/request_query")]
2038/// struct RequestQuery;
2039///
2040/// impl ServerHook for RequestQuery {
2041/// async fn new(_ctx: &Context) -> Self {
2042/// Self
2043/// }
2044///
2045/// #[prologue_macros(
2046/// request_query("test" => request_query_option),
2047/// response_body(&format!("request query: {request_query_option:?}")),
2048/// send
2049/// )]
2050/// async fn handle(self, ctx: &Context) {}
2051/// }
2052///
2053/// impl RequestQuery {
2054/// #[request_query("test" => request_query_option)]
2055/// async fn request_query_with_ref_self(&self, ctx: &Context) {}
2056/// }
2057///
2058/// #[request_query("test" => request_query_option)]
2059/// async fn standalone_request_query_handler(ctx: &Context) {}
2060/// ```
2061///
2062/// The macro accepts a key-to-variable mapping in the format `"key" => variable_name`.
2063/// The variable will be available as an `Option<String>` in the function scope.
2064///
2065/// Supports multiple parameters: `#[request_query("k1" => v1, "k2" => v2)]`
2066#[proc_macro_attribute]
2067pub fn request_query(attr: TokenStream, item: TokenStream) -> TokenStream {
2068 request_query_macro(attr, item, Position::Prologue)
2069}
2070
2071/// Extracts all request query parameters into a collection variable.
2072///
2073/// This attribute macro retrieves all available request query parameters from the URL request query string
2074/// and makes them available as a collection for comprehensive request query parameter access.
2075///
2076/// # Usage
2077///
2078/// ```rust
2079/// use hyperlane::*;
2080/// use hyperlane_macros::*;
2081///
2082/// #[route("/request_querys")]
2083/// struct RequestQuerys;
2084///
2085/// impl ServerHook for RequestQuerys {
2086/// async fn new(_ctx: &Context) -> Self {
2087/// Self
2088/// }
2089///
2090/// #[prologue_macros(
2091/// request_querys(request_querys),
2092/// response_body(&format!("request querys: {request_querys:?}")),
2093/// send
2094/// )]
2095/// async fn handle(self, ctx: &Context) {}
2096/// }
2097///
2098/// impl RequestQuerys {
2099/// #[request_querys(request_querys)]
2100/// async fn request_querys_with_ref_self(&self, ctx: &Context) {}
2101/// }
2102///
2103/// #[request_querys(request_querys)]
2104/// async fn standalone_request_querys_handler(ctx: &Context) {}
2105/// ```
2106///
2107/// The macro accepts a variable name that will contain all request query parameters.
2108/// The variable will be available as a collection in the function scope.
2109///
2110/// Supports multiple parameters: `#[request_querys(querys1, querys2)]`
2111#[proc_macro_attribute]
2112pub fn request_querys(attr: TokenStream, item: TokenStream) -> TokenStream {
2113 request_querys_macro(attr, item, Position::Prologue)
2114}
2115
2116/// Extracts a specific HTTP request header into a variable.
2117///
2118/// This attribute macro retrieves a specific HTTP request header by name and makes it
2119/// available as a variable. Header values are extracted from the request request headers collection.
2120///
2121/// # Usage
2122///
2123/// ```rust
2124/// use hyperlane::*;
2125/// use hyperlane_macros::*;
2126///
2127/// #[route("/request_header")]
2128/// struct RequestHeader;
2129///
2130/// impl ServerHook for RequestHeader {
2131/// async fn new(_ctx: &Context) -> Self {
2132/// Self
2133/// }
2134///
2135/// #[prologue_macros(
2136/// request_header(HOST => request_header_option),
2137/// response_body(&format!("request header: {request_header_option:?}")),
2138/// send
2139/// )]
2140/// async fn handle(self, ctx: &Context) {}
2141/// }
2142///
2143/// impl RequestHeader {
2144/// #[request_header(HOST => request_header_option)]
2145/// async fn request_header_with_ref_self(&self, ctx: &Context) {}
2146/// }
2147///
2148/// #[request_header(HOST => request_header_option)]
2149/// async fn standalone_request_header_handler(ctx: &Context) {}
2150/// ```
2151///
2152/// The macro accepts a request header name-to-variable mapping in the format `HEADER_NAME => variable_name`
2153/// or `"Header-Name" => variable_name`. The variable will be available as an `Option<String>`.
2154#[proc_macro_attribute]
2155pub fn request_header(attr: TokenStream, item: TokenStream) -> TokenStream {
2156 request_header_macro(attr, item, Position::Prologue)
2157}
2158
2159/// Extracts all HTTP request headers into a collection variable.
2160///
2161/// This attribute macro retrieves all available HTTP request headers from the request
2162/// and makes them available as a collection for comprehensive request header access.
2163///
2164/// # Usage
2165///
2166/// ```rust
2167/// use hyperlane::*;
2168/// use hyperlane_macros::*;
2169///
2170/// #[route("/request_headers")]
2171/// struct RequestHeaders;
2172///
2173/// impl ServerHook for RequestHeaders {
2174/// async fn new(_ctx: &Context) -> Self {
2175/// Self
2176/// }
2177///
2178/// #[prologue_macros(
2179/// request_headers(request_headers),
2180/// response_body(&format!("request headers: {request_headers:?}")),
2181/// send
2182/// )]
2183/// async fn handle(self, ctx: &Context) {}
2184/// }
2185///
2186/// impl RequestHeaders {
2187/// #[request_headers(request_headers)]
2188/// async fn request_headers_with_ref_self(&self, ctx: &Context) {}
2189/// }
2190///
2191/// #[request_headers(request_headers)]
2192/// async fn standalone_request_headers_handler(ctx: &Context) {}
2193/// ```
2194///
2195/// The macro accepts a variable name that will contain all HTTP request headers.
2196/// The variable will be available as a collection in the function scope.
2197#[proc_macro_attribute]
2198pub fn request_headers(attr: TokenStream, item: TokenStream) -> TokenStream {
2199 request_headers_macro(attr, item, Position::Prologue)
2200}
2201
2202/// Extracts a specific cookie value or all cookies into a variable.
2203///
2204/// This attribute macro supports two syntaxes:
2205/// 1. `cookie(key => variable_name)` - Extract a specific cookie value by key
2206/// 2. `cookie(variable_name)` - Extract all cookies as a raw string
2207///
2208/// # Usage
2209///
2210/// ```rust
2211/// use hyperlane::*;
2212/// use hyperlane_macros::*;
2213///
2214/// #[route("/cookie")]
2215/// struct Cookie;
2216///
2217/// impl ServerHook for Cookie {
2218/// async fn new(_ctx: &Context) -> Self {
2219/// Self
2220/// }
2221///
2222/// #[response_body(&format!("Session cookie: {session_cookie1_option:?}, {session_cookie2_option:?}"))]
2223/// #[request_cookie("test1" => session_cookie1_option, "test2" => session_cookie2_option)]
2224/// async fn handle(self, ctx: &Context) {}
2225/// }
2226///
2227/// impl Cookie {
2228/// #[response_body(&format!("Session cookie: {session_cookie1_option:?}, {session_cookie2_option:?}"))]
2229/// #[request_cookie("test1" => session_cookie1_option, "test2" => session_cookie2_option)]
2230/// async fn request_cookie_with_ref_self(&self, ctx: &Context) {}
2231/// }
2232///
2233/// #[response_body(&format!("Session cookie: {session_cookie1_option:?}, {session_cookie2_option:?}"))]
2234/// #[request_cookie("test1" => session_cookie1_option, "test2" => session_cookie2_option)]
2235/// async fn standalone_request_cookie_handler(ctx: &Context) {}
2236/// ```
2237///
2238/// For specific cookie extraction, the variable will be available as `Option<String>`.
2239/// For all cookies extraction, the variable will be available as `String`.
2240#[proc_macro_attribute]
2241pub fn request_cookie(attr: TokenStream, item: TokenStream) -> TokenStream {
2242 request_cookie_macro(attr, item, Position::Prologue)
2243}
2244
2245/// Extracts all cookies as a raw string into a variable.
2246///
2247/// This attribute macro retrieves the entire Cookie header from the request and makes it
2248/// available as a String variable. If no Cookie header is present, an empty string is used.
2249///
2250/// # Usage
2251///
2252/// ```rust
2253/// use hyperlane::*;
2254/// use hyperlane_macros::*;
2255///
2256/// #[route("/cookies")]
2257/// struct Cookies;
2258///
2259/// impl ServerHook for Cookies {
2260/// async fn new(_ctx: &Context) -> Self {
2261/// Self
2262/// }
2263///
2264/// #[response_body(&format!("All cookies: {cookie_value:?}"))]
2265/// #[request_cookies(cookie_value)]
2266/// async fn handle(self, ctx: &Context) {}
2267/// }
2268///
2269/// impl Cookies {
2270/// #[request_cookies(cookie_value)]
2271/// async fn request_cookies_with_ref_self(&self, ctx: &Context) {}
2272/// }
2273///
2274/// #[request_cookies(cookie_value)]
2275/// async fn standalone_request_cookies_handler(ctx: &Context) {}
2276/// ```
2277///
2278/// The macro accepts a variable name that will contain the Cookie header value.
2279/// The variable will be available as a String in the function scope.
2280#[proc_macro_attribute]
2281pub fn request_cookies(attr: TokenStream, item: TokenStream) -> TokenStream {
2282 request_cookies_macro(attr, item, Position::Prologue)
2283}
2284
2285/// Extracts the HTTP request version into a variable.
2286///
2287/// This attribute macro retrieves the HTTP version from the request and makes it
2288/// available as a variable. The version represents the HTTP protocol version used.
2289///
2290/// # Usage
2291///
2292/// ```rust
2293/// use hyperlane::*;
2294/// use hyperlane_macros::*;
2295///
2296/// #[route("/request_version")]
2297/// struct RequestVersionTest;
2298///
2299/// impl ServerHook for RequestVersionTest {
2300/// async fn new(_ctx: &Context) -> Self {
2301/// Self
2302/// }
2303///
2304/// #[response_body(&format!("HTTP Version: {http_version}"))]
2305/// #[request_version(http_version)]
2306/// async fn handle(self, ctx: &Context) {}
2307/// }
2308///
2309/// impl RequestVersionTest {
2310/// #[request_version(http_version)]
2311/// async fn request_version_with_ref_self(&self, ctx: &Context) {}
2312/// }
2313///
2314/// #[request_version(http_version)]
2315/// async fn standalone_request_version_handler(ctx: &Context) {}
2316/// ```
2317///
2318/// The macro accepts a variable name that will contain the HTTP request version.
2319/// The variable will be available as a RequestVersion type in the function scope.
2320#[proc_macro_attribute]
2321pub fn request_version(attr: TokenStream, item: TokenStream) -> TokenStream {
2322 request_version_macro(attr, item, Position::Prologue)
2323}
2324
2325/// Extracts the HTTP request path into a variable.
2326///
2327/// This attribute macro retrieves the request path from the HTTP request and makes it
2328/// available as a variable. The path represents the URL path portion of the request.
2329///
2330/// # Usage
2331///
2332/// ```rust
2333/// use hyperlane::*;
2334/// use hyperlane_macros::*;
2335///
2336/// #[route("/request_path")]
2337/// struct RequestPathTest;
2338///
2339/// impl ServerHook for RequestPathTest {
2340/// async fn new(_ctx: &Context) -> Self {
2341/// Self
2342/// }
2343///
2344/// #[response_body(&format!("Request Path: {request_path}"))]
2345/// #[request_path(request_path)]
2346/// async fn handle(self, ctx: &Context) {}
2347/// }
2348///
2349/// impl RequestPathTest {
2350/// #[request_path(request_path)]
2351/// async fn request_path_with_ref_self(&self, ctx: &Context) {}
2352/// }
2353///
2354/// #[request_path(request_path)]
2355/// async fn standalone_request_path_handler(ctx: &Context) {}
2356/// ```
2357///
2358/// The macro accepts a variable name that will contain the HTTP request path.
2359/// The variable will be available as a RequestPath type in the function scope.
2360#[proc_macro_attribute]
2361pub fn request_path(attr: TokenStream, item: TokenStream) -> TokenStream {
2362 request_path_macro(attr, item, Position::Prologue)
2363}
2364
2365/// Creates a new instance of a specified type with a given variable name.
2366///
2367/// This attribute macro generates an instance initialization at the beginning of the function.
2368///
2369/// # Usage
2370///
2371/// ```rust,no_run
2372/// use hyperlane::*;
2373/// use hyperlane_macros::*;
2374///
2375/// #[hyperlane(server: Server)]
2376/// #[hyperlane(config: ServerConfig)]
2377/// #[tokio::main]
2378/// async fn main() {
2379/// config.disable_nodelay().await;
2380/// server.config(config).await;
2381/// let server_hook: ServerControlHook = server.run().await.unwrap_or_default();
2382/// server_hook.wait().await;
2383/// }
2384/// ```
2385///
2386/// The macro accepts a `variable_name: Type` pair.
2387/// The variable will be available as an instance of the specified type in the function scope.
2388#[proc_macro_attribute]
2389pub fn hyperlane(attr: TokenStream, item: TokenStream) -> TokenStream {
2390 hyperlane_macro(attr, item)
2391}
2392
2393/// Registers a function as a route handler.
2394///
2395/// This attribute macro registers the decorated function as a route handler for a given path.
2396/// This macro requires the `#[hyperlane(server: Server)]` macro to be used to define the server instance.
2397///
2398/// # Usage
2399///
2400/// ```rust
2401/// use hyperlane::*;
2402/// use hyperlane_macros::*;
2403///
2404/// #[route("/response")]
2405/// struct Response;
2406///
2407/// impl ServerHook for Response {
2408/// async fn new(_ctx: &Context) -> Self {
2409/// Self
2410/// }
2411///
2412/// #[response_body("response")]
2413/// async fn handle(self, ctx: &Context) {}
2414/// }
2415/// ```
2416///
2417/// # Parameters
2418///
2419/// - `path`: String literal defining the route path
2420///
2421/// # Dependencies
2422///
2423/// This macro depends on the `#[hyperlane(server: Server)]` macro to define the server instance.
2424#[proc_macro_attribute]
2425pub fn route(attr: TokenStream, item: TokenStream) -> TokenStream {
2426 route_macro(attr, item)
2427}
2428
2429/// Registers a function as a request middleware.
2430///
2431/// This attribute macro registers the decorated function to be executed as a middleware
2432/// for incoming requests. This macro requires the `#[hyperlane(server: Server)]` macro to be used to define the server instance.
2433///
2434/// # Note
2435///
2436/// If an order parameter is not specified, the hook will have a higher priority than hooks with a specified order.
2437///
2438/// # Usage
2439///
2440/// ```rust
2441/// use hyperlane::*;
2442/// use hyperlane_macros::*;
2443///
2444/// #[request_middleware]
2445/// struct RequestMiddleware;
2446///
2447/// impl ServerHook for RequestMiddleware {
2448/// async fn new(_ctx: &Context) -> Self {
2449/// Self
2450/// }
2451///
2452/// #[epilogue_macros(
2453/// response_status_code(200),
2454/// response_version(HttpVersion::HTTP1_1),
2455/// response_header(SERVER => HYPERLANE)
2456/// )]
2457/// async fn handle(self, ctx: &Context) {}
2458/// }
2459/// ```
2460///
2461/// # Dependencies
2462///
2463/// This macro depends on the `#[hyperlane(server: Server)]` macro to define the server instance.
2464#[proc_macro_attribute]
2465pub fn request_middleware(attr: TokenStream, item: TokenStream) -> TokenStream {
2466 request_middleware_macro(attr, item)
2467}
2468
2469/// Registers a function as a response middleware.
2470///
2471/// This attribute macro registers the decorated function to be executed as a middleware
2472/// for outgoing responses. This macro requires the `#[hyperlane(server: Server)]` macro to be used to define the server instance.
2473///
2474/// # Note
2475///
2476/// If an order parameter is not specified, the hook will have a higher priority than hooks with a specified order.
2477///
2478/// # Usage
2479///
2480/// ```rust
2481/// use hyperlane::*;
2482/// use hyperlane_macros::*;
2483///
2484/// #[response_middleware]
2485/// struct ResponseMiddleware1;
2486///
2487/// impl ServerHook for ResponseMiddleware1 {
2488/// async fn new(_ctx: &Context) -> Self {
2489/// Self
2490/// }
2491///
2492/// async fn handle(self, ctx: &Context) {}
2493/// }
2494/// ```
2495///
2496/// # Dependencies
2497///
2498/// This macro depends on the `#[hyperlane(server: Server)]` macro to define the server instance.
2499#[proc_macro_attribute]
2500pub fn response_middleware(attr: TokenStream, item: TokenStream) -> TokenStream {
2501 response_middleware_macro(attr, item)
2502}
2503
2504/// Registers a function as a panic hook.
2505///
2506/// This attribute macro registers the decorated function to handle panics that occur
2507/// during request processing. This macro requires the `#[hyperlane(server: Server)]` macro to be used to define the server instance.
2508///
2509/// # Note
2510///
2511/// If an order parameter is not specified, the hook will have a higher priority than hooks with a specified order.
2512///
2513/// # Usage
2514///
2515/// ```rust
2516/// use hyperlane::*;
2517/// use hyperlane_macros::*;
2518///
2519/// #[panic_hook]
2520/// #[panic_hook(1)]
2521/// #[panic_hook("2")]
2522/// struct PanicHook;
2523///
2524/// impl ServerHook for PanicHook {
2525/// async fn new(_ctx: &Context) -> Self {
2526/// Self
2527/// }
2528///
2529/// #[epilogue_macros(response_body("panic_hook"), send)]
2530/// async fn handle(self, ctx: &Context) {}
2531/// }
2532/// ```
2533///
2534/// # Dependencies
2535///
2536/// This macro depends on the `#[hyperlane(server: Server)]` macro to define the server instance.
2537#[proc_macro_attribute]
2538pub fn panic_hook(attr: TokenStream, item: TokenStream) -> TokenStream {
2539 panic_hook_macro(attr, item)
2540}
2541
2542/// Injects a list of macros before the decorated function.
2543///
2544/// The macros are applied in head-insertion order, meaning the first macro in the list
2545/// is the outermost macro.
2546///
2547/// # Usage
2548///
2549/// ```rust
2550/// use hyperlane::*;
2551/// use hyperlane_macros::*;
2552///
2553/// #[route("/post")]
2554/// struct Post;
2555///
2556/// impl ServerHook for Post {
2557/// async fn new(_ctx: &Context) -> Self {
2558/// Self
2559/// }
2560///
2561/// #[prologue_macros(post, response_body("post"), send)]
2562/// async fn handle(self, ctx: &Context) {}
2563/// }
2564/// ```
2565#[proc_macro_attribute]
2566pub fn prologue_macros(attr: TokenStream, item: TokenStream) -> TokenStream {
2567 prologue_macros_macro(attr, item)
2568}
2569
2570/// Injects a list of macros after the decorated function.
2571///
2572/// The macros are applied in tail-insertion order, meaning the last macro in the list
2573/// is the outermost macro.
2574///
2575/// # Usage
2576///
2577/// ```rust
2578/// use hyperlane::*;
2579/// use hyperlane_macros::*;
2580///
2581/// #[response_middleware(2)]
2582/// struct ResponseMiddleware2;
2583///
2584/// impl ServerHook for ResponseMiddleware2 {
2585/// async fn new(_ctx: &Context) -> Self {
2586/// Self
2587/// }
2588///
2589/// #[epilogue_macros(send, flush)]
2590/// async fn handle(self, ctx: &Context) {}
2591/// }
2592/// ```
2593#[proc_macro_attribute]
2594pub fn epilogue_macros(attr: TokenStream, item: TokenStream) -> TokenStream {
2595 epilogue_macros_macro(attr, item)
2596}
2597
2598/// Sends only the response body with data after function execution.
2599///
2600/// This attribute macro ensures that only the response body is automatically sent
2601/// to the client after the function completes, handling request headers separately,
2602/// with the specified data.
2603///
2604/// # Usage
2605///
2606/// ```rust
2607/// use hyperlane::*;
2608/// use hyperlane_macros::*;
2609///
2610/// #[route("/send_body_with_data")]
2611/// struct SendBodyWithData;
2612///
2613/// impl ServerHook for SendBodyWithData {
2614/// async fn new(_ctx: &Context) -> Self {
2615/// Self
2616/// }
2617///
2618/// #[epilogue_macros(send_body_with_data("Response body content"))]
2619/// async fn handle(self, ctx: &Context) {}
2620/// }
2621/// ```
2622///
2623/// The macro accepts data to send and should be applied to async functions
2624/// that accept a `&Context` parameter.
2625#[proc_macro_attribute]
2626pub fn send_body_with_data(attr: TokenStream, item: TokenStream) -> TokenStream {
2627 send_body_with_data_macro(attr, item, Position::Epilogue)
2628}
2629
2630/// Wraps function body with WebSocket stream processing.
2631///
2632/// This attribute macro generates code that wraps the function body with a check to see if
2633/// data can be read from a WebSocket stream. The function body is only executed
2634/// if data is successfully read from the stream.
2635///
2636/// This attribute macro generates code that wraps the function body with a check to see if
2637/// data can be read from a WebSocket stream. The function body is only executed
2638/// if data is successfully read from the stream.
2639///
2640/// # Arguments
2641///
2642/// - `TokenStream`: The buffer to read from the WebSocket stream.
2643/// - `TokenStream`: The function item to be modified
2644///
2645/// # Returns
2646///
2647/// Returns a TokenStream containing the modified function with WebSocket stream processing logic.
2648///
2649/// # Examples
2650///
2651/// Using no parameters (default buffer size):
2652/// ```rust
2653/// use hyperlane::*;
2654/// use hyperlane_macros::*;
2655///
2656/// #[route("/ws1")]
2657/// struct Websocket1;
2658///
2659/// impl ServerHook for Websocket1 {
2660/// async fn new(_ctx: &Context) -> Self {
2661/// Self
2662/// }
2663///
2664/// #[ws]
2665/// #[ws_from_stream]
2666/// async fn handle(self, ctx: &Context) {
2667/// let body: RequestBody = ctx.get_request_body().await;
2668/// let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(&body);
2669/// ctx.send_body_list_with_data(&body_list).await.unwrap();
2670/// }
2671/// }
2672/// ```
2673///
2674/// Using only buffer size:
2675/// ```rust
2676/// use hyperlane::*;
2677/// use hyperlane_macros::*;
2678///
2679/// #[route("/ws5")]
2680/// struct Websocket5;
2681///
2682/// impl ServerHook for Websocket5 {
2683/// async fn new(_ctx: &Context) -> Self {
2684/// Self
2685/// }
2686///
2687/// #[ws]
2688/// #[ws_from_stream(1024)]
2689/// async fn handle(self, ctx: &Context) {
2690/// let body: RequestBody = ctx.get_request_body().await;
2691/// let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(&body);
2692/// ctx.send_body_list_with_data(&body_list).await.unwrap();
2693/// }
2694/// }
2695/// ```
2696///
2697/// Using variable name to store request data:
2698/// ```rust
2699/// use hyperlane::*;
2700/// use hyperlane_macros::*;
2701///
2702/// #[route("/ws2")]
2703/// struct Websocket2;
2704///
2705/// impl ServerHook for Websocket2 {
2706/// async fn new(_ctx: &Context) -> Self {
2707/// Self
2708/// }
2709///
2710/// #[ws]
2711/// #[ws_from_stream(request)]
2712/// async fn handle(self, ctx: &Context) {
2713/// let body: &RequestBody = &request.get_body();
2714/// let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(body);
2715/// ctx.send_body_list_with_data(&body_list).await.unwrap();
2716/// }
2717/// }
2718/// ```
2719///
2720/// Using buffer size and variable name:
2721/// ```rust
2722/// use hyperlane::*;
2723/// use hyperlane_macros::*;
2724///
2725/// #[route("/ws3")]
2726/// struct Websocket3;
2727///
2728/// impl ServerHook for Websocket3 {
2729/// async fn new(_ctx: &Context) -> Self {
2730/// Self
2731/// }
2732///
2733/// #[ws]
2734/// #[ws_from_stream(1024, request)]
2735/// async fn handle(self, ctx: &Context) {
2736/// let body: &RequestBody = request.get_body();
2737/// let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(&body);
2738/// ctx.send_body_list_with_data(&body_list).await.unwrap();
2739/// }
2740/// }
2741/// ```
2742///
2743/// Using variable name and buffer size (reversed order):
2744/// ```rust
2745/// use hyperlane::*;
2746/// use hyperlane_macros::*;
2747///
2748/// #[route("/ws4")]
2749/// struct Websocket4;
2750///
2751/// impl ServerHook for Websocket4 {
2752/// async fn new(_ctx: &Context) -> Self {
2753/// Self
2754/// }
2755///
2756/// #[ws]
2757/// #[ws_from_stream(request, 1024)]
2758/// async fn handle(self, ctx: &Context) {
2759/// let body: &RequestBody = request.get_body();
2760/// let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(&body);
2761/// ctx.send_body_list_with_data(&body_list).await.unwrap();
2762/// }
2763/// }
2764///
2765/// impl Websocket4 {
2766/// #[ws_from_stream(request)]
2767/// async fn ws_from_stream_with_ref_self(&self, ctx: &Context) {}
2768/// }
2769///
2770/// #[ws_from_stream]
2771/// async fn standalone_ws_from_stream_handler(ctx: &Context) {}
2772/// ```
2773#[proc_macro_attribute]
2774pub fn ws_from_stream(attr: TokenStream, item: TokenStream) -> TokenStream {
2775 ws_from_stream_macro(attr, item)
2776}
2777
2778/// Wraps function body with HTTP stream processing.
2779///
2780/// This attribute macro generates code that wraps the function body with a check to see if
2781/// data can be read from an HTTP stream. The function body is only executed
2782/// if data is successfully read from the stream.
2783///
2784/// This attribute macro generates code that wraps the function body with a check to see if
2785/// data can be read from an HTTP stream. The function body is only executed
2786/// if data is successfully read from the stream.
2787///
2788/// # Arguments
2789///
2790/// - `TokenStream`: The buffer to read from the HTTP stream.
2791/// - `TokenStream`: The function item to be modified
2792///
2793/// # Returns
2794///
2795/// Returns a TokenStream containing the modified function with HTTP stream processing logic.
2796///
2797/// # Examples
2798///
2799/// Using with epilogue_macros:
2800/// ```rust
2801/// use hyperlane::*;
2802/// use hyperlane_macros::*;
2803///
2804/// #[route("/request_query")]
2805/// struct RequestQuery;
2806///
2807/// impl ServerHook for RequestQuery {
2808/// async fn new(_ctx: &Context) -> Self {
2809/// Self
2810/// }
2811///
2812/// #[epilogue_macros(
2813/// request_query("test" => request_query_option),
2814/// response_body(&format!("request query: {request_query_option:?}")),
2815/// send,
2816/// http_from_stream(1024)
2817/// )]
2818/// async fn handle(self, ctx: &Context) {}
2819/// }
2820/// ```
2821///
2822/// Using with variable name:
2823/// ```rust
2824/// use hyperlane::*;
2825/// use hyperlane_macros::*;
2826///
2827/// #[route("/http_from_stream")]
2828/// struct HttpFromStreamTest;
2829///
2830/// impl ServerHook for HttpFromStreamTest {
2831/// async fn new(_ctx: &Context) -> Self {
2832/// Self
2833/// }
2834///
2835/// #[epilogue_macros(
2836/// http_from_stream(_request)
2837/// )]
2838/// async fn handle(self, ctx: &Context) {}
2839/// }
2840///
2841/// impl HttpFromStreamTest {
2842/// #[http_from_stream(_request)]
2843/// async fn http_from_stream_with_ref_self(&self, ctx: &Context) {}
2844/// }
2845///
2846/// #[http_from_stream]
2847/// async fn standalone_http_from_stream_handler(ctx: &Context) {}
2848/// ```
2849#[proc_macro_attribute]
2850pub fn http_from_stream(attr: TokenStream, item: TokenStream) -> TokenStream {
2851 http_from_stream_macro(attr, item)
2852}