1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright 2024 G-Core Innovations SARL
*/
//! Procedural macros for FastEdge applications.
//!
//! This crate provides the `#[fastedge::http]` attribute macro that transforms
//! a regular Rust function into a WebAssembly component handler for HTTP requests.
use TokenStream;
use quote;
use ;
/// Marks a function as the HTTP request handler for a FastEdge application.
///
/// This attribute macro transforms your function into a WebAssembly component that
/// can process HTTP requests. The function must have the following signature:
///
/// ```ignore
/// fn handler_name(req: Request<Body>) -> Result<Response<Body>>
/// ```
///
/// # Function Requirements
///
/// - **Parameter**: Must accept a single parameter of type `Request<Body>`
/// - **Return Type**: Must return `Result<Response<Body>>` (typically using `anyhow::Result`)
/// - **Function Name**: Can be any valid Rust identifier (commonly `main`)
///
/// # Error Handling
///
/// If your function returns an `Err`, the macro automatically converts it into an
/// HTTP 500 (Internal Server Error) response with the error message in the body.
///
/// # Examples
///
/// ## Basic Handler
///
/// ```no_run
/// use anyhow::Result;
/// use fastedge::body::Body;
/// use fastedge::http::{Request, Response, StatusCode};
///
/// #[fastedge::http]
/// fn main(_req: Request<Body>) -> Result<Response<Body>> {
/// Response::builder()
/// .status(StatusCode::OK)
/// .body(Body::from("Hello, World!"))
/// .map_err(Into::into)
/// }
/// ```
///
/// ## Request Processing
///
/// ```no_run
/// use anyhow::{Result, anyhow};
/// use fastedge::body::Body;
/// use fastedge::http::{Method, Request, Response, StatusCode};
///
/// #[fastedge::http]
/// fn main(req: Request<Body>) -> Result<Response<Body>> {
/// match req.method() {
/// &Method::GET => {
/// Response::builder()
/// .status(StatusCode::OK)
/// .body(Body::from("GET request received"))
/// .map_err(Into::into)
/// }
/// &Method::POST => {
/// let body_data = req.body();
/// Response::builder()
/// .status(StatusCode::CREATED)
/// .body(Body::from(format!("Received {} bytes", body_data.len())))
/// .map_err(Into::into)
/// }
/// _ => {
/// Err(anyhow!("Method not allowed"))
/// }
/// }
/// }
/// ```
///
/// ## With Backend Requests
///
/// ```no_run
/// use anyhow::Result;
/// use fastedge::body::Body;
/// use fastedge::http::{Method, Request, Response, StatusCode};
///
/// #[fastedge::http]
/// fn main(req: Request<Body>) -> Result<Response<Body>> {
/// // Make a request to a backend service
/// let backend_req = Request::builder()
/// .method(Method::GET)
/// .uri("https://api.example.com/data")
/// .body(Body::empty())?;
///
/// let backend_resp = fastedge::send_request(backend_req)?;
///
/// // Forward the response
/// Ok(backend_resp)
/// }
/// ```
///
/// ## Error Handling
///
/// ```no_run
/// use anyhow::{Result, Context};
/// use fastedge::body::Body;
/// use fastedge::http::{Request, Response, StatusCode};
///
/// #[fastedge::http]
/// fn main(req: Request<Body>) -> Result<Response<Body>> {
/// let query = req.uri()
/// .query()
/// .context("Missing query parameters")?;
///
/// // Process query...
///
/// Response::builder()
/// .status(StatusCode::OK)
/// .body(Body::from("Success"))
/// .map_err(Into::into)
/// }
/// ```
///
/// # Generated Code
///
/// The macro generates a WebAssembly component that:
/// 1. Implements the `Guest` trait for the HTTP handler interface
/// 2. Converts between bindgen types and standard `http` crate types
/// 3. Handles error conversion to HTTP responses
/// 4. Exports the component for use by the FastEdge runtime
///
/// # See Also
///
/// - [`fastedge::http`](https://docs.rs/fastedge/latest/fastedge/http/index.html) - HTTP types module
/// - [`fastedge::body::Body`](https://docs.rs/fastedge/latest/fastedge/body/struct.Body.html) - Body type