http_kit/
lib.rs

1#![deny(unsafe_code)]
2#![no_std]
3#![warn(missing_docs, missing_debug_implementations)]
4//! A flexible and ergonomic HTTP toolkit for Rust.
5//!
6//! This crate provides high-level abstractions for HTTP operations while maintaining
7//! performance and type safety. It's designed to be no-std compatible with optional
8//! standard library features.
9//!
10//! # Features
11//!
12//! - **Type-safe HTTP primitives** - Request, Response, Headers, and Body types with strong type checking
13//! - **Streaming support** - Efficient handling of large payloads through streaming interfaces
14//! - **Body transformations** - Convert between different body formats (JSON, form data, files) with zero-copy when possible
15//! - **Middleware system** - Extensible middleware architecture for request/response processing
16//! - **Async/await ready** - Built on top of `futures-lite` for async I/O operations
17//!
18//! # Optional Features
19//!
20//! - `json` - JSON serialization/deserialization via serde_json (enabled by default)
21//! - `form` - Form data handling via serde_urlencoded (enabled by default)
22//! - `fs` - File upload support with MIME type detection
23//! - `mime` - MIME type parsing and manipulation
24//! - `http_body` - Implementation of http_body traits
25//! - `std` - Enable standard library support (enabled by default)
26//!
27//! # Examples
28//!
29//! ## Basic Request/Response Handling
30//!
31//! ```rust
32//! use http_kit::{Request, Response, Result};
33//!
34//! async fn echo_handler(mut request: Request) -> Result<Response> {
35//!     let body = request.take_body()?;
36//!     Ok(Response::new(200, body))
37//! }
38//!
39//! # async fn example() -> Result<()> {
40//! let mut request = Request::get("/echo");
41//! request.replace_body("Hello, world!");
42//! let response = echo_handler(request).await?;
43//! # Ok(())
44//! # }
45//! ```
46//!
47//! ## JSON Handling
48//!
49//! ```rust
50//! # #[cfg(feature = "json")]
51//! # {
52//! use http_kit::{Request, Response, Result};
53//! use serde::{Deserialize, Serialize};
54//!
55//! #[derive(Serialize, Deserialize)]
56//! struct User {
57//!     name: String,
58//!     email: String,
59//! }
60//!
61//! async fn create_user(mut request: Request) -> Result<Response> {
62//!     let user: User = request.into_json().await?;
63//!     // Process user...
64//!     Ok(Response::empty().json(&user)?)
65//! }
66//! # }
67//! ```
68//!
69//! ## Middleware Usage
70//!
71//! ```rust
72//! use http_kit::{Request, Response, Result, Middleware, Endpoint};
73//!
74//! struct LoggingMiddleware;
75//!
76//! impl Middleware for LoggingMiddleware {
77//!     async fn handle(&self, request: &mut Request, next: impl Endpoint) -> Result<Response> {
78//!         println!("Request: {} {}", request.method(), request.uri());
79//!         let response = next.respond(request).await?;
80//!         println!("Response: {}", response.status());
81//!         Ok(response)
82//!     }
83//! }
84//! ```
85//!
86extern crate alloc;
87
88#[macro_use]
89mod macros;
90
91mod error;
92pub use error::{Error, Result, ResultExt};
93
94mod body;
95
96#[cfg(feature = "fs")]
97pub(crate) mod mime_guess;
98pub use body::Body;
99pub use body::Error as BodyError;
100
101pub mod middleware;
102#[doc(inline)]
103pub use middleware::Middleware;
104
105pub mod endpoint;
106#[doc(inline)]
107pub use endpoint::Endpoint;
108
109mod request;
110pub mod utils;
111pub use request::Request;
112mod response;
113pub use response::Response;
114
115#[cfg(feature = "cookie")]
116pub use cookie;
117
118pub use http::{header, method, uri, version, Extensions, Method, StatusCode, Uri, Version};