ignitia 0.2.4

A blazing fast, lightweight web framework for Rust that ignites your development journey
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! # HTTP Method Handling and Conversions
//!
//! This module provides utilities for working with HTTP methods in the Ignitia web framework.
//! It includes custom HTTP method types, conversions to/from standard HTTP methods, and string
//! parsing capabilities for flexible method handling in routing and middleware.
//!
//! ## Features
//!
//! - **Custom Method Enum**: Framework-specific HTTP method enumeration
//! - **Standard Conversions**: Seamless conversion to/from `http::Method`
//! - **String Parsing**: Parse HTTP methods from strings with error handling
//! - **Performance Optimized**: Efficient method comparisons and conversions
//! - **Comprehensive Coverage**: Support for all standard HTTP methods
//!
//! ## HTTP Method Overview
//!
//! ### Safe Methods (Idempotent and Safe)
//! - **GET**: Retrieve data without side effects
//! - **HEAD**: Get headers only, no response body
//! - **OPTIONS**: Query supported methods and capabilities
//!
//! ### Idempotent Methods (Can be repeated safely)
//! - **PUT**: Replace or create resource completely
//! - **DELETE**: Remove resource
//! - **GET, HEAD, OPTIONS**: Also idempotent
//!
//! ### Non-Idempotent Methods
//! - **POST**: Create resource or perform action (side effects)
//! - **PATCH**: Partial resource modification
//!
//! ### Connection Methods
//! - **CONNECT**: Establish tunnel (typically for HTTPS proxy)
//! - **TRACE**: Diagnostic trace of request path
//!
//! ## Usage Examples
//!
//! ### Basic Method Usage
//! ```
//! use ignitia::router::method::HttpMethod;
//! use http::Method;
//!
//! // Create custom method
//! let custom_method = HttpMethod::Get;
//!
//! // Convert to standard HTTP method
//! let std_method: Method = custom_method.into();
//! assert_eq!(std_method, Method::GET);
//!
//! // Convert from standard HTTP method
//! let from_std: HttpMethod = Method::POST.into();
//! assert_eq!(from_std, HttpMethod::Post);
//! ```
//!
//! ### String Parsing
//! ```
//! use ignitia::router::method::HttpMethod;
//! use std::str::FromStr;
//!
//! // Parse from string (case insensitive)
//! let method = HttpMethod::from_str("GET").unwrap();
//! assert_eq!(method, HttpMethod::Get);
//!
//! let method = HttpMethod::from_str("post").unwrap();
//! assert_eq!(method, HttpMethod::Post);
//!
//! // Invalid method returns error
//! assert!(HttpMethod::from_str("INVALID").is_err());
//! ```
//!
//! ### Router Integration
//! ```
//! use ignitia::{Router, Response};
//! use ignitia::router::method::HttpMethod;
//! use http::Method;
//!
//! let router = Router::new()
//!     .route_with("/api/data", Method::GET, || async {
//!         Ok(Response::json(serde_json::json!({
//!             "method": "GET",
//!             "message": "Data retrieved"
//!         }))?)
//!     })
//!     .route_with("/api/data", Method::POST, || async {
//!         Ok(Response::json(serde_json::json!({
//!             "method": "POST",
//!             "message": "Data created"
//!         }))?)
//!     });
//! ```
//!
//! ## Method Characteristics
//!
//! ### RESTful API Patterns
//! ```
//! use ignitia::{Router, Response, Path, Json};
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Deserialize)]
//! struct CreateUser {
//!     name: String,
//!     email: String,
//! }
//!
//! #[derive(Serialize)]
//! struct User {
//!     id: u32,
//!     name: String,
//!     email: String,
//! }
//!
//! let api_router = Router::new()
//!     // GET /users - List all users (safe, idempotent)
//!     .get("/users", || async {
//!         Ok(Response::json(vec![
//!             User { id: 1, name: "Alice".into(), email: "alice@example.com".into() }
//!         ])?)
//!     })
//!     // POST /users - Create new user (not idempotent)
//!     .post("/users", |Json(user): Json<CreateUser>| async move {
//!         let new_user = User {
//!             id: 123, // Would be generated
//!             name: user.name,
//!             email: user.email,
//!         };
//!         Ok(Response::json(new_user)?.with_status_code(201))
//!     })
//!     // GET /users/:id - Get specific user (safe, idempotent)
//!     .get("/users/:id", |Path(id): Path<u32>| async move {
//!         Ok(Response::json(User {
//!             id,
//!             name: "User".into(),
//!             email: "user@example.com".into(),
//!         })?)
//!     })
//!     // PUT /users/:id - Replace user completely (idempotent)
//!     .put("/users/:id", |Path(id): Path<u32>, Json(user): Json<CreateUser>| async move {
//!         Ok(Response::json(User {
//!             id,
//!             name: user.name,
//!             email: user.email,
//!         })?)
//!     })
//!     // PATCH /users/:id - Partial update (not idempotent)
//!     .patch("/users/:id", |Path(id): Path<u32>| async move {
//!         Ok(Response::json(serde_json::json!({
//!             "id": id,
//!             "message": "User updated"
//!         }))?)
//!     })
//!     // DELETE /users/:id - Remove user (idempotent)
//!     .delete("/users/:id", |Path(id): Path<u32>| async move {
//!         Ok(Response::json(serde_json::json!({
//!             "message": format!("User {} deleted", id)
//!         }))?)
//!     })
//!     // OPTIONS /users - Get allowed methods
//!     .options("/users", || async {
//!         Ok(Response::text("")
//!             .with_status_code(204)
//!             .with_header("Allow", "GET, POST, OPTIONS"))
//!     });
//! ```
//!
//! ## Advanced Usage Patterns
//!
//! ### Method-Based Middleware
//! ```
//! use ignitia::{Router, Response, Request, Result, Middleware};
//! use http::Method;
//!
//! struct MethodLoggingMiddleware;
//!
//! #[async_trait::async_trait]
//! impl Middleware for MethodLoggingMiddleware {
//!     async fn before(&self, req: &mut Request) -> Result<()> {
//!         match req.method {
//!             Method::GET | Method::HEAD | Method::OPTIONS => {
//!                 tracing::info!("Safe method: {}", req.method);
//!             }
//!             Method::PUT | Method::DELETE => {
//!                 tracing::info!("Idempotent method: {}", req.method);
//!             }
//!             Method::POST | Method::PATCH => {
//!                 tracing::warn!("Non-idempotent method: {}", req.method);
//!             }
//!             _ => {
//!                 tracing::debug!("Other method: {}", req.method);
//!             }
//!         }
//!         Ok(())
//!     }
//! }
//! ```
//!
//! ### Dynamic Method Routing
//! ```
//! use ignitia::{Router, Response, Request, Method};
//!
//! async fn dynamic_handler(req: Request) -> ignitia::Result<Response> {
//!     let response_data = match req.method {
//!         Method::GET => serde_json::json!({
//!             "action": "retrieve",
//!             "safe": true,
//!             "idempotent": true
//!         }),
//!         Method::POST => serde_json::json!({
//!             "action": "create",
//!             "safe": false,
//!             "idempotent": false
//!         }),
//!         Method::PUT => serde_json::json!({
//!             "action": "replace",
//!             "safe": false,
//!             "idempotent": true
//!         }),
//!         Method::PATCH => serde_json::json!({
//!             "action": "modify",
//!             "safe": false,
//!             "idempotent": false
//!         }),
//!         Method::DELETE => serde_json::json!({
//!             "action": "remove",
//!             "safe": false,
//!             "idempotent": true
//!         }),
//!         _ => serde_json::json!({
//!             "action": "unknown",
//!             "method": req.method.to_string()
//!         })
//!     };
//!
//!     Response::json(response_data)
//! }
//! ```
//!
//! ## Performance Considerations
//!
//! ### Method Comparison Performance
//! Method comparisons are highly optimized:
//! - Enum variants use simple integer comparisons
//! - No string allocations during comparison
//! - Branch prediction friendly for common methods (GET, POST)
//!
//! ### Memory Efficiency
//! - Small enum variants (1 byte each)
//! - No heap allocations for method storage
//! - Efficient conversion between framework and standard types

