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_status_code")]
557/// struct ResponseStatusCode;
558///
559/// impl ServerHook for ResponseStatusCode {
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 ResponseStatusCode {
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_reason")]
598/// struct ResponseReason;
599///
600/// impl ServerHook for ResponseReason {
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 ResponseReason {
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_header")]
641/// struct ResponseHeader;
642///
643/// impl ServerHook for ResponseHeader {
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 ResponseHeader {
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_body")]
697/// struct ResponseBody;
698///
699/// impl ServerHook for ResponseBody {
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 ResponseBody {
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("/clear_response_headers")]
735/// struct ClearResponseHeaders;
736///
737/// impl ServerHook for ClearResponseHeaders {
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("clear_response_headers")
746/// )]
747/// async fn handle(self, ctx: &Context) {}
748/// }
749///
750/// impl ClearResponseHeaders {
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 with panic on parsing failure.
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/// If the request body does not exist or JSON parsing fails, the function will panic with an error message.
1696///
1697/// # Usage
1698///
1699/// ```rust
1700/// use hyperlane::*;
1701/// use hyperlane_macros::*;
1702/// use serde::{Deserialize, Serialize};
1703///
1704/// #[derive(Debug, Serialize, Deserialize, Clone)]
1705/// struct TestData {
1706/// name: String,
1707/// age: u32,
1708/// }
1709///
1710/// #[route("/request_body_json_result")]
1711/// struct RequestBodyJson;
1712///
1713/// impl ServerHook for RequestBodyJson {
1714/// async fn new(_ctx: &Context) -> Self {
1715/// Self
1716/// }
1717///
1718/// #[response_body(&format!("request data: {request_data_result:?}"))]
1719/// #[request_body_json_result(request_data_result: TestData)]
1720/// async fn handle(self, ctx: &Context) {}
1721/// }
1722///
1723/// impl RequestBodyJson {
1724/// #[request_body_json_result(request_data_result: TestData)]
1725/// async fn request_body_json_with_ref_self(&self, ctx: &Context) {}
1726/// }
1727///
1728/// #[request_body_json_result(request_data_result: TestData)]
1729/// async fn standalone_request_body_json_handler(ctx: &Context) {}
1730/// ```
1731///
1732/// # Multi-Parameter Usage
1733///
1734/// ```rust
1735/// use hyperlane::*;
1736/// use hyperlane_macros::*;
1737/// use serde::{Deserialize, Serialize};
1738///
1739/// #[derive(Debug, Serialize, Deserialize, Clone)]
1740/// struct User {
1741/// name: String,
1742/// }
1743///
1744/// #[derive(Debug, Serialize, Deserialize, Clone)]
1745/// struct Config {
1746/// debug: bool,
1747/// }
1748///
1749/// #[route("/request_body_json_result")]
1750/// struct TestData;
1751///
1752/// impl ServerHook for TestData {
1753/// async fn new(_ctx: &Context) -> Self {
1754/// Self
1755/// }
1756///
1757/// #[response_body(&format!("user: {user:?}, config: {config:?}"))]
1758/// #[request_body_json_result(user: User, config: Config)]
1759/// async fn handle(self, ctx: &Context) {}
1760/// }
1761/// ```
1762///
1763/// The macro accepts one or more `variable_name: Type` pairs separated by commas.
1764/// Each variable will be available in the function scope as a `ResultJsonError<Type>`.
1765#[proc_macro_attribute]
1766pub fn request_body_json_result(attr: TokenStream, item: TokenStream) -> TokenStream {
1767 request_body_json_result_macro(attr, item, Position::Prologue)
1768}
1769
1770/// Parses the request body as JSON into a specified variable and type with panic on parsing failure.
1771///
1772/// This attribute macro extracts and deserializes the request body content as JSON into a variable
1773/// with the specified type. The body content is parsed as JSON using serde.
1774/// If the request body does not exist or JSON parsing fails, the function will panic with an error message.
1775///
1776/// # Usage
1777///
1778/// ```rust
1779/// use hyperlane::*;
1780/// use hyperlane_macros::*;
1781/// use serde::{Deserialize, Serialize};
1782///
1783/// #[derive(Debug, Serialize, Deserialize, Clone)]
1784/// struct TestData {
1785/// name: String,
1786/// age: u32,
1787/// }
1788///
1789/// #[route("/request_body_json")]
1790/// struct RequestBodyJson;
1791///
1792/// impl ServerHook for RequestBodyJson {
1793/// async fn new(_ctx: &Context) -> Self {
1794/// Self
1795/// }
1796///
1797/// #[response_body(&format!("request data: {request_data_result:?}"))]
1798/// #[request_body_json(request_data_result: TestData)]
1799/// async fn handle(self, ctx: &Context) {}
1800/// }
1801///
1802/// impl RequestBodyJson {
1803/// #[request_body_json(request_data_result: TestData)]
1804/// async fn request_body_json_with_ref_self(&self, ctx: &Context) {}
1805/// }
1806///
1807/// #[request_body_json(request_data_result: TestData)]
1808/// async fn standalone_request_body_json_handler(ctx: &Context) {}
1809/// ```
1810///
1811/// # Multi-Parameter Usage
1812///
1813/// ```rust
1814/// use hyperlane::*;
1815/// use hyperlane_macros::*;
1816/// use serde::{Deserialize, Serialize};
1817///
1818/// #[derive(Debug, Serialize, Deserialize, Clone)]
1819/// struct User {
1820/// name: String,
1821/// }
1822///
1823/// #[derive(Debug, Serialize, Deserialize, Clone)]
1824/// struct Config {
1825/// debug: bool,
1826/// }
1827///
1828/// #[route("/request_body_json")]
1829/// struct TestData;
1830///
1831/// impl ServerHook for TestData {
1832/// async fn new(_ctx: &Context) -> Self {
1833/// Self
1834/// }
1835///
1836/// #[response_body(&format!("user: {user:?}, config: {config:?}"))]
1837/// #[request_body_json(user: User, config: Config)]
1838/// async fn handle(self, ctx: &Context) {}
1839/// }
1840/// ```
1841///
1842/// The macro accepts one or more `variable_name: Type` pairs separated by commas.
1843/// Each variable will be available in the function scope as a `ResultJsonError<Type>`.
1844///
1845/// # Panics
1846///
1847/// This macro will panic if the request body does not exist or JSON parsing fails.
1848#[proc_macro_attribute]
1849pub fn request_body_json(attr: TokenStream, item: TokenStream) -> TokenStream {
1850 request_body_json_macro(attr, item, Position::Prologue)
1851}
1852
1853/// Extracts a specific attribute value into a variable wrapped in Option type.
1854///
1855/// This attribute macro retrieves a specific attribute by key and makes it available
1856/// as a typed Option variable from the request context. The extracted value is wrapped
1857/// in an Option type to safely handle cases where the attribute may not exist.
1858///
1859/// # Usage
1860///
1861/// ```rust
1862/// use hyperlane::*;
1863/// use hyperlane_macros::*;
1864/// use serde::{Deserialize, Serialize};
1865///
1866/// const TEST_ATTRIBUTE_KEY: &str = "test_attribute_key";
1867///
1868/// #[derive(Debug, Serialize, Deserialize, Clone)]
1869/// struct TestData {
1870/// name: String,
1871/// age: u32,
1872/// }
1873///
1874/// #[route("/attribute_option")]
1875/// struct Attribute;
1876///
1877/// impl ServerHook for Attribute {
1878/// async fn new(_ctx: &Context) -> Self {
1879/// Self
1880/// }
1881///
1882/// #[response_body(&format!("request attribute: {request_attribute_option:?}"))]
1883/// #[attribute_option(TEST_ATTRIBUTE_KEY => request_attribute_option: TestData)]
1884/// async fn handle(self, ctx: &Context) {}
1885/// }
1886///
1887/// impl Attribute {
1888/// #[attribute_option(TEST_ATTRIBUTE_KEY => request_attribute_option: TestData)]
1889/// async fn attribute_with_ref_self(&self, ctx: &Context) {}
1890/// }
1891///
1892/// #[attribute_option(TEST_ATTRIBUTE_KEY => request_attribute_option: TestData)]
1893/// async fn standalone_attribute_handler(ctx: &Context) {}
1894/// ```
1895///
1896/// The macro accepts a key-to-variable mapping in the format `key => variable_name: Type`.
1897/// The variable will be available as an `Option<Type>` in the function scope.
1898///
1899/// # Multi-Parameter Usage
1900///
1901/// ```rust
1902/// use hyperlane::*;
1903/// use hyperlane_macros::*;
1904///
1905/// #[route("/attribute_option")]
1906/// struct MultiAttr;
1907///
1908/// impl ServerHook for MultiAttr {
1909/// async fn new(_ctx: &Context) -> Self {
1910/// Self
1911/// }
1912///
1913/// #[response_body(&format!("attrs: {attr1:?}, {attr2:?}"))]
1914/// #[attribute_option("key1" => attr1: String, "key2" => attr2: i32)]
1915/// async fn handle(self, ctx: &Context) {}
1916/// }
1917/// ```
1918///
1919/// The macro accepts multiple `key => variable_name: Type` tuples separated by commas.
1920#[proc_macro_attribute]
1921pub fn attribute_option(attr: TokenStream, item: TokenStream) -> TokenStream {
1922 attribute_option_macro(attr, item, Position::Prologue)
1923}
1924
1925/// Extracts a specific attribute value into a variable with panic on missing value.
1926///
1927/// This attribute macro retrieves a specific attribute by key and makes it available
1928/// as a typed variable from the request context. If the attribute does not exist,
1929/// the function will panic with an error message indicating the missing attribute.
1930///
1931/// # Usage
1932///
1933/// ```rust
1934/// use hyperlane::*;
1935/// use hyperlane_macros::*;
1936/// use serde::{Deserialize, Serialize};
1937///
1938/// const TEST_ATTRIBUTE_KEY: &str = "test_attribute_key";
1939///
1940/// #[derive(Debug, Serialize, Deserialize, Clone)]
1941/// struct TestData {
1942/// name: String,
1943/// age: u32,
1944/// }
1945///
1946/// #[route("/attribute")]
1947/// struct Attribute;
1948///
1949/// impl ServerHook for Attribute {
1950/// async fn new(_ctx: &Context) -> Self {
1951/// Self
1952/// }
1953///
1954/// #[response_body(&format!("request attribute: {request_attribute:?}"))]
1955/// #[attribute(TEST_ATTRIBUTE_KEY => request_attribute: TestData)]
1956/// async fn handle(self, ctx: &Context) {}
1957/// }
1958///
1959/// impl Attribute {
1960/// #[attribute(TEST_ATTRIBUTE_KEY => request_attribute: TestData)]
1961/// async fn attribute_with_ref_self(&self, ctx: &Context) {}
1962/// }
1963///
1964/// #[attribute(TEST_ATTRIBUTE_KEY => request_attribute: TestData)]
1965/// async fn standalone_attribute_handler(ctx: &Context) {}
1966/// ```
1967///
1968/// The macro accepts a key-to-variable mapping in the format `key => variable_name: Type`.
1969/// The variable will be available as an `Type` in the function scope.
1970///
1971/// # Multi-Parameter Usage
1972///
1973/// ```rust
1974/// use hyperlane::*;
1975/// use hyperlane_macros::*;
1976///
1977/// #[route("/attribute")]
1978/// struct MultiAttr;
1979///
1980/// impl ServerHook for MultiAttr {
1981/// async fn new(_ctx: &Context) -> Self {
1982/// Self
1983/// }
1984///
1985/// #[response_body(&format!("attrs: {attr1}, {attr2}"))]
1986/// #[attribute("key1" => attr1: String, "key2" => attr2: i32)]
1987/// async fn handle(self, ctx: &Context) {}
1988/// }
1989/// ```
1990///
1991/// The macro accepts multiple `key => variable_name: Type` tuples separated by commas.
1992///
1993/// # Panics
1994///
1995/// This macro will panic if the requested attribute does not exist in the request context.
1996#[proc_macro_attribute]
1997pub fn attribute(attr: TokenStream, item: TokenStream) -> TokenStream {
1998 attribute_macro(attr, item, Position::Prologue)
1999}
2000
2001/// Extracts all attributes into a ThreadSafeAttributeStore variable.
2002///
2003/// This attribute macro retrieves all available attributes from the request context
2004/// and makes them available as a ThreadSafeAttributeStore for comprehensive attribute access.
2005///
2006/// # Usage
2007///
2008/// ```rust
2009/// use hyperlane::*;
2010/// use hyperlane_macros::*;
2011///
2012/// #[route("/attributes")]
2013/// struct Attributes;
2014///
2015/// impl ServerHook for Attributes {
2016/// async fn new(_ctx: &Context) -> Self {
2017/// Self
2018/// }
2019///
2020/// #[response_body(&format!("request attributes: {request_attributes:?}"))]
2021/// #[attributes(request_attributes)]
2022/// async fn handle(self, ctx: &Context) {}
2023/// }
2024///
2025/// impl Attributes {
2026/// #[attributes(request_attributes)]
2027/// async fn attributes_with_ref_self(&self, ctx: &Context) {}
2028/// }
2029///
2030/// #[attributes(request_attributes)]
2031/// async fn standalone_attributes_handler(ctx: &Context) {}
2032/// ```
2033///
2034/// The macro accepts a variable name that will contain a HashMap of all attributes.
2035/// The variable will be available as a HashMap in the function scope.
2036///
2037/// # Multi-Parameter Usage
2038///
2039/// ```rust
2040/// use hyperlane::*;
2041/// use hyperlane_macros::*;
2042///
2043/// #[route("/multi_attrs")]
2044/// struct MultiAttrs;
2045///
2046/// impl ServerHook for MultiAttrs {
2047/// async fn new(_ctx: &Context) -> Self {
2048/// Self
2049/// }
2050///
2051/// #[response_body(&format!("attrs1: {attrs1:?}, attrs2: {attrs2:?}"))]
2052/// #[attributes(attrs1, attrs2)]
2053/// async fn handle(self, ctx: &Context) {}
2054/// }
2055/// ```
2056///
2057/// The macro accepts multiple variable names separated by commas.
2058#[proc_macro_attribute]
2059pub fn attributes(attr: TokenStream, item: TokenStream) -> TokenStream {
2060 attributes_macro(attr, item, Position::Prologue)
2061}
2062
2063/// Extracts a specific route parameter into a variable wrapped in Option type.
2064///
2065/// This attribute macro retrieves a specific route parameter by key and makes it
2066/// available as an Option variable. Route parameters are extracted from the URL path segments
2067/// and wrapped in an Option type to safely handle cases where the parameter may not exist.
2068///
2069/// # Usage
2070///
2071/// ```rust
2072/// use hyperlane::*;
2073/// use hyperlane_macros::*;
2074///
2075/// #[route("/route_param_option/:test")]
2076/// struct RouteParam;
2077///
2078/// impl ServerHook for RouteParam {
2079/// async fn new(_ctx: &Context) -> Self {
2080/// Self
2081/// }
2082///
2083/// #[response_body(&format!("route param: {request_route_param:?}"))]
2084/// #[route_param_option("test" => request_route_param)]
2085/// async fn handle(self, ctx: &Context) {}
2086/// }
2087///
2088/// impl RouteParam {
2089/// #[route_param_option("test" => request_route_param)]
2090/// async fn route_param_with_ref_self(&self, ctx: &Context) {}
2091/// }
2092///
2093/// #[route_param_option("test" => request_route_param)]
2094/// async fn standalone_route_param_handler(ctx: &Context) {}
2095/// ```
2096///
2097/// The macro accepts a key-to-variable mapping in the format `"key" => variable_name`.
2098/// The variable will be available as an `OptionString` in the function scope.
2099///
2100/// # Multi-Parameter Usage
2101///
2102/// ```rust
2103/// use hyperlane::*;
2104/// use hyperlane_macros::*;
2105///
2106/// #[route("/multi_param/:id/:name")]
2107/// struct MultiParam;
2108///
2109/// impl ServerHook for MultiParam {
2110/// async fn new(_ctx: &Context) -> Self {
2111/// Self
2112/// }
2113///
2114/// #[response_body(&format!("id: {id:?}, name: {name:?}"))]
2115/// #[route_param_option("id" => id, "name" => name)]
2116/// async fn handle(self, ctx: &Context) {}
2117/// }
2118/// ```
2119///
2120/// The macro accepts multiple `"key" => variable_name` pairs separated by commas.
2121#[proc_macro_attribute]
2122pub fn route_param_option(attr: TokenStream, item: TokenStream) -> TokenStream {
2123 route_param_option_macro(attr, item, Position::Prologue)
2124}
2125
2126/// Extracts a specific route parameter into a variable with panic on missing value.
2127///
2128/// This attribute macro retrieves a specific route parameter by key and makes it
2129/// available as a variable. Route parameters are extracted from the URL path segments.
2130/// If the requested route parameter does not exist, the function will panic with an error message.
2131///
2132/// # Usage
2133///
2134/// ```rust
2135/// use hyperlane::*;
2136/// use hyperlane_macros::*;
2137///
2138/// #[route("/route_param/:test")]
2139/// struct RouteParam;
2140///
2141/// impl ServerHook for RouteParam {
2142/// async fn new(_ctx: &Context) -> Self {
2143/// Self
2144/// }
2145///
2146/// #[response_body(&format!("route param: {request_route_param:?}"))]
2147/// #[route_param("test" => request_route_param)]
2148/// async fn handle(self, ctx: &Context) {}
2149/// }
2150///
2151/// impl RouteParam {
2152/// #[route_param("test" => request_route_param)]
2153/// async fn route_param_with_ref_self(&self, ctx: &Context) {}
2154/// }
2155///
2156/// #[route_param("test" => request_route_param)]
2157/// async fn standalone_route_param_handler(ctx: &Context) {}
2158/// ```
2159///
2160/// The macro accepts a key-to-variable mapping in the format `"key" => variable_name`.
2161/// The variable will be available as an `String` in the function scope.
2162///
2163///
2164/// # Multi-Parameter Usage
2165///
2166/// ```rust
2167/// use hyperlane::*;
2168/// use hyperlane_macros::*;
2169///
2170/// #[route("/multi_param/:id/:name")]
2171/// struct MultiParam;
2172///
2173/// impl ServerHook for MultiParam {
2174/// async fn new(_ctx: &Context) -> Self {
2175/// Self
2176/// }
2177///
2178/// #[response_body(&format!("id: {id:?}, name: {name:?}"))]
2179/// #[route_param("id" => id, "name" => name)]
2180/// async fn handle(self, ctx: &Context) {}
2181/// }
2182/// ```
2183///
2184/// The macro accepts multiple `"key" => variable_name` pairs separated by commas.
2185///
2186/// # Panics
2187///
2188/// This macro will panic if the requested route parameter does not exist in the URL path.
2189#[proc_macro_attribute]
2190pub fn route_param(attr: TokenStream, item: TokenStream) -> TokenStream {
2191 route_param_macro(attr, item, Position::Prologue)
2192}
2193
2194/// Extracts all route parameters into a collection variable.
2195///
2196/// This attribute macro retrieves all available route parameters from the URL path
2197/// and makes them available as a collection for comprehensive route parameter access.
2198///
2199/// # Usage
2200///
2201/// ```rust
2202/// use hyperlane::*;
2203/// use hyperlane_macros::*;
2204///
2205/// #[route("/route_params/:test")]
2206/// struct RouteParams;
2207///
2208/// impl ServerHook for RouteParams {
2209/// async fn new(_ctx: &Context) -> Self {
2210/// Self
2211/// }
2212///
2213/// #[response_body(&format!("request route params: {request_route_params:?}"))]
2214/// #[route_params(request_route_params)]
2215/// async fn handle(self, ctx: &Context) {}
2216/// }
2217///
2218/// impl RouteParams {
2219/// #[route_params(request_route_params)]
2220/// async fn route_params_with_ref_self(&self, ctx: &Context) {}
2221/// }
2222///
2223/// #[route_params(request_route_params)]
2224/// async fn standalone_route_params_handler(ctx: &Context) {}
2225/// ```
2226///
2227/// The macro accepts a variable name that will contain all route parameters.
2228/// The variable will be available as a RouteParams type in the function scope.
2229///
2230/// # Multi-Parameter Usage
2231///
2232/// ```rust
2233/// use hyperlane::*;
2234/// use hyperlane_macros::*;
2235///
2236/// #[route("/multi_params/:id")]
2237/// struct MultiParams;
2238///
2239/// impl ServerHook for MultiParams {
2240/// async fn new(_ctx: &Context) -> Self {
2241/// Self
2242/// }
2243///
2244/// #[response_body(&format!("params1: {params1:?}, params2: {params2:?}"))]
2245/// #[route_params(params1, params2)]
2246/// async fn handle(self, ctx: &Context) {}
2247/// }
2248/// ```
2249///
2250/// The macro accepts multiple variable names separated by commas.
2251#[proc_macro_attribute]
2252pub fn route_params(attr: TokenStream, item: TokenStream) -> TokenStream {
2253 route_params_macro(attr, item, Position::Prologue)
2254}
2255
2256/// Extracts a specific request query parameter into a variable wrapped in Option type.
2257///
2258/// This attribute macro retrieves a specific request query parameter by key and makes it
2259/// available as an Option variable. Query parameters are extracted from the URL request query string
2260/// and wrapped in an Option type to safely handle cases where the parameter may not exist.
2261///
2262/// # Usage
2263///
2264/// ```rust
2265/// use hyperlane::*;
2266/// use hyperlane_macros::*;
2267///
2268/// #[route("/request_query_option")]
2269/// struct RequestQuery;
2270///
2271/// impl ServerHook for RequestQuery {
2272/// async fn new(_ctx: &Context) -> Self {
2273/// Self
2274/// }
2275///
2276/// #[prologue_macros(
2277/// request_query_option("test" => request_query_option),
2278/// response_body(&format!("request query: {request_query_option:?}")),
2279/// send
2280/// )]
2281/// async fn handle(self, ctx: &Context) {}
2282/// }
2283///
2284/// impl RequestQuery {
2285/// #[request_query_option("test" => request_query_option)]
2286/// async fn request_query_with_ref_self(&self, ctx: &Context) {}
2287/// }
2288///
2289/// #[request_query_option("test" => request_query_option)]
2290/// async fn standalone_request_query_handler(ctx: &Context) {}
2291/// ```
2292///
2293/// The macro accepts a key-to-variable mapping in the format `"key" => variable_name`.
2294/// The variable will be available as an `OptionRequestQuerysValue` in the function scope.
2295///
2296/// Supports multiple parameters: `#[request_query_option("k1" => v1, "k2" => v2)]`
2297#[proc_macro_attribute]
2298pub fn request_query_option(attr: TokenStream, item: TokenStream) -> TokenStream {
2299 request_query_option_macro(attr, item, Position::Prologue)
2300}
2301
2302/// Extracts a specific request query parameter into a variable with panic on missing value.
2303///
2304/// This attribute macro retrieves a specific request query parameter by key and makes it
2305/// available as a variable. Query parameters are extracted from the URL request query string.
2306/// If the requested query parameter does not exist, the function will panic with an error message.
2307///
2308/// # Usage
2309///
2310/// ```rust
2311/// use hyperlane::*;
2312/// use hyperlane_macros::*;
2313///
2314/// #[route("/request_query")]
2315/// struct RequestQuery;
2316///
2317/// impl ServerHook for RequestQuery {
2318/// async fn new(_ctx: &Context) -> Self {
2319/// Self
2320/// }
2321///
2322/// #[prologue_macros(
2323/// request_query("test" => request_query),
2324/// response_body(&format!("request query: {request_query}")),
2325/// send
2326/// )]
2327/// async fn handle(self, ctx: &Context) {}
2328/// }
2329///
2330/// impl RequestQuery {
2331/// #[request_query("test" => request_query)]
2332/// async fn request_query_with_ref_self(&self, ctx: &Context) {}
2333/// }
2334///
2335/// #[request_query("test" => request_query)]
2336/// async fn standalone_request_query_handler(ctx: &Context) {}
2337/// ```
2338///
2339/// The macro accepts a key-to-variable mapping in the format `"key" => variable_name`.
2340/// The variable will be available as an `RequestQuerysValue` in the function scope.
2341///
2342/// Supports multiple parameters: `#[request_query("k1" => v1, "k2" => v2)]`
2343///
2344/// # Panics
2345///
2346/// This macro will panic if the requested query parameter does not exist in the URL query string.
2347#[proc_macro_attribute]
2348pub fn request_query(attr: TokenStream, item: TokenStream) -> TokenStream {
2349 request_query_macro(attr, item, Position::Prologue)
2350}
2351
2352/// Extracts all request query parameters into a RequestQuerys variable.
2353///
2354/// This attribute macro retrieves all available request query parameters from the URL request query string
2355/// and makes them available as a RequestQuerys for comprehensive request query parameter access.
2356///
2357/// # Usage
2358///
2359/// ```rust
2360/// use hyperlane::*;
2361/// use hyperlane_macros::*;
2362///
2363/// #[route("/request_querys")]
2364/// struct RequestQuerys;
2365///
2366/// impl ServerHook for RequestQuerys {
2367/// async fn new(_ctx: &Context) -> Self {
2368/// Self
2369/// }
2370///
2371/// #[prologue_macros(
2372/// request_querys(request_querys),
2373/// response_body(&format!("request querys: {request_querys:?}")),
2374/// send
2375/// )]
2376/// async fn handle(self, ctx: &Context) {}
2377/// }
2378///
2379/// impl RequestQuerys {
2380/// #[request_querys(request_querys)]
2381/// async fn request_querys_with_ref_self(&self, ctx: &Context) {}
2382/// }
2383///
2384/// #[request_querys(request_querys)]
2385/// async fn standalone_request_querys_handler(ctx: &Context) {}
2386/// ```
2387///
2388/// The macro accepts a variable name that will contain all request query parameters.
2389/// The variable will be available as a collection in the function scope.
2390///
2391/// Supports multiple parameters: `#[request_querys(querys1, querys2)]`
2392#[proc_macro_attribute]
2393pub fn request_querys(attr: TokenStream, item: TokenStream) -> TokenStream {
2394 request_querys_macro(attr, item, Position::Prologue)
2395}
2396
2397/// Extracts a specific HTTP request header into a variable wrapped in Option type.
2398///
2399/// This attribute macro retrieves a specific HTTP request header by name and makes it
2400/// available as an Option variable. Header values are extracted from the request request headers collection
2401/// and wrapped in an Option type to safely handle cases where the header may not exist.
2402///
2403/// # Usage
2404///
2405/// ```rust
2406/// use hyperlane::*;
2407/// use hyperlane_macros::*;
2408///
2409/// #[route("/request_header_option")]
2410/// struct RequestHeader;
2411///
2412/// impl ServerHook for RequestHeader {
2413/// async fn new(_ctx: &Context) -> Self {
2414/// Self
2415/// }
2416///
2417/// #[prologue_macros(
2418/// request_header_option(HOST => request_header_option),
2419/// response_body(&format!("request header: {request_header_option:?}")),
2420/// send
2421/// )]
2422/// async fn handle(self, ctx: &Context) {}
2423/// }
2424///
2425/// impl RequestHeader {
2426/// #[request_header_option(HOST => request_header_option)]
2427/// async fn request_header_with_ref_self(&self, ctx: &Context) {}
2428/// }
2429///
2430/// #[request_header_option(HOST => request_header_option)]
2431/// async fn standalone_request_header_handler(ctx: &Context) {}
2432/// ```
2433///
2434/// The macro accepts a request header name-to-variable mapping in the format `HEADER_NAME => variable_name`
2435/// or `"Header-Name" => variable_name`. The variable will be available as an `OptionRequestHeadersValueItem`.
2436#[proc_macro_attribute]
2437pub fn request_header_option(attr: TokenStream, item: TokenStream) -> TokenStream {
2438 request_header_option_macro(attr, item, Position::Prologue)
2439}
2440
2441/// Extracts a specific HTTP request header into a variable with panic on missing value.
2442///
2443/// This attribute macro retrieves a specific HTTP request header by name and makes it
2444/// available as a variable. Header values are extracted from the request request headers collection.
2445/// If the requested header does not exist, the function will panic with an error message.
2446///
2447/// # Usage
2448///
2449/// ```rust
2450/// use hyperlane::*;
2451/// use hyperlane_macros::*;
2452///
2453/// #[route("/request_header")]
2454/// struct RequestHeader;
2455///
2456/// impl ServerHook for RequestHeader {
2457/// async fn new(_ctx: &Context) -> Self {
2458/// Self
2459/// }
2460///
2461/// #[prologue_macros(
2462/// request_header(HOST => request_header),
2463/// response_body(&format!("request header: {request_header}")),
2464/// send
2465/// )]
2466/// async fn handle(self, ctx: &Context) {}
2467/// }
2468///
2469/// impl RequestHeader {
2470/// #[request_header(HOST => request_header)]
2471/// async fn request_header_with_ref_self(&self, ctx: &Context) {}
2472/// }
2473///
2474/// #[request_header(HOST => request_header)]
2475/// async fn standalone_request_header_handler(ctx: &Context) {}
2476/// ```
2477///
2478/// The macro accepts a request header name-to-variable mapping in the format `HEADER_NAME => variable_name`
2479/// or `"Header-Name" => variable_name`. The variable will be available as an `RequestHeadersValueItem`.
2480///
2481/// # Panics
2482///
2483/// This macro will panic if the requested header does not exist in the HTTP request headers.
2484#[proc_macro_attribute]
2485pub fn request_header(attr: TokenStream, item: TokenStream) -> TokenStream {
2486 request_header_macro(attr, item, Position::Prologue)
2487}
2488
2489/// Extracts all HTTP request headers into a collection variable.
2490///
2491/// This attribute macro retrieves all available HTTP request headers from the request
2492/// and makes them available as a collection for comprehensive request header access.
2493///
2494/// # Usage
2495///
2496/// ```rust
2497/// use hyperlane::*;
2498/// use hyperlane_macros::*;
2499///
2500/// #[route("/request_headers")]
2501/// struct RequestHeaders;
2502///
2503/// impl ServerHook for RequestHeaders {
2504/// async fn new(_ctx: &Context) -> Self {
2505/// Self
2506/// }
2507///
2508/// #[prologue_macros(
2509/// request_headers(request_headers),
2510/// response_body(&format!("request headers: {request_headers:?}")),
2511/// send
2512/// )]
2513/// async fn handle(self, ctx: &Context) {}
2514/// }
2515///
2516/// impl RequestHeaders {
2517/// #[request_headers(request_headers)]
2518/// async fn request_headers_with_ref_self(&self, ctx: &Context) {}
2519/// }
2520///
2521/// #[request_headers(request_headers)]
2522/// async fn standalone_request_headers_handler(ctx: &Context) {}
2523/// ```
2524///
2525/// The macro accepts a variable name that will contain all HTTP request headers.
2526/// The variable will be available as a RequestHeaders type in the function scope.
2527#[proc_macro_attribute]
2528pub fn request_headers(attr: TokenStream, item: TokenStream) -> TokenStream {
2529 request_headers_macro(attr, item, Position::Prologue)
2530}
2531
2532/// Extracts a specific cookie value or all cookies into a variable wrapped in Option type.
2533///
2534/// This attribute macro supports two syntaxes:
2535/// 1. `cookie(key => variable_name)` - Extract a specific cookie value by key, wrapped in Option
2536/// 2. `cookie(variable_name)` - Extract all cookies as a raw string, wrapped in Option
2537///
2538/// # Usage
2539///
2540/// ```rust
2541/// use hyperlane::*;
2542/// use hyperlane_macros::*;
2543///
2544/// #[route("/cookie")]
2545/// struct Cookie;
2546///
2547/// impl ServerHook for Cookie {
2548/// async fn new(_ctx: &Context) -> Self {
2549/// Self
2550/// }
2551///
2552/// #[response_body(&format!("Session cookie: {session_cookie1_option:?}, {session_cookie2_option:?}"))]
2553/// #[request_cookie_option("test1" => session_cookie1_option, "test2" => session_cookie2_option)]
2554/// async fn handle(self, ctx: &Context) {}
2555/// }
2556///
2557/// impl Cookie {
2558/// #[response_body(&format!("Session cookie: {session_cookie1_option:?}, {session_cookie2_option:?}"))]
2559/// #[request_cookie_option("test1" => session_cookie1_option, "test2" => session_cookie2_option)]
2560/// async fn request_cookie_with_ref_self(&self, ctx: &Context) {}
2561/// }
2562///
2563/// #[response_body(&format!("Session cookie: {session_cookie1_option:?}, {session_cookie2_option:?}"))]
2564/// #[request_cookie_option("test1" => session_cookie1_option, "test2" => session_cookie2_option)]
2565/// async fn standalone_request_cookie_handler(ctx: &Context) {}
2566/// ```
2567///
2568/// For specific cookie extraction, the variable will be available as `Option<String>`.
2569/// For all cookies extraction, the variable will be available as `String`.
2570#[proc_macro_attribute]
2571pub fn request_cookie_option(attr: TokenStream, item: TokenStream) -> TokenStream {
2572 request_cookie_option_macro(attr, item, Position::Prologue)
2573}
2574
2575/// Extracts a specific cookie value or all cookies into a variable with panic on missing value.
2576///
2577/// This attribute macro supports two syntaxes:
2578/// 1. `cookie(key => variable_name)` - Extract a specific cookie value by key, panics if missing
2579/// 2. `cookie(variable_name)` - Extract all cookies as a raw string, panics if missing
2580///
2581/// # Usage
2582///
2583/// ```rust
2584/// use hyperlane::*;
2585/// use hyperlane_macros::*;
2586///
2587/// #[route("/cookie")]
2588/// struct Cookie;
2589///
2590/// impl ServerHook for Cookie {
2591/// async fn new(_ctx: &Context) -> Self {
2592/// Self
2593/// }
2594///
2595/// #[response_body(&format!("Session cookie: {session_cookie1}, {session_cookie2}"))]
2596/// #[request_cookie("test1" => session_cookie1, "test2" => session_cookie2)]
2597/// async fn handle(self, ctx: &Context) {}
2598/// }
2599///
2600/// impl Cookie {
2601/// #[response_body(&format!("Session cookie: {session_cookie1}, {session_cookie2}"))]
2602/// #[request_cookie("test1" => session_cookie1, "test2" => session_cookie2)]
2603/// async fn request_cookie_with_ref_self(&self, ctx: &Context) {}
2604/// }
2605///
2606/// #[response_body(&format!("Session cookie: {session_cookie1}, {session_cookie2}"))]
2607/// #[request_cookie("test1" => session_cookie1, "test2" => session_cookie2)]
2608/// async fn standalone_request_cookie_handler(ctx: &Context) {}
2609/// ```
2610///
2611/// For specific cookie extraction, the variable will be available as `String`.
2612/// For all cookies extraction, the variable will be available as `String`.
2613///
2614/// # Panics
2615///
2616/// This macro will panic if the requested cookie does not exist in the HTTP request headers.
2617#[proc_macro_attribute]
2618pub fn request_cookie(attr: TokenStream, item: TokenStream) -> TokenStream {
2619 request_cookie_macro(attr, item, Position::Prologue)
2620}
2621
2622/// Extracts all cookies as a raw string into a variable.
2623///
2624/// This attribute macro retrieves the entire Cookie header from the request and makes it
2625/// available as a String variable. If no Cookie header is present, an empty string is used.
2626///
2627/// # Usage
2628///
2629/// ```rust
2630/// use hyperlane::*;
2631/// use hyperlane_macros::*;
2632///
2633/// #[route("/cookies")]
2634/// struct Cookies;
2635///
2636/// impl ServerHook for Cookies {
2637/// async fn new(_ctx: &Context) -> Self {
2638/// Self
2639/// }
2640///
2641/// #[response_body(&format!("All cookies: {cookie_value:?}"))]
2642/// #[request_cookies(cookie_value)]
2643/// async fn handle(self, ctx: &Context) {}
2644/// }
2645///
2646/// impl Cookies {
2647/// #[request_cookies(cookie_value)]
2648/// async fn request_cookies_with_ref_self(&self, ctx: &Context) {}
2649/// }
2650///
2651/// #[request_cookies(cookie_value)]
2652/// async fn standalone_request_cookies_handler(ctx: &Context) {}
2653/// ```
2654///
2655/// The macro accepts a variable name that will contain all cookies.
2656/// The variable will be available as a Cookies type in the function scope.
2657#[proc_macro_attribute]
2658pub fn request_cookies(attr: TokenStream, item: TokenStream) -> TokenStream {
2659 request_cookies_macro(attr, item, Position::Prologue)
2660}
2661
2662/// Extracts the HTTP request version into a variable.
2663///
2664/// This attribute macro retrieves the HTTP version from the request and makes it
2665/// available as a variable. The version represents the HTTP protocol version used.
2666///
2667/// # Usage
2668///
2669/// ```rust
2670/// use hyperlane::*;
2671/// use hyperlane_macros::*;
2672///
2673/// #[route("/request_version")]
2674/// struct RequestVersionTest;
2675///
2676/// impl ServerHook for RequestVersionTest {
2677/// async fn new(_ctx: &Context) -> Self {
2678/// Self
2679/// }
2680///
2681/// #[response_body(&format!("HTTP Version: {http_version}"))]
2682/// #[request_version(http_version)]
2683/// async fn handle(self, ctx: &Context) {}
2684/// }
2685///
2686/// impl RequestVersionTest {
2687/// #[request_version(http_version)]
2688/// async fn request_version_with_ref_self(&self, ctx: &Context) {}
2689/// }
2690///
2691/// #[request_version(http_version)]
2692/// async fn standalone_request_version_handler(ctx: &Context) {}
2693/// ```
2694///
2695/// The macro accepts a variable name that will contain the HTTP request version.
2696/// The variable will be available as a RequestVersion type in the function scope.
2697#[proc_macro_attribute]
2698pub fn request_version(attr: TokenStream, item: TokenStream) -> TokenStream {
2699 request_version_macro(attr, item, Position::Prologue)
2700}
2701
2702/// Extracts the HTTP request path into a variable.
2703///
2704/// This attribute macro retrieves the request path from the HTTP request and makes it
2705/// available as a variable. The path represents the URL path portion of the request.
2706///
2707/// # Usage
2708///
2709/// ```rust
2710/// use hyperlane::*;
2711/// use hyperlane_macros::*;
2712///
2713/// #[route("/request_path")]
2714/// struct RequestPathTest;
2715///
2716/// impl ServerHook for RequestPathTest {
2717/// async fn new(_ctx: &Context) -> Self {
2718/// Self
2719/// }
2720///
2721/// #[response_body(&format!("Request Path: {request_path}"))]
2722/// #[request_path(request_path)]
2723/// async fn handle(self, ctx: &Context) {}
2724/// }
2725///
2726/// impl RequestPathTest {
2727/// #[request_path(request_path)]
2728/// async fn request_path_with_ref_self(&self, ctx: &Context) {}
2729/// }
2730///
2731/// #[request_path(request_path)]
2732/// async fn standalone_request_path_handler(ctx: &Context) {}
2733/// ```
2734///
2735/// The macro accepts a variable name that will contain the HTTP request path.
2736/// The variable will be available as a RequestPath type in the function scope.
2737#[proc_macro_attribute]
2738pub fn request_path(attr: TokenStream, item: TokenStream) -> TokenStream {
2739 request_path_macro(attr, item, Position::Prologue)
2740}
2741
2742/// Creates a new instance of a specified type with a given variable name.
2743///
2744/// This attribute macro generates an instance initialization at the beginning of the function.
2745///
2746/// # Usage
2747///
2748/// ```rust,no_run
2749/// use hyperlane::*;
2750/// use hyperlane_macros::*;
2751///
2752/// #[hyperlane(server: Server)]
2753/// #[hyperlane(config: ServerConfig)]
2754/// #[tokio::main]
2755/// async fn main() {
2756/// config.disable_nodelay().await;
2757/// server.config(config).await;
2758/// let server_hook: ServerControlHook = server.run().await.unwrap_or_default();
2759/// server_hook.wait().await;
2760/// }
2761/// ```
2762///
2763/// The macro accepts a `variable_name: Type` pair.
2764/// The variable will be available as an instance of the specified type in the function scope.
2765#[proc_macro_attribute]
2766pub fn hyperlane(attr: TokenStream, item: TokenStream) -> TokenStream {
2767 hyperlane_macro(attr, item)
2768}
2769
2770/// Registers a function as a route handler.
2771///
2772/// This attribute macro registers the decorated function as a route handler for a given path.
2773/// This macro requires the `#[hyperlane(server: Server)]` macro to be used to define the server instance.
2774///
2775/// # Usage
2776///
2777/// ```rust
2778/// use hyperlane::*;
2779/// use hyperlane_macros::*;
2780///
2781/// #[route("/response")]
2782/// struct Response;
2783///
2784/// impl ServerHook for Response {
2785/// async fn new(_ctx: &Context) -> Self {
2786/// Self
2787/// }
2788///
2789/// #[response_body("response")]
2790/// async fn handle(self, ctx: &Context) {}
2791/// }
2792/// ```
2793///
2794/// # Parameters
2795///
2796/// - `path`: String literal defining the route path
2797///
2798/// # Dependencies
2799///
2800/// This macro depends on the `#[hyperlane(server: Server)]` macro to define the server instance.
2801#[proc_macro_attribute]
2802pub fn route(attr: TokenStream, item: TokenStream) -> TokenStream {
2803 route_macro(attr, item)
2804}
2805
2806/// Registers a function as a request middleware.
2807///
2808/// This attribute macro registers the decorated function to be executed as a middleware
2809/// for incoming requests. This macro requires the `#[hyperlane(server: Server)]` macro to be used to define the server instance.
2810///
2811/// # Note
2812///
2813/// If an order parameter is not specified, the hook will have a higher priority than hooks with a specified order.
2814///
2815/// # Usage
2816///
2817/// ```rust
2818/// use hyperlane::*;
2819/// use hyperlane_macros::*;
2820///
2821/// #[request_middleware]
2822/// struct RequestMiddleware;
2823///
2824/// impl ServerHook for RequestMiddleware {
2825/// async fn new(_ctx: &Context) -> Self {
2826/// Self
2827/// }
2828///
2829/// #[epilogue_macros(
2830/// response_status_code(200),
2831/// response_version(HttpVersion::HTTP1_1),
2832/// response_header(SERVER => HYPERLANE)
2833/// )]
2834/// async fn handle(self, ctx: &Context) {}
2835/// }
2836/// ```
2837///
2838/// # Dependencies
2839///
2840/// This macro depends on the `#[hyperlane(server: Server)]` macro to define the server instance.
2841#[proc_macro_attribute]
2842pub fn request_middleware(attr: TokenStream, item: TokenStream) -> TokenStream {
2843 request_middleware_macro(attr, item)
2844}
2845
2846/// Registers a function as a response middleware.
2847///
2848/// This attribute macro registers the decorated function to be executed as a middleware
2849/// for outgoing responses. This macro requires the `#[hyperlane(server: Server)]` macro to be used to define the server instance.
2850///
2851/// # Note
2852///
2853/// If an order parameter is not specified, the hook will have a higher priority than hooks with a specified order.
2854///
2855/// # Usage
2856///
2857/// ```rust
2858/// use hyperlane::*;
2859/// use hyperlane_macros::*;
2860///
2861/// #[response_middleware]
2862/// struct ResponseMiddleware1;
2863///
2864/// impl ServerHook for ResponseMiddleware1 {
2865/// async fn new(_ctx: &Context) -> Self {
2866/// Self
2867/// }
2868///
2869/// async fn handle(self, ctx: &Context) {}
2870/// }
2871/// ```
2872///
2873/// # Dependencies
2874///
2875/// This macro depends on the `#[hyperlane(server: Server)]` macro to define the server instance.
2876#[proc_macro_attribute]
2877pub fn response_middleware(attr: TokenStream, item: TokenStream) -> TokenStream {
2878 response_middleware_macro(attr, item)
2879}
2880
2881/// Registers a function as a panic hook.
2882///
2883/// This attribute macro registers the decorated function to handle panics that occur
2884/// during request processing. This macro requires the `#[hyperlane(server: Server)]` macro to be used to define the server instance.
2885///
2886/// # Note
2887///
2888/// If an order parameter is not specified, the hook will have a higher priority than hooks with a specified order.
2889///
2890/// # Usage
2891///
2892/// ```rust
2893/// use hyperlane::*;
2894/// use hyperlane_macros::*;
2895///
2896/// #[panic_hook]
2897/// #[panic_hook(1)]
2898/// #[panic_hook("2")]
2899/// struct PanicHook;
2900///
2901/// impl ServerHook for PanicHook {
2902/// async fn new(_ctx: &Context) -> Self {
2903/// Self
2904/// }
2905///
2906/// #[epilogue_macros(response_body("panic_hook"), send)]
2907/// async fn handle(self, ctx: &Context) {}
2908/// }
2909/// ```
2910///
2911/// # Dependencies
2912///
2913/// This macro depends on the `#[hyperlane(server: Server)]` macro to define the server instance.
2914#[proc_macro_attribute]
2915pub fn panic_hook(attr: TokenStream, item: TokenStream) -> TokenStream {
2916 panic_hook_macro(attr, item)
2917}
2918
2919/// Injects a list of macros before the decorated function.
2920///
2921/// The macros are applied in head-insertion order, meaning the first macro in the list
2922/// is the outermost macro.
2923///
2924/// # Usage
2925///
2926/// ```rust
2927/// use hyperlane::*;
2928/// use hyperlane_macros::*;
2929///
2930/// #[route("/prologue_macros")]
2931/// struct PrologueMacros;
2932///
2933/// impl ServerHook for PrologueMacros {
2934/// async fn new(_ctx: &Context) -> Self {
2935/// Self
2936/// }
2937///
2938/// #[prologue_macros(post, response_body("prologue_macros"), send)]
2939/// async fn handle(self, ctx: &Context) {}
2940/// }
2941/// ```
2942#[proc_macro_attribute]
2943pub fn prologue_macros(attr: TokenStream, item: TokenStream) -> TokenStream {
2944 prologue_macros_macro(attr, item)
2945}
2946
2947/// Injects a list of macros after the decorated function.
2948///
2949/// The macros are applied in tail-insertion order, meaning the last macro in the list
2950/// is the outermost macro.
2951///
2952/// # Usage
2953///
2954/// ```rust
2955/// use hyperlane::*;
2956/// use hyperlane_macros::*;
2957///
2958/// #[response_middleware(2)]
2959/// struct ResponseMiddleware2;
2960///
2961/// impl ServerHook for ResponseMiddleware2 {
2962/// async fn new(_ctx: &Context) -> Self {
2963/// Self
2964/// }
2965///
2966/// #[epilogue_macros(send, flush)]
2967/// async fn handle(self, ctx: &Context) {}
2968/// }
2969/// ```
2970#[proc_macro_attribute]
2971pub fn epilogue_macros(attr: TokenStream, item: TokenStream) -> TokenStream {
2972 epilogue_macros_macro(attr, item)
2973}
2974
2975/// Sends only the response body with data after function execution.
2976///
2977/// This attribute macro ensures that only the response body is automatically sent
2978/// to the client after the function completes, handling request headers separately,
2979/// with the specified data.
2980///
2981/// # Usage
2982///
2983/// ```rust
2984/// use hyperlane::*;
2985/// use hyperlane_macros::*;
2986///
2987/// #[route("/send_body_with_data")]
2988/// struct SendBodyWithData;
2989///
2990/// impl ServerHook for SendBodyWithData {
2991/// async fn new(_ctx: &Context) -> Self {
2992/// Self
2993/// }
2994///
2995/// #[epilogue_macros(send_body_with_data("Response body content"))]
2996/// async fn handle(self, ctx: &Context) {}
2997/// }
2998/// ```
2999///
3000/// The macro accepts data to send and should be applied to async functions
3001/// that accept a `&Context` parameter.
3002#[proc_macro_attribute]
3003pub fn send_body_with_data(attr: TokenStream, item: TokenStream) -> TokenStream {
3004 send_body_with_data_macro(attr, item, Position::Epilogue)
3005}
3006
3007/// Wraps function body with WebSocket stream processing.
3008///
3009/// This attribute macro generates code that wraps the function body with a check to see if
3010/// data can be read from a WebSocket stream. The function body is only executed
3011/// if data is successfully read from the stream.
3012///
3013/// This attribute macro generates code that wraps the function body with a check to see if
3014/// data can be read from a WebSocket stream. The function body is only executed
3015/// if data is successfully read from the stream.
3016///
3017/// # Arguments
3018///
3019/// - `TokenStream`: The buffer to read from the WebSocket stream.
3020/// - `TokenStream`: The function item to be modified
3021///
3022/// # Returns
3023///
3024/// Returns a TokenStream containing the modified function with WebSocket stream processing logic.
3025///
3026/// # Examples
3027///
3028/// Using no parameters (default buffer size):
3029///
3030/// ```rust
3031/// use hyperlane::*;
3032/// use hyperlane_macros::*;
3033///
3034/// #[route("/ws")]
3035/// struct Websocket;
3036///
3037/// impl ServerHook for Websocket {
3038/// async fn new(_ctx: &Context) -> Self {
3039/// Self
3040/// }
3041///
3042/// #[ws]
3043/// #[ws_from_stream]
3044/// async fn handle(self, ctx: &Context) {
3045/// let body: RequestBody = ctx.get_request_body().await;
3046/// let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(&body);
3047/// ctx.send_body_list_with_data(&body_list).await.unwrap();
3048/// }
3049/// }
3050/// ```
3051///
3052/// Using only buffer size:
3053///
3054/// ```rust
3055/// use hyperlane::*;
3056/// use hyperlane_macros::*;
3057///
3058/// #[route("/ws")]
3059/// struct Websocket;
3060///
3061/// impl ServerHook for Websocket {
3062/// async fn new(_ctx: &Context) -> Self {
3063/// Self
3064/// }
3065///
3066/// #[ws]
3067/// #[ws_from_stream(1024)]
3068/// async fn handle(self, ctx: &Context) {
3069/// let body: RequestBody = ctx.get_request_body().await;
3070/// let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(&body);
3071/// ctx.send_body_list_with_data(&body_list).await.unwrap();
3072/// }
3073/// }
3074/// ```
3075///
3076/// Using variable name to store request data:
3077///
3078/// ```rust
3079/// use hyperlane::*;
3080/// use hyperlane_macros::*;
3081///
3082/// #[route("/ws")]
3083/// struct Websocket;
3084///
3085/// impl ServerHook for Websocket {
3086/// async fn new(_ctx: &Context) -> Self {
3087/// Self
3088/// }
3089///
3090/// #[ws]
3091/// #[ws_from_stream(request)]
3092/// async fn handle(self, ctx: &Context) {
3093/// let body: &RequestBody = &request.get_body();
3094/// let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(body);
3095/// ctx.send_body_list_with_data(&body_list).await.unwrap();
3096/// }
3097/// }
3098/// ```
3099///
3100/// Using buffer size and variable name:
3101///
3102/// ```rust
3103/// use hyperlane::*;
3104/// use hyperlane_macros::*;
3105///
3106/// #[route("/ws")]
3107/// struct Websocket;
3108///
3109/// impl ServerHook for Websocket {
3110/// async fn new(_ctx: &Context) -> Self {
3111/// Self
3112/// }
3113///
3114/// #[ws]
3115/// #[ws_from_stream(1024, request)]
3116/// async fn handle(self, ctx: &Context) {
3117/// let body: &RequestBody = request.get_body();
3118/// let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(&body);
3119/// ctx.send_body_list_with_data(&body_list).await.unwrap();
3120/// }
3121/// }
3122/// ```
3123///
3124/// Using variable name and buffer size (reversed order):
3125///
3126/// ```rust
3127/// use hyperlane::*;
3128/// use hyperlane_macros::*;
3129///
3130/// #[route("/ws")]
3131/// struct Websocket;
3132///
3133/// impl ServerHook for Websocket {
3134/// async fn new(_ctx: &Context) -> Self {
3135/// Self
3136/// }
3137///
3138/// #[ws]
3139/// #[ws_from_stream(request, 1024)]
3140/// async fn handle(self, ctx: &Context) {
3141/// let body: &RequestBody = request.get_body();
3142/// let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(&body);
3143/// ctx.send_body_list_with_data(&body_list).await.unwrap();
3144/// }
3145/// }
3146///
3147/// impl Websocket {
3148/// #[ws_from_stream(request)]
3149/// async fn ws_from_stream_with_ref_self(&self, ctx: &Context) {}
3150/// }
3151///
3152/// #[ws_from_stream]
3153/// async fn standalone_ws_from_stream_handler(ctx: &Context) {}
3154/// ```
3155#[proc_macro_attribute]
3156pub fn ws_from_stream(attr: TokenStream, item: TokenStream) -> TokenStream {
3157 ws_from_stream_macro(attr, item)
3158}
3159
3160/// Wraps function body with HTTP stream processing.
3161///
3162/// This attribute macro generates code that wraps the function body with a check to see if
3163/// data can be read from an HTTP stream. The function body is only executed
3164/// if data is successfully read from the stream.
3165///
3166/// This attribute macro generates code that wraps the function body with a check to see if
3167/// data can be read from an HTTP stream. The function body is only executed
3168/// if data is successfully read from the stream.
3169///
3170/// # Arguments
3171///
3172/// - `TokenStream`: The buffer to read from the HTTP stream.
3173/// - `TokenStream`: The function item to be modified
3174///
3175/// # Returns
3176///
3177/// Returns a TokenStream containing the modified function with HTTP stream processing logic.
3178///
3179/// # Examples
3180///
3181/// Using with epilogue_macros:
3182///
3183/// ```rust
3184/// use hyperlane::*;
3185/// use hyperlane_macros::*;
3186///
3187/// #[route("/http_from_stream")]
3188/// struct HttpFromStreamTest;
3189///
3190/// impl ServerHook for HttpFromStreamTest {
3191/// async fn new(_ctx: &Context) -> Self {
3192/// Self
3193/// }
3194///
3195/// #[epilogue_macros(
3196/// request_query("test" => request_query_option),
3197/// response_body(&format!("request query: {request_query_option:?}")),
3198/// send,
3199/// http_from_stream(1024)
3200/// )]
3201/// async fn handle(self, ctx: &Context) {}
3202/// }
3203/// ```
3204///
3205/// Using with variable name:
3206///
3207/// ```rust
3208/// use hyperlane::*;
3209/// use hyperlane_macros::*;
3210///
3211/// #[route("/http_from_stream")]
3212/// struct HttpFromStreamTest;
3213///
3214/// impl ServerHook for HttpFromStreamTest {
3215/// async fn new(_ctx: &Context) -> Self {
3216/// Self
3217/// }
3218///
3219/// #[epilogue_macros(
3220/// http_from_stream(_request)
3221/// )]
3222/// async fn handle(self, ctx: &Context) {}
3223/// }
3224///
3225/// impl HttpFromStreamTest {
3226/// #[http_from_stream(_request)]
3227/// async fn http_from_stream_with_ref_self(&self, ctx: &Context) {}
3228/// }
3229///
3230/// #[http_from_stream]
3231/// async fn standalone_http_from_stream_handler(ctx: &Context) {}
3232/// ```
3233#[proc_macro_attribute]
3234pub fn http_from_stream(attr: TokenStream, item: TokenStream) -> TokenStream {
3235 http_from_stream_macro(attr, item)
3236}