Implement path extractor (#124)

Fixes #42
This commit is contained in:
Sunli
2021-08-06 10:17:57 +02:00
committed by GitHub
parent 2be79168d8
commit 9fdbd42fba
14 changed files with 871 additions and 66 deletions
+30
View File
@@ -159,6 +159,25 @@ impl IntoResponse for InvalidUrlParam {
}
}
/// Rejection type for [`Path`](super::Path) if the capture route
/// param didn't have the expected type.
#[derive(Debug)]
pub struct InvalidPathParam(String);
impl InvalidPathParam {
pub(super) fn new(err: impl Into<String>) -> Self {
InvalidPathParam(err.into())
}
}
impl IntoResponse for InvalidPathParam {
fn into_response(self) -> http::Response<Body> {
let mut res = http::Response::new(Body::from(format!("Invalid URL param. {}", self.0)));
*res.status_mut() = http::StatusCode::BAD_REQUEST;
res
}
}
/// Rejection type for extractors that deserialize query strings if the input
/// couldn't be deserialized into the target type.
#[derive(Debug)]
@@ -254,6 +273,17 @@ composite_rejection! {
}
}
composite_rejection! {
/// Rejection used for [`Path`](super::Path).
///
/// Contains one variant for each way the [`Path`](super::Path) extractor
/// can fail.
pub enum PathParamsRejection {
InvalidPathParam,
MissingRouteParams,
}
}
composite_rejection! {
/// Rejection used for [`Bytes`](bytes::Bytes).
///