Skip to main content

wae_https/extract/
mod.rs

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