mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-30 00:00:32 +02:00
Changes to UrlParamsMap
This commit is contained in:
@@ -52,7 +52,7 @@ struct State {
|
|||||||
|
|
||||||
async fn get(
|
async fn get(
|
||||||
_req: Request<Body>,
|
_req: Request<Body>,
|
||||||
params: extract::UrlParams,
|
params: extract::UrlParamsMap,
|
||||||
state: extract::Extension<SharedState>,
|
state: extract::Extension<SharedState>,
|
||||||
) -> Result<Bytes, Error> {
|
) -> Result<Bytes, Error> {
|
||||||
let state = state.into_inner();
|
let state = state.into_inner();
|
||||||
@@ -69,7 +69,7 @@ async fn get(
|
|||||||
|
|
||||||
async fn set(
|
async fn set(
|
||||||
_req: Request<Body>,
|
_req: Request<Body>,
|
||||||
params: extract::UrlParams,
|
params: extract::UrlParamsMap,
|
||||||
value: extract::BytesMaxLength<{ 1024 * 5_000 }>, // ~5mb
|
value: extract::BytesMaxLength<{ 1024 * 5_000 }>, // ~5mb
|
||||||
state: extract::Extension<SharedState>,
|
state: extract::Extension<SharedState>,
|
||||||
) -> Result<response::Empty, Error> {
|
) -> Result<response::Empty, Error> {
|
||||||
|
|||||||
+5
-1
@@ -39,6 +39,9 @@ pub enum Error {
|
|||||||
#[error("response failed with status {0}")]
|
#[error("response failed with status {0}")]
|
||||||
Status(StatusCode),
|
Status(StatusCode),
|
||||||
|
|
||||||
|
#[error("invalid URL param. Expected something of type `{type_name}`")]
|
||||||
|
InvalidUrlParam { type_name: &'static str },
|
||||||
|
|
||||||
#[error("unknown URL param `{0}`")]
|
#[error("unknown URL param `{0}`")]
|
||||||
UnknownUrlParam(String),
|
UnknownUrlParam(String),
|
||||||
}
|
}
|
||||||
@@ -65,7 +68,8 @@ where
|
|||||||
match error {
|
match error {
|
||||||
Error::DeserializeRequestBody(_)
|
Error::DeserializeRequestBody(_)
|
||||||
| Error::QueryStringMissing
|
| Error::QueryStringMissing
|
||||||
| Error::DeserializeQueryString(_) => make_response(StatusCode::BAD_REQUEST),
|
| Error::DeserializeQueryString(_)
|
||||||
|
| Error::InvalidUrlParam { .. } => make_response(StatusCode::BAD_REQUEST),
|
||||||
|
|
||||||
Error::Status(status) => make_response(status),
|
Error::Status(status) => make_response(status),
|
||||||
|
|
||||||
|
|||||||
+12
-3
@@ -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> {
|
pub fn get(&self, key: &str) -> Result<&str, Error> {
|
||||||
if let Some(value) = self.0.get(key) {
|
if let Some(value) = self.0.get(key) {
|
||||||
Ok(value)
|
Ok(value)
|
||||||
@@ -194,9 +194,18 @@ impl UrlParams {
|
|||||||
Err(Error::UnknownUrlParam(key.to_string()))
|
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>>;
|
type Future = future::Ready<Result<Self, Error>>;
|
||||||
|
|
||||||
fn from_request(req: &mut Request<Body>) -> Self::Future {
|
fn from_request(req: &mut Request<Body>) -> Self::Future {
|
||||||
|
|||||||
Reference in New Issue
Block a user