sword 0.2.1

Structured web framework built on top of tokio ecosystem, providing powerful features for building robust web applications.
Documentation
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
mod error;
mod extract;

#[cfg(feature = "validation-validator")]
mod validator;

use super::interceptor::HttpInterceptorResult;
use axum::{
    body::Bytes as BodyBytes,
    http::{Extensions, HeaderMap, HeaderName, HeaderValue, Method, Uri},
    middleware::Next,
};
use axum_responses::JsonResponse;
use serde::de::DeserializeOwned;
use std::{collections::HashMap, fmt::Display, str::FromStr};
use sword_core::layers::RequestId;
use sword_layers::cookies::Cookies;

pub use error::*;

#[allow(unused_imports)]
pub use extract::*;

#[cfg(feature = "validation-validator")]
pub use validator::ValidatorRequestValidation;

/// Represents the incoming request in the Sword framework.
///
/// `Request` is the primary extractor for accessing request data in Sword applications.
/// It provides access to request parameters, body data, HTTP method, headers, URI,
#[derive(Debug)]
pub struct Request {
    params: HashMap<String, String>,
    body_bytes: BodyBytes,
    method: Method,
    headers: HeaderMap,
    uri: Uri,
    next: Option<Next>,
    /// Axum extensions for additional request metadata.
    pub extensions: Extensions,
}

impl Request {
    pub fn uri(&self) -> String {
        self.uri.to_string()
    }

    pub const fn method(&self) -> &Method {
        &self.method
    }

    pub fn header(&self, key: &str) -> Option<&str> {
        self.headers.get(key).and_then(|value| value.to_str().ok())
    }

    pub const fn headers(&self) -> &HeaderMap {
        &self.headers
    }

    pub fn headers_mut(&mut self) -> &mut HeaderMap {
        &mut self.headers
    }

    /// Sets or updates the value of a header in the request.
    ///
    /// ### Arguments
    /// * `name` - The header name to set. Must implement `Into<String>`.
    /// * `value` - The header value to set. Must implement `Into<String>`.
    ///
    /// ### Note
    /// If the header already exists, its value will be overwritten.
    pub fn set_header(
        &mut self,
        name: impl Into<String>,
        value: impl Into<String>,
    ) -> Result<(), RequestError> {
        let header_name = name.into();
        let header_value = value.into();

        let header_name = header_name
            .parse::<HeaderName>()
            .map_err(|_| RequestError::InvalidHeaderName(header_name))?;

        let header_value = HeaderValue::from_str(&header_value)
            .map_err(|_| RequestError::InvalidHeaderValue(header_value))?;

        self.headers.insert(header_name, header_value);

        Ok(())
    }

    /// Retrieves and parses a route parameter by name.
    ///
    /// This method extracts URL parameters (path parameters) from the request
    /// and converts them to the specified type. The parameter must implement
    /// the `FromStr` trait for conversion.
    ///
    /// ### Type Parameters
    ///
    /// * `T` - The type to convert the parameter to (must implement `FromStr`)
    ///
    /// ### Arguments
    ///
    /// * `key` - The name of the route parameter to extract
    ///
    /// ### Returns
    ///
    /// Returns `Ok(T)` with the parsed value if the parameter exists and can be
    /// converted, or `Err(RequestError)` if the parameter is missing or invalid.
    ///
    /// ### Errors
    ///
    /// This function will return an error if:
    /// - The parameter is not found in the request
    /// - The parameter value cannot be parsed to type `T`
    ///
    /// ### Example
    ///
    /// ```rust,ignore
    /// use sword::prelude::*;
    ///
    /// ... asuming you have a controller struct ...
    ///
    /// #[get("/users/{id}/posts/{post_id}")]
    /// async fn get_user_post(&self, req: Request) -> HttpResult {
    ///     let user_id: u32 = req.param("id")?;
    ///     let post_id: u64 = req.param("post_id")?;
    ///
    ///     let message = format!("User ID: {}, Post ID: {}", user_id, post_id);
    ///     
    ///     Ok(JsonResponse::Ok().message(message))
    /// }
    /// ```
    pub fn param<T>(&self, key: &str) -> Result<T, RequestError>
    where
        T: FromStr,
        T::Err: Display,
    {
        if let Some(value) = self.params.get(key) {
            return value.parse::<T>().map_err(|e| {
                RequestError::parse_error(
                    format!("Invalid parameter format for '{key}'"),
                    format!("Parse  Error: {e}"),
                )
            });
        }

        Err(RequestError::parse_error(
            "Parameter not found",
            format!("Parameter '{key}' not found in request parameters"),
        ))
    }

    // pub const fn params(&self) -> Result<&HashMap<String, String>, RequestError> {
    //     let params = self.uri.path_and_query().ok_or(