use http::Method;
use std::str::FromStr;

/// Custom HTTP method enumeration for the Ignitia web framework.
///
/// This enum provides a framework-specific representation of HTTP methods
/// with efficient conversions to/from the standard `http::Method` type.
/// It's designed for use in routing, middleware, and request handling.
///
/// # Method Categories
///
/// ## Safe Methods (RFC 7231)
/// Safe methods do not have side effects on the server:
/// - `Get`: Retrieve resource representation
/// - `Head`: Get resource metadata (headers only)
/// - `Options`: Query communication options
///
/// ## Idempotent Methods (RFC 7231)
/// Idempotent methods can be called multiple times with the same effect:
/// - `Get`, `Head`, `Options`: Also safe
/// - `Put`: Replace resource (same result each time)
/// - `Delete`: Remove resource (404 after first deletion is fine)
///
/// ## Non-Idempotent Methods
/// These methods may have different effects when repeated:
/// - `Post`: Create resource or trigger action
/// - `Patch`: Modify resource (result may depend on current state)
///
/// ## Special Methods
/// - `Connect`: Establish tunnel connection
/// - `Trace`: Diagnostic message loop-back
///
/// # Examples
///
/// ## Basic Usage
/// ```
/// use ignitia::router::method::HttpMethod;
///
/// let method = HttpMethod::Get;
/// assert_eq!(method, HttpMethod::Get);
///
/// // Pattern matching
/// match method {
///     HttpMethod::Get => println!("Safe GET request"),
///     HttpMethod::Post => println!("Create or action request"),
///     _ => println!("Other method"),
/// }
/// ```
///
/// ## RESTful Patterns
/// ```
/// use ignitia::router::method::HttpMethod;
///
/// fn handle_resource_method(method: HttpMethod) -> &'static str {
///     match method {
///         HttpMethod::Get => "Retrieve resource",
///         HttpMethod::Post => "Create new resource",
///         HttpMethod::Put => "Replace entire resource",
///         HttpMethod::Patch => "Update part of resource",
///         HttpMethod::Delete => "Remove resource",
///         HttpMethod::Options => "Get allowed methods",
///         _ => "Method not typically used for resources"
///     }
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum HttpMethod {
    /// GET method - retrieve resource without side effects (safe, idempotent)
    Get,
    /// POST method - create resource or perform action (not safe, not idempotent)
    Post,
    /// PUT method - replace resource completely (not safe, idempotent)
    Put,
    /// DELETE method - remove resource (not safe, idempotent)
    Delete,
    /// PATCH method - partial resource modification (not safe, not idempotent)
    Patch,
    /// HEAD method - get headers only, no body (safe, idempotent)
    Head,
    /// OPTIONS method - query supported methods (safe, idempotent)
    Options,
    /// CONNECT method - establish tunnel connection
    Connect,
    /// TRACE method - diagnostic trace of request path
    Trace,

    /// ANY method - match any method
    Any,
}

