use crate::extract::Query;
use crate::extract::from_request::FromRequest;
use crate::{OptionReqBody, PathParams, RequestContext};
use micro_http::protocol::ParseError;
use serde::Deserialize;
impl<T> FromRequest for Query<T>
where
T: for<'de> Deserialize<'de> + Send,
{
type Output<'r> = T;
type Error = ParseError;
async fn from_request<'r>(req: &'r RequestContext<'_, '_>, _body: OptionReqBody) -> Result<Self::Output<'r>, Self::Error> {
let query = req.uri().query().ok_or_else(|| ParseError::invalid_header("has no query string, path"))?;
serde_qs::from_str::<'_, T>(query).map_err(|e| ParseError::invalid_header(e.to_string()))
}
}
impl FromRequest for &PathParams<'_, '_> {
type Output<'r> = &'r PathParams<'r, 'r>;
type Error = ParseError;
async fn from_request<'r>(req: &'r RequestContext<'_, '_>, _body: OptionReqBody) -> Result<Self::Output<'r>, Self::Error> {
Ok(req.path_params())
}
}