    //     )
    // }

    /// Deserializes the request body from JSON to a specific type.
    ///
    /// This method reads the request body and attempts to parse it as JSON,
    /// deserializing it to the specified type. The body is consumed during
    /// this operation.
    ///
    /// ### Type Parameters
    ///
    /// * `T` - The type to deserialize the JSON body to (must implement `DeserializeOwned`)
    ///
    /// ### Returns
    ///
    /// Returns `Ok(T)` with the deserialized instance if the JSON is valid,
    /// or `Err(RequestError)` if the body is empty or invalid JSON.
    ///
    /// ### Errors
    ///
    /// This function will return an error if:
    /// - The request body is empty
    /// - The body contains invalid JSON
    /// - The JSON structure doesn't match the target type `T`
    ///
    /// ### Example
    ///
    /// ```rust,ignore
    /// use sword::prelude::*;
    /// use serde::Deserialize;
    ///
    /// #[derive(Deserialize)]
    /// struct CreateUserRequest {
    ///     name: String,
    ///     email: String,
    ///     age: u32,
    /// }
    ///
    /// ... asuming you have a controller struct ...
    ///
    /// #[post("/users")]
    /// async fn create_user(&self, req: Request) -> HttpResult {
    ///     let user_data: CreateUserRequest = req.body()?;
    ///     
    ///     // Process user creation...
    ///     
    ///     Ok(JsonResponse::Created().message("User created"))
    /// }
    /// ```
    pub fn body<T: DeserializeOwned>(&self) -> Result<T, RequestError> {
        if self.body_bytes.is_empty() {
            return Err(RequestError::BodyIsEmpty);
        }

        if !self.is_content_type_json() {
            return Err(RequestError::unsupported_media_type(
                "Expected Content-Type to be application/json",
            ));
        }

        serde_json::from_slice(&self.body_bytes).map_err(|e| {
            RequestError::deserialization_error(
                "Invalid request body",
                "Failed to deserialize request body to the required type.".into(),
                e.into(),
            )
        })
    }

    /// Deserializes query parameters from the URL query string to a specific type.
    ///
    /// This method parses the query string portion of the URL and deserializes
    /// it to the specified type. Since query parameters are optional in HTTP,
    /// this method returns `Option<T>` where `None` indicates no query parameters
    /// were present.
    ///
    /// ### Type Parameters
    ///
    /// * `T` - The type to deserialize the query parameters to (must implement `DeserializeOwned`)
    ///
    /// ### Returns
    ///
    /// Returns:
    /// - `Ok(Some(T))` with the deserialized query parameters if they exist and are valid
    /// - `Ok(None)` if no query parameters are present in the URL
    /// - `Err(RequestError)` if query parameters exist but cannot be deserialized
    ///
    /// ### Errors
    ///
    /// This function will return an error if the query parameters exist but
    /// cannot be parsed or deserialized to the target type.
    ///
    /// ### Example
    ///
    /// ```rust,ignore
    /// use sword::prelude::*;
    /// use serde::Deserialize;
    ///
    /// #[derive(Deserialize, Default)]
    /// struct SearchQuery {
    ///     q: Option<String>,
    ///     page: Option<u32>,
    ///     limit: Option<u32>,
    /// }
    ///
    /// ... asuming you have a controller struct ...
    ///
    /// #[get("/search")]
    /// async fn search(&self, req: Request) -> HttpResult {
    ///     let query: SearchQuery = req.query()?.unwrap_or_default();
    ///     
    ///     let search_term = query.q.unwrap_or("".into());
    ///     let page = query.page.unwrap_or(1);
    ///     let limit = query.limit.unwrap_or(20);
    ///     
    ///     Ok(JsonResponse::Ok().data(format!(
    ///         "Search results for '{search_term}', page {page}, limit {limit}"
    ///     )))
    /// }
    /// ```
    pub fn query<T: DeserializeOwned>(&self) -> Result<Option<T>, RequestError> {
        let Some(query_string) = self.uri.query() else {
            return Ok(None);
        };

        if query_string.is_empty() {
            return Ok(None);
        }

        let deserializer = serde_urlencoded::Deserializer::new(
            form_urlencoded::parse(query_string.as_bytes()),
        );

        let deserialized = T::deserialize(deserializer).map_err(|e| {
            RequestError::deserialization_error(
                "Invalid query parameters",
                "Failed to deserialize query params to the required type.".into(),
                e.into(),
            )
        })?;

        Ok(Some(deserialized))
    }