impl From<Method> for HttpMethod {
    /// Converts from the standard `http::Method` to framework `HttpMethod`.
    ///
    /// This conversion handles all standard HTTP methods and provides a
    /// fallback to `Get` for any unrecognized methods.
    ///
    /// # Parameters
    /// - `method`: The standard HTTP method to convert
    ///
    /// # Returns
    /// The corresponding `HttpMethod` variant
    ///
    /// # Examples
    /// ```
    /// use ignitia::router::method::HttpMethod;
    /// use http::Method;
    ///
    /// let std_method = Method::POST;
    /// let custom_method: HttpMethod = std_method.into();
    /// assert_eq!(custom_method, HttpMethod::Post);
    ///
    /// // Extension methods are mapped to Get as fallback
    /// let extension_method = Method::from_bytes(b"CUSTOM").unwrap();
    /// let custom_method: HttpMethod = extension_method.into();
    /// assert_eq!(custom_method, HttpMethod::Get);
    /// ```
    fn from(method: Method) -> Self {
        match method {
            Method::GET => HttpMethod::Get,
            Method::POST => HttpMethod::Post,
            Method::PUT => HttpMethod::Put,
            Method::DELETE => HttpMethod::Delete,
            Method::PATCH => HttpMethod::Patch,
            Method::HEAD => HttpMethod::Head,
            Method::OPTIONS => HttpMethod::Options,
            Method::CONNECT => HttpMethod::Connect,
            Method::TRACE => HttpMethod::Trace,
            _ => HttpMethod::Get, // Default fallback for extension methods
        }
    }
}

