Skip to main content

FromRequest

Trait FromRequest 

Source
pub trait FromRequest: Sized {
    type Rejection: Into<Response> + Send + 'static;

    // Required method
    fn from_request<'life0, 'async_trait>(
        req: &'life0 mut Request,
    ) -> Pin<Box<dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

FromRequest 是萃取器的核心 trait,用于从 HTTP 请求中提取特定类型的数据。

通过实现这个 trait,您可以创建自定义的萃取器,从请求中提取任何需要的数据。 所有内置萃取器(Path、Query、Json 等)都实现了这个 trait。

§基本用法

要实现一个自定义萃取器,您需要:

  1. 定义您的数据类型
  2. 实现 FromRequest trait
  3. 在处理函数中使用萃取器

§示例:创建 JWT 令牌萃取器

use async_trait::async_trait;
use silent::extractor::FromRequest;
use silent::{Request, Result, SilentError};

struct JwtToken(String);

#[async_trait]
impl FromRequest for JwtToken {
    type Rejection = SilentError;

    async fn from_request(req: &mut Request) -> std::result::Result<Self, Self::Rejection> {
        let token = req.headers()
            .get("authorization")
            .and_then(|v| v.to_str().ok())
            .and_then(|s| s.strip_prefix("Bearer "))
            .map(|s| s.to_string())
            .ok_or(SilentError::ParamsNotFound)?;

        Ok(JwtToken(token))
    }
}

// 使用自定义萃取器
async fn protected_handler(token: JwtToken) -> Result<String> {
    Ok(format!("访问受保护的资源,Token: {}", token.0))
}

§错误处理

FromRequestRejection 类型决定了萃取失败时的错误类型。常用的错误类型:

  • SilentError:框架内置错误,包含 ParamsNotFoundParamsEmpty
  • Response:直接返回 HTTP 响应

§组合使用

多个萃取器可以组合使用:

use silent::Result;
use silent::extractor::{Path, Query, Json};
use serde::Deserialize;

#[derive(Deserialize)]
struct Page {
    page: u32,
    size: u32,
}

#[derive(Deserialize)]
struct Data {
    name: String,
}

async fn handler(
    (Path(id), Query(p), Json(data)): (Path<i64>, Query<Page>, Json<Data>),
) -> Result<String> {
    // 处理提取的数据
    Ok("成功".to_string())
}

§可选参数

使用 Option<T> 可以处理可选参数:

use silent::Result;
use silent::extractor::Path;

async fn handler(opt_id: Option<Path<i64>>) -> Result<String> {
    match opt_id {
        Some(Path(id)) => Ok(format!("ID: {}", id)),
        None => Ok("无ID".to_string()),
    }
}

Required Associated Types§

Source

type Rejection: Into<Response> + Send + 'static

萃取失败时的错误类型

这个类型必须能够转换为 HTTP 响应(实现了 Into<Response>

Required Methods§

Source

fn from_request<'life0, 'async_trait>( req: &'life0 mut Request, ) -> Pin<Box<dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

从请求中提取数据

§参数
  • req - 可变的请求引用,可以从中提取数据
§返回值

返回 Result<Self, Self::Rejection>

  • 成功时返回 Ok(extracted_value)
  • 失败时返回 Err(error)
§示例

参见上面 FromRequest trait 的完整示例。

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<A> FromRequest for (A,)
where A: FromRequest + Send + 'static,

Source§

type Rejection = Response

Source§

fn from_request<'life0, 'async_trait>( req: &'life0 mut Request, ) -> Pin<Box<dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

impl<A, B> FromRequest for (A, B)
where A: FromRequest + Send + 'static, B: FromRequest + Send + 'static,

Source§

type Rejection = Response

Source§

fn from_request<'life0, 'async_trait>( req: &'life0 mut Request, ) -> Pin<Box<dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

impl<A, B, C> FromRequest for (A, B, C)
where A: FromRequest + Send + 'static, B: FromRequest + Send + 'static, C: FromRequest + Send + 'static,

Source§

type Rejection = Response

Source§

fn from_request<'life0, 'async_trait>( req: &'life0 mut Request, ) -> Pin<Box<dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

impl<A, B, C, D> FromRequest for (A, B, C, D)
where A: FromRequest + Send + 'static, B: FromRequest + Send + 'static, C: FromRequest + Send + 'static, D: FromRequest + Send + 'static,

Source§

type Rejection = Response

Source§

fn from_request<'life0, 'async_trait>( req: &'life0 mut Request, ) -> Pin<Box<dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

impl<T> FromRequest for Option<T>
where T: FromRequest + Send + 'static,

Source§

type Rejection = Response

Source§

fn from_request<'life0, 'async_trait>( req: &'life0 mut Request, ) -> Pin<Box<dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

impl<T> FromRequest for Result<T, Response>
where T: FromRequest + Send + 'static,

Source§

type Rejection = Response

Source§

fn from_request<'life0, 'async_trait>( req: &'life0 mut Request, ) -> Pin<Box<dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Implementors§

Source§

impl FromRequest for Method

Source§

impl FromRequest for RemoteAddr

Source§

impl FromRequest for Uri

Source§

impl FromRequest for Version

Source§

impl<H> FromRequest for TypedHeader<H>
where H: Header + Send + 'static,

Source§

impl<T> FromRequest for Configs<T>
where T: Send + Sync + Clone + 'static,

Source§

impl<T> FromRequest for Extension<T>
where T: Clone + Send + Sync + 'static,

Source§

impl<T> FromRequest for Form<T>
where for<'de> T: Deserialize<'de> + Serialize + Send + 'static,

Source§

impl<T> FromRequest for Json<T>
where for<'de> T: Deserialize<'de> + Send + 'static,

Source§

impl<T> FromRequest for Path<T>
where for<'de> T: Deserialize<'de> + Send + 'static,

Source§

impl<T> FromRequest for Query<T>
where for<'de> T: Deserialize<'de> + Send + 'static,