    /// Access the cookies from the request.
    /// This method returns a reference to the `Cookies` instance, a struct that provides
    /// methods to get, set, and remove cookies.
    ///
    /// The documentation for `tower_cookies::Cookies` can be found [here](https://docs.rs/tower-cookies/latest/tower_cookies/struct.Cookies.html)
    /// Also, the other cookie-related types like `Cookie`, `CookieBuilder`, `Expiration`, and `SameSite` can be found in the `tower_cookies` crate.
    ///
    /// ### Usage
    /// ```rust,ignore
    ///
    /// use sword::prelude::*;
    ///
    /// ... asuming controller struct ...
    ///
    /// #[get("/show-cookies")]
    /// async fn show_cookies(&self, req: Request) -> HttpResult {
    ///     let cookies = ctx.cookies()?;
    ///     let session_cookie = cookies.get("session_id");
    ///
    ///     if let Some(cookie) = session_cookie {
    ///         Ok(JsonResponse::Ok().body(format!("Session ID: {}", cookie.value())))
    ///     }
    ///
    ///     Ok(JsonResponse::Ok().body("No session cookie found"))
    /// }
    /// ```
    pub fn cookies(&self) -> Result<&Cookies, JsonResponse> {
        self.extensions.get::<Cookies>().ok_or_else(|| {
            JsonResponse::InternalServerError()
                .message("Can't extract cookies. Is `CookieManagerLayer` enabled?")
        })
    }

    pub fn authorization(&self) -> Option<&str> {
        self.header("Authorization")
    }

    pub fn user_agent(&self) -> Option<&str> {
        self.header("User-Agent")
    }

    pub fn ip(&self) -> Option<&str> {
        self.header("X-Forwarded-For")
    }

    pub fn ips(&self) -> Option<Vec<&str>> {
        self.header("X-Forwarded-For")
            .map(|ips| ips.split(',').map(|s| s.trim()).collect())
    }

    pub fn protocol(&self) -> &str {
        self.header("X-Forwarded-Proto").unwrap_or("http")
    }

    pub fn content_length(&self) -> Option<u64> {
        self.header("Content-Length")
            .and_then(|value| value.parse::<u64>().ok())
    }

    /// Returns the unique request ID from the `RequestId` extension if present.
    /// If not present, returns "unknown".
    ///
    /// `RequestId` is added automatically by the `RequestIdLayer` middleware.
    /// If "unknown" is returned, it indicates that the middleware was not applied.
    pub fn id(&self) -> String {
        if let Some(id) = self.extensions.get::<RequestId>() {
            return id.header_value().to_str().unwrap_or_default().to_string();
        }

        "unknown".to_string()
    }

    pub fn content_type(&self) -> Option<&str> {
        self.header("Content-Type")
    }

    #[cfg(feature = "multipart")]
    /// Extracts multipart form data from the request.
    ///
    /// ### Errors
    /// Returns `RequestError::ParseError` if the multipart form data cannot be parsed.
    ///
    /// ### Example
    /// ```rust,ignore
    /// use sword::prelude::*;
    ///
    /// ... asuming a controller struct ...
    ///
    /// #[post("/upload")]
    /// async fn upload(&self, req: Request) -> HttpResult {
    ///     let mut multipart = req.multipart().await?;
    ///     let mut field_names = Vec::new();
    ///
    ///     // Process each field in the multipart form data
    ///     // And ensure to handle errors appropriately
    ///     while let Some(field) = multipart.next_field().await? {
    ///         field_names.push(field.name().unwrap_or("Unknown").to_string());
    ///     }
    ///
    ///     Ok(JsonResponse::Ok().data(field_names))
    /// }
    /// ```
    pub async fn multipart(
        self,
    ) -> Result<axum::extract::multipart::Multipart, RequestError> {
        use axum::extract::FromRequest;
        Ok(axum::extract::Multipart::from_request(self.try_into()?, &()).await?)
    }

    pub(crate) fn is_content_type_json(&self) -> bool {
        let Some(content_type) = self.content_type() else {
            return false;
        };

        let Ok(mime) = content_type.parse::<mime::Mime>() else {
            return false;
        };

        mime.type_() == "application"
            && (mime.subtype() == "json"
                || mime.suffix().is_some_and(|name| name == "json"))
    }

    #[doc(hidden)]
    pub fn clear_next(&mut self) {
        self.next = None;
    }

    #[doc(hidden)]
    pub fn set_next(&mut self, next: Next) {
        self.next = Some(next);
    }

    /// Runs the next interceptor or handler in the chain.
    ///
    /// This method must be used only in interceptor implementations to
    /// pass control to the next interceptor or the final request handler.
    pub async fn next(mut self) -> HttpInterceptorResult {
        let Some(next) = self.next.take() else {
            tracing::error!(
                "Attempted to call `next()` on Request in a context that is not a `OnRequest` `Interceptor`"
            );
            return Err(JsonResponse::InternalServerError());
        };

        Ok(next.run(self.try_into()?).await)
    }
}