use axum::{ extract::{FromRequest, FromRequestParts, Request}, http::{Method, request::Parts}, }; use serde::de::DeserializeOwned; use crate::WebError; /// An extractor which deserializes a struct from a POST request's body. /// For GET requests the struct will be None. #[derive(Debug, Clone, Copy, Default)] #[must_use] pub(crate) struct PostForm(pub Option); impl FromRequest for PostForm where T: DeserializeOwned, S: Send + Sync, { type Rejection = WebError; async fn from_request(req: Request, state: &S) -> Result { if req.method() == Method::POST { let axum::Form(data) = axum::Form::from_request(req, state).await?; Ok(Self(Some(data))) } else { Ok(Self(None)) } } } /// An extractor which wraps another extractor and converts its errors into /// `WebError`s. pub(crate) struct Expect(pub E); impl FromRequestParts for Expect where E: FromRequestParts, WebError: From, S: Send + Sync, { type Rejection = WebError; async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { Ok(Self(E::from_request_parts(parts, state).await?)) } }