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, Body};
33//!
34//! async fn echo_handler(mut request: Request) -> Result<Response> {
35//! let body = std::mem::replace(request.body_mut(), Body::empty());
36//! Ok(Response::new(body))
37//! }
38//!
39//! # async fn example() -> Result<()> {
40//! let mut request = Request::new(Body::from_bytes("Hello, world!"));
41//! let response = echo_handler(request).await?;
42//! # Ok(())
43//! # }
44//! ```
45//!
46//! ## JSON Handling
47//!
48//! ```rust
49//! # #[cfg(feature = "json")]
50//! # {
51//! use http_kit::{Request, Response, Result, Body};
52//! use serde::{Deserialize, Serialize};
53//!
54//! #[derive(Serialize, Deserialize)]
55//! struct User {
56//! name: String,
57//! email: String,
58//! }
59//!
60//! async fn create_user(mut request: Request) -> Result<Response> {
61//! let user: User = request.body_mut().into_json().await?;
62//! // Process user...
63//! let response_body = Body::from_json(&user)?;
64//! Ok(Response::new(response_body))
65//! }
66//! # }
67//! ```
68//!
69//! ## Middleware Usage
70//!
71//! ```rust
72//! use http_kit::{Request, Response, Result, Middleware, Endpoint, Body, Error};
73//! use http_kit::middleware::MiddlewareError;
74//!
75//! struct LoggingMiddleware;
76//!
77//! impl Middleware for LoggingMiddleware {
78//! type Error = Error;
79//! async fn handle<E: Endpoint>(&mut self, request: &mut Request, mut next: E) -> Result<Response, MiddlewareError<E::Error, Self::Error>> {
80//! println!("Request: {} {}", request.method(), request.uri());
81//! let response = next.respond(request).await.map_err(MiddlewareError::Endpoint)?;
82//! println!("Response: {}", response.status());
83//! Ok(response)
84//! }
85//! }
86//! ```
87//!
88extern crate alloc;
89
90#[macro_use]
91mod macros;
92
93pub mod sse;
94
95pub mod error;
96pub use error::{Error, HttpError, Result, ResultExt};
97mod body;
98
99pub use body::Body;
100pub use body::Error as BodyError;
101
102pub mod middleware;
103#[doc(inline)]
104pub use middleware::Middleware;
105
106pub mod endpoint;
107#[doc(inline)]
108pub use endpoint::Endpoint;
109
110pub mod utils;
111/// A type alias for HTTP requests with a custom `Body` type.
112pub type Request = http::Request<Body>;
113/// A type alias for HTTP responses with a custom `Body` type.
114pub type Response = http::Response<Body>;
115
116#[cfg(feature = "cookie")]
117pub use cookie;
118
119pub use http::{header, method, uri, version, Extensions, Method, StatusCode, Uri, Version};