Skip to main content

wae_https/extract/
mod.rs

1#![doc = include_str!("readme.md")]
2
3use http::{Method, Uri, Version, header::HeaderMap};
4use std::fmt;
5
6/// Extractor 错误类型
7///
8/// 统一封装所有提取器可能产生的错误,便于错误处理和响应。
9#[derive(Debug)]
10pub enum ExtractorError {
11    /// 路径参数提取错误
12    PathRejection(String),
13
14    /// 查询参数提取错误
15    QueryRejection(String),
16
17    /// JSON 提取错误
18    JsonRejection(String),
19
20    /// 表单数据提取错误
21    FormRejection(String),
22
23    /// 扩展数据提取错误
24    ExtensionRejection(String),
25
26    /// Host 提取错误
27    HostRejection(String),
28
29    /// TypedHeader 提取错误
30    TypedHeaderRejection(String),
31
32    /// Cookie 提取错误
33    CookieRejection(String),
34
35    /// Multipart 提取错误
36    MultipartRejection(String),
37
38    /// WebSocketUpgrade 提取错误
39    WebSocketUpgradeRejection(String),
40
41    /// Bytes 提取错误
42    BytesRejection(String),
43
44    /// Stream 提取错误
45    StreamRejection(String),
46
47    /// 自定义错误消息
48    Custom(String),
49}
50
51impl fmt::Display for ExtractorError {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        match self {
54            ExtractorError::PathRejection(e) => write!(f, "Path extraction failed: {}", e),
55            ExtractorError::QueryRejection(e) => write!(f, "Query extraction failed: {}", e),
56            ExtractorError::JsonRejection(e) => write!(f, "Json extraction failed: {}", e),
57            ExtractorError::FormRejection(e) => write!(f, "Form extraction failed: {}", e),
58            ExtractorError::ExtensionRejection(e) => write!(f, "Extension extraction failed: {}", e),
59            ExtractorError::HostRejection(e) => write!(f, "Host extraction failed: {}", e),
60            ExtractorError::TypedHeaderRejection(e) => write!(f, "TypedHeader extraction failed: {}", e),
61            ExtractorError::CookieRejection(e) => write!(f, "Cookie extraction failed: {}", e),
62            ExtractorError::MultipartRejection(e) => write!(f, "Multipart extraction failed: {}", e),
63            ExtractorError::WebSocketUpgradeRejection(e) => write!(f, "WebSocketUpgrade extraction failed: {}", e),
64            ExtractorError::BytesRejection(e) => write!(f, "Bytes extraction failed: {}", e),
65            ExtractorError::StreamRejection(e) => write!(f, "Stream extraction failed: {}", e),
66            ExtractorError::Custom(msg) => write!(f, "Extractor error: {}", msg),
67        }
68    }
69}
70
71impl std::error::Error for ExtractorError {}
72
73/// 请求头提取器
74///
75/// 用于从请求中提取指定名称的请求头值。
76///
77/// # 示例
78///
79/// ```ignore
80/// use wae_https::extract::Header;
81///
82/// async fn handler(Header(content_type): Header<String>) -> String {
83///     content_type
84/// }
85/// ```
86#[derive(Debug, Clone)]
87pub struct Header<T>(pub T);
88
89/// HTTP 方法提取器
90///
91/// 获取当前请求的 HTTP 方法(GET、POST、PUT、DELETE 等)。
92pub type HttpMethod = Method;
93
94/// 请求 URI 提取器
95///
96/// 获取当前请求的完整 URI。
97pub type RequestUri = Uri;
98
99/// HTTP 版本提取器
100///
101/// 获取当前请求的 HTTP 版本(HTTP/1.0、HTTP/1.1、HTTP/2.0 等)。
102pub type HttpVersion = Version;
103
104/// 请求头映射提取器
105///
106/// 获取所有请求头的键值对映射。
107pub type Headers = HeaderMap;
108
109/// 扩展数据提取器
110#[derive(Debug, Clone)]
111pub struct Extension<T>(pub T);
112
113/// 表单数据提取器
114#[derive(Debug, Clone, serde::Deserialize)]
115pub struct Form<T>(pub T);
116
117/// JSON 提取器
118#[derive(Debug, Clone, serde::Deserialize)]
119pub struct Json<T>(pub T);
120
121/// 多部分表单数据提取器
122#[derive(Debug, Clone)]
123pub struct Multipart;
124
125/// 原始 URI 提取器
126pub type OriginalUri = http::Uri;
127
128/// 路径参数提取器
129#[derive(Debug, Clone)]
130pub struct Path<T>(pub T);
131
132/// 查询参数提取器
133#[derive(Debug, Clone, serde::Deserialize)]
134pub struct Query<T>(pub T);
135
136/// 状态提取器
137#[derive(Debug, Clone)]
138pub struct State<T>(pub T);
139
140/// WebSocket 升级提取器
141#[derive(Debug, Clone)]
142pub struct WebSocketUpgrade;
143
144pub use bytes::Bytes;
145
146/// 流式请求体提取器
147///
148/// 用于从请求中提取流式数据。
149pub type Stream = crate::Body;