FromRequest

Trait FromRequest 

Source
pub trait FromRequest: Sized {
    type Error: IntoResponse;

    // Required method
    fn from_request(req: &Request) -> Result<Self, Self::Error>;
}
Expand description

Core trait for extracting typed data from HTTP requests.

Types implementing this trait can be used as handler parameters, enabling automatic extraction and validation of request data. The framework will automatically call from_request for each extractor parameter before invoking the handler function.

§Type Parameters

The trait has an associated Error type that must implement Into<ExtractionError>, allowing custom error handling for failed extractions.

§Examples

§Implementing a Custom Extractor

use ignitia::handler::extractor::FromRequest;
use ignitia::Request;

struct ApiKey(String);

impl FromRequest for ApiKey {
    type Error = ExtractionError;

    fn from_request(req: &Request) -> Result<Self, Self::Error> {
        req.header("x-api-key")
            .map(|key| ApiKey(key.to_string()))
            .ok_or_else(|| ExtractionError::unauthorized("Missing API key"))
    }
}

async fn protected_handler(ApiKey(key): ApiKey) -> String {
    format!("Authenticated with key: {}", key)
}

Required Associated Types§

Source

type Error: IntoResponse

The error type returned when extraction fails.

Required Methods§

Source

fn from_request(req: &Request) -> Result<Self, Self::Error>

Extract this type from an HTTP request.

§Arguments
  • req - Reference to the HTTP request
§Returns

Returns Ok(Self) if extraction succeeds, or Err(Self::Error) if it fails.

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.

Implementors§

Source§

impl FromRequest for Multipart

Source§

impl FromRequest for Body

Source§

impl FromRequest for Cookies

Source§

impl FromRequest for Headers

Source§

impl FromRequest for Method

Source§

impl FromRequest for Uri

Source§

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

Extension extractor implementation.

This allows extracting request extensions that were previously set by middleware or other parts of the application.

Source§

impl<T> FromRequest for Form<T>

Source§

impl<T> FromRequest for Json<T>

Source§

impl<T> FromRequest for Path<T>

Source§

impl<T> FromRequest for Query<T>

Source§

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