fix examples

This commit is contained in:
David Pedersen 2022-06-26 19:21:59 +02:00
parent 069b660a76
commit cc07b8dbd0
15 changed files with 49 additions and 49 deletions

View file

@ -1,6 +1,6 @@
//! Additional types for defining routes.
use axum::{handler::Handler, Router};
use axum::Router;
mod resource;
@ -32,7 +32,7 @@ pub trait RouterExt<B>: sealed::Sealed {
#[cfg(feature = "typed-routing")]
fn typed_get<H, T, P>(self, handler: H) -> Self
where
H: Handler<T, B>,
H: axum::handler::Handler<T, B>,
T: FirstElementIs<P> + 'static,
P: TypedPath;
@ -45,7 +45,7 @@ pub trait RouterExt<B>: sealed::Sealed {
#[cfg(feature = "typed-routing")]
fn typed_delete<H, T, P>(self, handler: H) -> Self
where
H: Handler<T, B>,
H: axum::handler::Handler<T, B>,
T: FirstElementIs<P> + 'static,
P: TypedPath;
@ -58,7 +58,7 @@ pub trait RouterExt<B>: sealed::Sealed {
#[cfg(feature = "typed-routing")]
fn typed_head<H, T, P>(self, handler: H) -> Self
where
H: Handler<T, B>,
H: axum::handler::Handler<T, B>,
T: FirstElementIs<P> + 'static,
P: TypedPath;
@ -71,7 +71,7 @@ pub trait RouterExt<B>: sealed::Sealed {
#[cfg(feature = "typed-routing")]
fn typed_options<H, T, P>(self, handler: H) -> Self
where
H: Handler<T, B>,
H: axum::handler::Handler<T, B>,
T: FirstElementIs<P> + 'static,
P: TypedPath;
@ -84,7 +84,7 @@ pub trait RouterExt<B>: sealed::Sealed {
#[cfg(feature = "typed-routing")]
fn typed_patch<H, T, P>(self, handler: H) -> Self
where
H: Handler<T, B>,
H: axum::handler::Handler<T, B>,
T: FirstElementIs<P> + 'static,
P: TypedPath;
@ -97,7 +97,7 @@ pub trait RouterExt<B>: sealed::Sealed {
#[cfg(feature = "typed-routing")]
fn typed_post<H, T, P>(self, handler: H) -> Self
where
H: Handler<T, B>,
H: axum::handler::Handler<T, B>,
T: FirstElementIs<P> + 'static,
P: TypedPath;
@ -110,7 +110,7 @@ pub trait RouterExt<B>: sealed::Sealed {
#[cfg(feature = "typed-routing")]
fn typed_put<H, T, P>(self, handler: H) -> Self
where
H: Handler<T, B>,
H: axum::handler::Handler<T, B>,
T: FirstElementIs<P> + 'static,
P: TypedPath;
@ -123,7 +123,7 @@ pub trait RouterExt<B>: sealed::Sealed {
#[cfg(feature = "typed-routing")]
fn typed_trace<H, T, P>(self, handler: H) -> Self
where
H: Handler<T, B>,
H: axum::handler::Handler<T, B>,
T: FirstElementIs<P> + 'static,
P: TypedPath;
}
@ -135,7 +135,7 @@ where
#[cfg(feature = "typed-routing")]
fn typed_get<H, T, P>(self, handler: H) -> Self
where
H: Handler<T, B>,
H: axum::handler::Handler<T, B>,
T: FirstElementIs<P> + 'static,
P: TypedPath,
{
@ -145,7 +145,7 @@ where
#[cfg(feature = "typed-routing")]
fn typed_delete<H, T, P>(self, handler: H) -> Self
where
H: Handler<T, B>,
H: axum::handler::Handler<T, B>,
T: FirstElementIs<P> + 'static,
P: TypedPath,
{
@ -155,7 +155,7 @@ where
#[cfg(feature = "typed-routing")]
fn typed_head<H, T, P>(self, handler: H) -> Self
where
H: Handler<T, B>,
H: axum::handler::Handler<T, B>,
T: FirstElementIs<P> + 'static,
P: TypedPath,
{
@ -165,7 +165,7 @@ where
#[cfg(feature = "typed-routing")]
fn typed_options<H, T, P>(self, handler: H) -> Self
where
H: Handler<T, B>,
H: axum::handler::Handler<T, B>,
T: FirstElementIs<P> + 'static,
P: TypedPath,
{
@ -175,7 +175,7 @@ where
#[cfg(feature = "typed-routing")]
fn typed_patch<H, T, P>(self, handler: H) -> Self
where
H: Handler<T, B>,
H: axum::handler::Handler<T, B>,
T: FirstElementIs<P> + 'static,
P: TypedPath,
{
@ -185,7 +185,7 @@ where
#[cfg(feature = "typed-routing")]
fn typed_post<H, T, P>(self, handler: H) -> Self
where
H: Handler<T, B>,
H: axum::handler::Handler<T, B>,
T: FirstElementIs<P> + 'static,
P: TypedPath,
{
@ -195,7 +195,7 @@ where
#[cfg(feature = "typed-routing")]
fn typed_put<H, T, P>(self, handler: H) -> Self
where
H: Handler<T, B>,
H: axum::handler::Handler<T, B>,
T: FirstElementIs<P> + 'static,
P: TypedPath,
{
@ -205,7 +205,7 @@ where
#[cfg(feature = "typed-routing")]
fn typed_trace<H, T, P>(self, handler: H) -> Self
where
H: Handler<T, B>,
H: axum::handler::Handler<T, B>,
T: FirstElementIs<P> + 'static,
P: TypedPath,
{

View file

@ -390,6 +390,8 @@ use http::{StatusCode, header::{HeaderValue, USER_AGENT}};
struct ExtractUserAgent(HeaderValue);
// if you need to consume the request body use `axum::extract::Once` instead of
// `R`, otherwise use a generic type.
#[async_trait]
impl<R, B> FromRequest<R, B> for ExtractUserAgent
where

View file

@ -7,7 +7,7 @@
use axum::{
async_trait,
body::{self, BoxBody, Bytes, Full},
extract::{FromRequest, RequestParts},
extract::{FromRequest, Once, RequestParts},
http::{Request, StatusCode},
middleware::{self, Next},
response::{IntoResponse, Response},
@ -72,26 +72,24 @@ fn do_thing_with_request_body(bytes: Bytes) {
tracing::debug!(body = ?bytes);
}
async fn handler(_: PrintRequestBody, body: Bytes) {
async fn handler(PrintRequestBody(body): PrintRequestBody) {
tracing::debug!(?body, "handler received body");
}
// extractor that shows how to consume the request body upfront
struct PrintRequestBody;
struct PrintRequestBody(BoxBody);
#[async_trait]
impl FromRequest<BoxBody> for PrintRequestBody {
impl FromRequest<Once, BoxBody> for PrintRequestBody {
type Rejection = Response;
async fn from_request(req: &mut RequestParts<BoxBody>) -> Result<Self, Self::Rejection> {
async fn from_request(req: &mut RequestParts<Once, BoxBody>) -> Result<Self, Self::Rejection> {
let request = Request::from_request(req)
.await
.map_err(|err| err.into_response())?;
let request = buffer_request_body(request).await?;
*req = RequestParts::new(request);
Ok(Self)
Ok(Self(request.into_body()))
}
}

View file

@ -6,7 +6,7 @@
use axum::{
async_trait,
extract::{rejection::JsonRejection, FromRequest, RequestParts},
extract::{rejection::JsonRejection, FromRequest, RequestParts, Once},
http::StatusCode,
routing::post,
BoxError, Router,
@ -53,7 +53,7 @@ struct User {
struct Json<T>(T);
#[async_trait]
impl<B, T> FromRequest<B> for Json<T>
impl<B, T> FromRequest<Once, B> for Json<T>
where
// these trait bounds are copied from `impl FromRequest for axum::Json`
T: DeserializeOwned,
@ -63,7 +63,7 @@ where
{
type Rejection = (StatusCode, axum::Json<Value>);
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
async fn from_request(req: &mut RequestParts<Once, B>) -> Result<Self, Self::Rejection> {
match axum::Json::<T>::from_request(req).await {
Ok(value) => Ok(Self(value.0)),
Err(rejection) => {

View file

@ -52,7 +52,7 @@ struct Params {
struct Path<T>(T);
#[async_trait]
impl<B, T> FromRequest<B> for Path<T>
impl<R, B, T> FromRequest<R, B> for Path<T>
where
// these trait bounds are copied from `impl FromRequest for axum::extract::path::Path`
T: DeserializeOwned + Send,
@ -60,7 +60,7 @@ where
{
type Rejection = (StatusCode, axum::Json<PathError>);
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
async fn from_request(req: &mut RequestParts<R, B>) -> Result<Self, Self::Rejection> {
match axum::extract::Path::<T>::from_request(req).await {
Ok(value) => Ok(Self(value.0)),
Err(rejection) => {

View file

@ -68,8 +68,8 @@ async fn users_show(
/// Handler for `POST /users`.
async fn users_create(
Json(params): Json<CreateUser>,
Extension(user_repo): Extension<DynUserRepo>,
Json(params): Json<CreateUser>,
) -> Result<Json<User>, AppError> {
let user = user_repo.create(params).await?;

View file

@ -122,13 +122,13 @@ impl AuthBody {
}
#[async_trait]
impl<B> FromRequest<B> for Claims
impl<R, B> FromRequest<R, B> for Claims
where
B: Send,
{
type Rejection = AuthError;
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
async fn from_request(req: &mut RequestParts<R, B>) -> Result<Self, Self::Rejection> {
// Extract the token from the authorization header
let TypedHeader(Authorization(bearer)) =
TypedHeader::<Authorization<Bearer>>::from_request(req)

View file

@ -95,8 +95,8 @@ async fn kv_get(
async fn kv_set(
Path(key): Path<String>,
ContentLengthLimit(bytes): ContentLengthLimit<Bytes, { 1024 * 5_000 }>, // ~5mb
Extension(state): Extension<SharedState>,
ContentLengthLimit(bytes): ContentLengthLimit<Bytes, { 1024 * 5_000 }>, // ~5mb
) {
state.write().unwrap().db.insert(key, bytes);
}

View file

@ -205,14 +205,14 @@ impl IntoResponse for AuthRedirect {
}
#[async_trait]
impl<B> FromRequest<B> for User
impl<R, B> FromRequest<R, B> for User
where
B: Send,
{
// If anything goes wrong or no session is found, redirect to the auth page
type Rejection = AuthRedirect;
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
async fn from_request(req: &mut RequestParts<R, B>) -> Result<Self, Self::Rejection> {
let Extension(store) = Extension::<MemoryStore>::from_request(req)
.await
.expect("`MemoryStore` extension is missing");

View file

@ -82,13 +82,13 @@ enum UserIdFromSession {
}
#[async_trait]
impl<B> FromRequest<B> for UserIdFromSession
impl<R, B> FromRequest<R, B> for UserIdFromSession
where
B: Send,
{
type Rejection = (StatusCode, &'static str);
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
async fn from_request(req: &mut RequestParts<R, B>) -> Result<Self, Self::Rejection> {
let Extension(store) = Extension::<MemoryStore>::from_request(req)
.await
.expect("`MemoryStore` extension missing");

View file

@ -77,13 +77,13 @@ async fn using_connection_pool_extractor(
struct DatabaseConnection(sqlx::pool::PoolConnection<sqlx::Postgres>);
#[async_trait]
impl<B> FromRequest<B> for DatabaseConnection
impl<R, B> FromRequest<R, B> for DatabaseConnection
where
B: Send,
{
type Rejection = (StatusCode, String);
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
async fn from_request(req: &mut RequestParts<R, B>) -> Result<Self, Self::Rejection> {
let Extension(pool) = Extension::<PgPool>::from_request(req)
.await
.map_err(internal_error)?;

View file

@ -107,8 +107,8 @@ struct CreateTodo {
}
async fn todos_create(
Json(input): Json<CreateTodo>,
Extension(db): Extension<Db>,
Json(input): Json<CreateTodo>,
) -> impl IntoResponse {
let todo = Todo {
id: Uuid::new_v4(),
@ -129,8 +129,8 @@ struct UpdateTodo {
async fn todos_update(
Path(id): Path<Uuid>,
Json(input): Json<UpdateTodo>,
Extension(db): Extension<Db>,
Json(input): Json<UpdateTodo>,
) -> Result<impl IntoResponse, StatusCode> {
let mut todo = db
.read()

View file

@ -71,13 +71,13 @@ async fn using_connection_pool_extractor(
struct DatabaseConnection(PooledConnection<'static, PostgresConnectionManager<NoTls>>);
#[async_trait]
impl<B> FromRequest<B> for DatabaseConnection
impl<R, B> FromRequest<R, B> for DatabaseConnection
where
B: Send,
{
type Rejection = (StatusCode, String);
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
async fn from_request(req: &mut RequestParts<R, B>) -> Result<Self, Self::Rejection> {
let Extension(pool) = Extension::<ConnectionPool>::from_request(req)
.await
.map_err(internal_error)?;

View file

@ -12,7 +12,7 @@
use async_trait::async_trait;
use axum::{
extract::{Form, FromRequest, RequestParts},
extract::{Form, FromRequest, RequestParts, Once},
http::StatusCode,
response::{Html, IntoResponse, Response},
routing::get,
@ -60,7 +60,7 @@ async fn handler(ValidatedForm(input): ValidatedForm<NameInput>) -> Html<String>
pub struct ValidatedForm<T>(pub T);
#[async_trait]
impl<T, B> FromRequest<B> for ValidatedForm<T>
impl<T, B> FromRequest<Once, B> for ValidatedForm<T>
where
T: DeserializeOwned + Validate,
B: http_body::Body + Send,
@ -69,7 +69,7 @@ where
{
type Rejection = ServerError;
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
async fn from_request(req: &mut RequestParts<Once, B>) -> Result<Self, Self::Rejection> {
let Form(value) = Form::<T>::from_request(req).await?;
value.validate()?;
Ok(ValidatedForm(value))

View file

@ -48,13 +48,13 @@ enum Version {
}
#[async_trait]
impl<B> FromRequest<B> for Version
impl<R, B> FromRequest<R, B> for Version
where
B: Send,
{
type Rejection = Response;
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
async fn from_request(req: &mut RequestParts<R, B>) -> Result<Self, Self::Rejection> {
let params = Path::<HashMap<String, String>>::from_request(req)
.await
.map_err(IntoResponse::into_response)?;