mincat_core/
request.rs

1use crate::{
2    body::{Body, BodyLimitedSize},
3    response::IntoResponse,
4};
5
6pub use http::request::Parts;
7pub use http_body_util::Limited;
8
9pub type Request<T = Body> = http::Request<T>;
10
11pub trait RequestExt {
12    fn change_to_limited_body(self) -> Self;
13}
14
15impl RequestExt for Request {
16    fn change_to_limited_body(self) -> Self {
17        match self.extensions().get::<BodyLimitedSize>().copied() {
18            Some(BodyLimitedSize(size)) => self.map(|body| Body::new(Limited::new(body, size))),
19            None => self,
20        }
21    }
22}
23
24#[async_trait::async_trait]
25pub trait FromRequest: Sized {
26    type Error: IntoResponse;
27
28    async fn from_request(request: Request) -> Result<Self, Self::Error>;
29}
30
31#[async_trait::async_trait]
32impl<T> FromRequest for T
33where
34    T: FromRequestParts,
35{
36    type Error = <Self as FromRequestParts>::Error;
37
38    async fn from_request(req: Request) -> Result<Self, Self::Error> {
39        let (mut parts, _) = req.into_parts();
40        Self::from_request_parts(&mut parts).await
41    }
42}
43
44#[async_trait::async_trait]
45pub trait FromRequestParts: Sized {
46    type Error: IntoResponse;
47
48    async fn from_request_parts(parts: &mut Parts) -> Result<Self, Self::Error>;
49}