1
0
Fork 0
mirror of https://github.com/tokio-rs/axum.git synced 2025-03-27 00:48:44 +01:00

Changes to UrlParamsMap

This commit is contained in:
David Pedersen 2021-05-30 16:37:27 +02:00
parent 763d4e8d21
commit 03fb15e7a7
3 changed files with 19 additions and 6 deletions

View file

@ -52,7 +52,7 @@ struct State {
async fn get(
_req: Request<Body>,
params: extract::UrlParams,
params: extract::UrlParamsMap,
state: extract::Extension<SharedState>,
) -> Result<Bytes, Error> {
let state = state.into_inner();
@ -69,7 +69,7 @@ async fn get(
async fn set(
_req: Request<Body>,
params: extract::UrlParams,
params: extract::UrlParamsMap,
value: extract::BytesMaxLength<{ 1024 * 5_000 }>, // ~5mb
state: extract::Extension<SharedState>,
) -> Result<response::Empty, Error> {

View file

@ -39,6 +39,9 @@ pub enum Error {
#[error("response failed with status {0}")]
Status(StatusCode),
#[error("invalid URL param. Expected something of type `{type_name}`")]
InvalidUrlParam { type_name: &'static str },
#[error("unknown URL param `{0}`")]
UnknownUrlParam(String),
}
@ -65,7 +68,8 @@ where
match error {
Error::DeserializeRequestBody(_)
| Error::QueryStringMissing
| Error::DeserializeQueryString(_) => make_response(StatusCode::BAD_REQUEST),
| Error::DeserializeQueryString(_)
| Error::InvalidUrlParam { .. } => make_response(StatusCode::BAD_REQUEST),
Error::Status(status) => make_response(status),

View file

@ -184,9 +184,9 @@ impl<const N: u64> FromRequest for BytesMaxLength<N> {
}
}
pub struct UrlParams(HashMap<String, String>);
pub struct UrlParamsMap(HashMap<String, String>);
impl UrlParams {
impl UrlParamsMap {
pub fn get(&self, key: &str) -> Result<&str, Error> {
if let Some(value) = self.0.get(key) {
Ok(value)
@ -194,9 +194,18 @@ impl UrlParams {
Err(Error::UnknownUrlParam(key.to_string()))
}
}
pub fn get_typed<T>(&self, key: &str) -> Result<T, Error>
where
T: std::str::FromStr,
{
self.get(key)?.parse().map_err(|_| Error::InvalidUrlParam {
type_name: std::any::type_name::<T>(),
})
}
}
impl FromRequest for UrlParams {
impl FromRequest for UrlParamsMap {
type Future = future::Ready<Result<Self, Error>>;
fn from_request(req: &mut Request<Body>) -> Self::Future {