impl From<HttpMethod> for Method {
    /// Converts from framework `HttpMethod` to standard `http::Method`.
    ///
    /// This conversion is lossless for all supported HTTP methods.
    ///
    /// # Parameters
    /// - `method`: The framework HTTP method to convert
    ///
    /// # Returns
    /// The corresponding standard `Method`
    ///
    /// # Examples
    /// ```
    /// use ignitia::router::method::HttpMethod;
    /// use http::Method;
    ///
    /// let custom_method = HttpMethod::Delete;
    /// let std_method: Method = custom_method.into();
    /// assert_eq!(std_method, Method::DELETE);
    /// ```
    fn from(method: HttpMethod) -> Self {
        match method {
            HttpMethod::Get => Method::GET,
            HttpMethod::Post => Method::POST,
            HttpMethod::Put => Method::PUT,
            HttpMethod::Delete => Method::DELETE,
            HttpMethod::Patch => Method::PATCH,
            HttpMethod::Head => Method::HEAD,
            HttpMethod::Options => Method::OPTIONS,
            HttpMethod::Connect => Method::CONNECT,
            HttpMethod::Trace => Method::TRACE,
            HttpMethod::Any => Method::from_bytes(b"ANY").unwrap_or(Method::GET),
        }
    }
}

impl FromStr for HttpMethod {
    type Err = ();

    /// Parses an HTTP method from a string representation.
    ///
    /// This parser is case-insensitive and handles all standard HTTP methods.
    /// Unknown methods return an error rather than falling back to a default.
    ///
    /// # Parameters
    /// - `s`: The string to parse as an HTTP method
    ///
    /// # Returns
    /// - `Ok(HttpMethod)`: Successfully parsed method
    /// - `Err(())`: Invalid or unrecognized method string
    ///
    /// # Supported Strings
    /// Case-insensitive parsing of:
    /// - "GET", "get", "Get" → `HttpMethod::Get`
    /// - "POST", "post", "Post" → `HttpMethod::Post`
    /// - "PUT", "put", "Put" → `HttpMethod::Put`
    /// - "DELETE", "delete", "Delete" → `HttpMethod::Delete`
    /// - "PATCH", "patch", "Patch" → `HttpMethod::Patch`
    /// - "HEAD", "head", "Head" → `HttpMethod::Head`
    /// - "OPTIONS", "options", "Options" → `HttpMethod::Options`
    /// - "CONNECT", "connect", "Connect" → `HttpMethod::Connect`
    /// - "TRACE", "trace", "Trace" → `HttpMethod::Trace`
    ///
    /// # Examples
    /// ```
    /// use ignitia::router::method::HttpMethod;
    /// use std::str::FromStr;
    ///
    /// // Standard cases
    /// assert_eq!(HttpMethod::from_str("GET").unwrap(), HttpMethod::Get);
    /// assert_eq!(HttpMethod::from_str("POST").unwrap(), HttpMethod::Post);
    ///
    /// // Case insensitive
    /// assert_eq!(HttpMethod::from_str("get").unwrap(), HttpMethod::Get);
    /// assert_eq!(HttpMethod::from_str("Post").unwrap(), HttpMethod::Post);
    /// assert_eq!(HttpMethod::from_str("DELETE").unwrap(), HttpMethod::Delete);
    ///
    /// // Invalid methods
    /// assert!(HttpMethod::from_str("INVALID").is_err());
    /// assert!(HttpMethod::from_str("").is_err());
    /// assert!(HttpMethod::from_str("HTTP").is_err());
    /// ```
    ///
    /// ## Usage in Request Processing
    /// ```
    /// use ignitia::router::method::HttpMethod;
    /// use std::str::FromStr;
    ///
    /// fn process_method_header(method_str: &str) -> Result<String, &'static str> {
    ///     match HttpMethod::from_str(method_str) {
    ///         Ok(HttpMethod::Get) => Ok("Safe read operation".to_string()),
    ///         Ok(HttpMethod::Post) => Ok("Create operation".to_string()),
    ///         Ok(HttpMethod::Put) => Ok("Replace operation".to_string()),
    ///         Ok(HttpMethod::Delete) => Ok("Remove operation".to_string()),
    ///         Ok(_) => Ok("Other operation".to_string()),
    ///         Err(_) => Err("Unknown HTTP method"),
    ///     }
    /// }
    /// ```
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_uppercase().as_str() {
            "GET" => Ok(HttpMethod::Get),
            "POST" => Ok(HttpMethod::Post),
            "PUT" => Ok(HttpMethod::Put),
            "DELETE" => Ok(HttpMethod::Delete),
            "PATCH" => Ok(HttpMethod::Patch),
            "HEAD" => Ok(HttpMethod::Head),
            "OPTIONS" => Ok(HttpMethod::Options),
            "CONNECT" => Ok(HttpMethod::Connect),
            "TRACE" => Ok(HttpMethod::Trace),
            "ANY" => Ok(HttpMethod::Any),
            _ => Err(()),
        }
    }
}