viz_core/
from_request.rs

1//! Extracts data from the [`Request`] by types.
2
3use crate::{Future, IntoResponse, Request};
4
5/// An interface for extracting data from the HTTP [`Request`].
6pub trait FromRequest: Sized {
7    /// The type returned in the event of a conversion error.
8    type Error: IntoResponse;
9
10    /// Extracts this type from the HTTP [`Request`].
11    #[must_use]
12    fn extract(req: &mut Request) -> impl Future<Output = Result<Self, Self::Error>> + Send;
13}
14
15impl<T> FromRequest for Option<T>
16where
17    T: FromRequest,
18{
19    type Error = std::convert::Infallible;
20
21    async fn extract(req: &mut Request) -> Result<Self, Self::Error> {
22        Ok(T::extract(req).await.ok())
23    }
24}
25
26impl<T> FromRequest for Result<T, T::Error>
27where
28    T: FromRequest,
29{
30    type Error = std::convert::Infallible;
31
32    async fn extract(req: &mut Request) -> Result<Self, Self::Error> {
33        Ok(T::extract(req).await)
34    }
35}