More docs and expand key_value_store example

This commit is contained in:
David Pedersen
2021-06-08 12:43:16 +02:00
parent d7605d3184
commit 1f8b39f05d
13 changed files with 920 additions and 431 deletions
+10 -10
View File
@@ -44,8 +44,8 @@ where
type Rejection = QueryStringMissing;
async fn from_request(req: &mut Request<Body>) -> Result<Self, Self::Rejection> {
let query = req.uri().query().ok_or(QueryStringMissing(()))?;
let value = serde_urlencoded::from_str(query).map_err(|_| QueryStringMissing(()))?;
let query = req.uri().query().ok_or(QueryStringMissing)?;
let value = serde_urlencoded::from_str(query).map_err(|_| QueryStringMissing)?;
Ok(Query(value))
}
}
@@ -75,7 +75,7 @@ where
Ok(Json(value))
} else {
Err(MissingJsonContentType(()).into_response())
Err(MissingJsonContentType.into_response())
}
}
}
@@ -110,7 +110,7 @@ where
let value = req
.extensions()
.get::<T>()
.ok_or(MissingExtension(()))
.ok_or(MissingExtension)
.map(|x| x.clone())?;
Ok(Extension(value))
@@ -179,10 +179,10 @@ impl<const N: u64> FromRequest for BytesMaxLength<N> {
if let Some(length) = content_length {
if length > N {
return Err(PayloadTooLarge(()).into_response());
return Err(PayloadTooLarge.into_response());
}
} else {
return Err(LengthRequired(()).into_response());
return Err(LengthRequired.into_response());
};
let bytes = hyper::body::to_bytes(body)
@@ -221,7 +221,7 @@ impl FromRequest for UrlParamsMap {
let params = params.take().expect("params already taken").0;
Ok(Self(params.into_iter().collect()))
} else {
Err(MissingRouteParams(()))
Err(MissingRouteParams)
}
}
}
@@ -248,7 +248,7 @@ macro_rules! impl_parse_url {
{
params.take().expect("params already taken").0
} else {
return Err(MissingRouteParams(()).into_response())
return Err(MissingRouteParams.into_response())
};
if let [(_, $head), $((_, $tail),)*] = &*params {
@@ -268,7 +268,7 @@ macro_rules! impl_parse_url {
Ok(UrlParams(($head, $($tail,)*)))
} else {
return Err(MissingRouteParams(()).into_response())
return Err(MissingRouteParams.into_response())
}
}
}
@@ -283,7 +283,7 @@ fn take_body(req: &mut Request<Body>) -> Result<Body, BodyAlreadyTaken> {
struct BodyAlreadyTakenExt;
if req.extensions_mut().insert(BodyAlreadyTakenExt).is_some() {
Err(BodyAlreadyTaken(()))
Err(BodyAlreadyTaken)
} else {
let body = std::mem::take(req.body_mut());
Ok(body)
+10 -9
View File
@@ -8,11 +8,12 @@ macro_rules! define_rejection {
#[status = $status:ident]
#[body = $body:expr]
$(#[$m:meta])*
pub struct $name:ident (());
pub struct $name:ident;
) => {
$(#[$m])*
#[derive(Debug)]
pub struct $name(pub(super) ());
#[non_exhaustive]
pub struct $name;
impl IntoResponse for $name {
fn into_response(self) -> http::Response<Body> {
@@ -57,7 +58,7 @@ define_rejection! {
#[status = BAD_REQUEST]
#[body = "Query string was invalid or missing"]
/// Rejection type for [`Query`](super::Query).
pub struct QueryStringMissing(());
pub struct QueryStringMissing;
}
define_rejection! {
@@ -72,7 +73,7 @@ define_rejection! {
#[body = "Expected request with `Content-Type: application/json`"]
/// Rejection type for [`Json`](super::Json) used if the `Content-Type`
/// header is missing.
pub struct MissingJsonContentType(());
pub struct MissingJsonContentType;
}
define_rejection! {
@@ -80,7 +81,7 @@ define_rejection! {
#[body = "Missing request extension"]
/// Rejection type for [`Extension`](super::Extension) if an expected
/// request extension was not found.
pub struct MissingExtension(());
pub struct MissingExtension;
}
define_rejection! {
@@ -104,7 +105,7 @@ define_rejection! {
#[body = "Request payload is too large"]
/// Rejection type for [`BytesMaxLength`](super::BytesMaxLength) if the
/// request body is too large.
pub struct PayloadTooLarge(());
pub struct PayloadTooLarge;
}
define_rejection! {
@@ -112,7 +113,7 @@ define_rejection! {
#[body = "Content length header is required"]
/// Rejection type for [`BytesMaxLength`](super::BytesMaxLength) if the
/// request is missing the `Content-Length` header or it is invalid.
pub struct LengthRequired(());
pub struct LengthRequired;
}
define_rejection! {
@@ -121,7 +122,7 @@ define_rejection! {
/// Rejection type for [`UrlParamsMap`](super::UrlParamsMap) and
/// [`UrlParams`](super::UrlParams) if you try and extract the URL params
/// more than once.
pub struct MissingRouteParams(());
pub struct MissingRouteParams;
}
define_rejection! {
@@ -129,7 +130,7 @@ define_rejection! {
#[body = "Cannot have two request body extractors for a single handler"]
/// Rejection type used if you try and extract the request body more than
/// once.
pub struct BodyAlreadyTaken(());
pub struct BodyAlreadyTaken;
}
/// Rejection type for [`UrlParams`](super::UrlParams) if the capture route