tako/types.rs
1//! Core type definitions and aliases used throughout the Tako framework.
2//!
3//! This module provides fundamental type aliases that standardize the types used across
4//! the framework for requests, responses, errors, and middleware. These aliases ensure
5//! consistency and make the API more ergonomic by hiding complex generic parameters.
6//! The main types include `Request` and `Response` for HTTP handling, and `BoxMiddleware`
7//! for middleware function composition.
8//!
9//! # Examples
10//!
11//! ```rust
12//! use tako::types::{Request, Response, BoxMiddleware};
13//! use tako::middleware::Next;
14//! use std::sync::Arc;
15//!
16//! // Using the Request type in a handler
17//! async fn handler(req: Request) -> Response {
18//! Response::new(tako::body::TakoBody::from("Hello, World!"))
19//! }
20//!
21//! // Creating middleware using BoxMiddleware
22//! let middleware: BoxMiddleware = Arc::new(|req, next| {
23//! Box::pin(async move {
24//! println!("Request to: {}", req.uri());
25//! next.run(req).await
26//! })
27//! });
28//! ```
29
30use std::sync::Arc;
31
32use bytes::Bytes;
33use futures_util::future::BoxFuture;
34use http_body_util::combinators::UnsyncBoxBody;
35
36use crate::{body::TakoBody, middleware::Next};
37
38/// HTTP request type with streaming body support (hyper-independent).
39pub type Request = http::Request<TakoBody>;
40
41/// HTTP response type with Tako's custom body implementation.
42pub type Response = http::Response<TakoBody>;
43
44/// Boxed HTTP body type for internal response handling.
45pub(crate) type BoxBody = UnsyncBoxBody<Bytes, BoxError>;
46
47/// Boxed error type for thread-safe error handling.
48pub(crate) type BoxError = Box<dyn std::error::Error + Send + Sync>;
49
50/// Boxed middleware function type for dynamic middleware composition.
51pub type BoxMiddleware = Arc<dyn Fn(Request, Next) -> BoxFuture<'static, Response> + Send + Sync>;