Remove the associated Body type on IntoResponse (#571)

This commit is contained in:
Kai Jewson
2021-11-28 18:52:18 +01:00
committed by GitHub
parent decdd4c948
commit 2b6dba49cb
25 changed files with 171 additions and 358 deletions
@@ -9,7 +9,7 @@
use axum::{
async_trait,
body::{Bytes, Full},
body::BoxBody,
extract::{Extension, Path},
http::{Response, StatusCode},
response::IntoResponse,
@@ -18,7 +18,7 @@ use axum::{
};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{convert::Infallible, net::SocketAddr, sync::Arc};
use std::{net::SocketAddr, sync::Arc};
use uuid::Uuid;
#[tokio::main]
@@ -92,10 +92,7 @@ impl From<UserRepoError> for AppError {
}
impl IntoResponse for AppError {
type Body = Full<Bytes>;
type BodyError = Infallible;
fn into_response(self) -> Response<Self::Body> {
fn into_response(self) -> Response<BoxBody> {
let (status, error_message) = match self {
AppError::UserRepo(UserRepoError::NotFound) => {
(StatusCode::NOT_FOUND, "User not found")
+10 -8
View File
@@ -13,8 +13,9 @@
//! Example is based on <https://github.com/hyperium/hyper/blob/master/examples/http_proxy.rs>
use axum::{
body::{boxed, Body},
body::{self, Body, BoxBody},
http::{Method, Request, Response, StatusCode},
response::IntoResponse,
routing::get,
Router,
};
@@ -37,7 +38,7 @@ async fn main() {
let router = router.clone();
async move {
if req.method() == Method::CONNECT {
proxy(req).await.map(|res| res.map(boxed))
proxy(req).await
} else {
router.oneshot(req).await.map_err(|err| match err {})
}
@@ -54,7 +55,7 @@ async fn main() {
.unwrap();
}
async fn proxy(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
async fn proxy(req: Request<Body>) -> Result<Response<BoxBody>, hyper::Error> {
tracing::trace!(?req);
if let Some(host_addr) = req.uri().authority().map(|auth| auth.to_string()) {
@@ -69,13 +70,14 @@ async fn proxy(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
}
});
Ok(Response::new(Body::empty()))
Ok(Response::new(body::boxed(body::Empty::new())))
} else {
tracing::warn!("CONNECT host is not socket addr: {:?}", req.uri());
let mut resp = Response::new(Body::from("CONNECT must be to a socket address"));
*resp.status_mut() = StatusCode::BAD_REQUEST;
Ok(resp)
Ok((
StatusCode::BAD_REQUEST,
"CONNECT must be to a socket address",
)
.into_response())
}
}
+3 -6
View File
@@ -8,7 +8,7 @@
use axum::{
async_trait,
body::{Bytes, Full},
body::BoxBody,
extract::{FromRequest, RequestParts, TypedHeader},
http::{Response, StatusCode},
response::IntoResponse,
@@ -20,7 +20,7 @@ use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, Validation}
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{convert::Infallible, fmt::Display, net::SocketAddr};
use std::{fmt::Display, net::SocketAddr};
// Quick instructions
//
@@ -141,10 +141,7 @@ where
}
impl IntoResponse for AuthError {
type Body = Full<Bytes>;
type BodyError = Infallible;
fn into_response(self) -> Response<Self::Body> {
fn into_response(self) -> Response<BoxBody> {
let (status, error_message) = match self {
AuthError::WrongCredentials => (StatusCode::UNAUTHORIZED, "Wrong credentials"),
AuthError::MissingCredentials => (StatusCode::BAD_REQUEST, "Missing credentials"),
+2 -5
View File
@@ -9,7 +9,7 @@
use async_session::{MemoryStore, Session, SessionStore};
use axum::{
async_trait,
body::{Bytes, Empty},
body::BoxBody,
extract::{Extension, FromRequest, Query, RequestParts, TypedHeader},
http::{header::SET_COOKIE, HeaderMap, Response},
response::{IntoResponse, Redirect},
@@ -199,10 +199,7 @@ async fn login_authorized(
struct AuthRedirect;
impl IntoResponse for AuthRedirect {
type Body = Empty<Bytes>;
type BodyError = <Self::Body as axum::body::HttpBody>::Error;
fn into_response(self) -> Response<Self::Body> {
fn into_response(self) -> Response<BoxBody> {
Redirect::found("/auth/discord".parse().unwrap()).into_response()
}
}
+5 -8
View File
@@ -6,14 +6,14 @@
use askama::Template;
use axum::{
body::{Bytes, Full},
body::{self, BoxBody, Full},
extract,
http::{Response, StatusCode},
response::{Html, IntoResponse},
routing::get,
Router,
};
use std::{convert::Infallible, net::SocketAddr};
use std::net::SocketAddr;
#[tokio::main]
async fn main() {
@@ -52,18 +52,15 @@ impl<T> IntoResponse for HtmlTemplate<T>
where
T: Template,
{
type Body = Full<Bytes>;
type BodyError = Infallible;
fn into_response(self) -> Response<Self::Body> {
fn into_response(self) -> Response<BoxBody> {
match self.0.render() {
Ok(html) => Html(html).into_response(),
Err(err) => Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Full::from(format!(
.body(body::boxed(Full::from(format!(
"Failed to render template. Error: {}",
err
)))
))))
.unwrap(),
}
}
+3 -6
View File
@@ -12,7 +12,7 @@
use async_trait::async_trait;
use axum::{
body::{Bytes, Full},
body::BoxBody,
extract::{Form, FromRequest, RequestParts},
http::{Response, StatusCode},
response::{Html, IntoResponse},
@@ -20,7 +20,7 @@ use axum::{
BoxError, Router,
};
use serde::{de::DeserializeOwned, Deserialize};
use std::{convert::Infallible, net::SocketAddr};
use std::net::SocketAddr;
use thiserror::Error;
use validator::Validate;
@@ -84,10 +84,7 @@ pub enum ServerError {
}
impl IntoResponse for ServerError {
type Body = Full<Bytes>;
type BodyError = Infallible;
fn into_response(self) -> Response<Self::Body> {
fn into_response(self) -> Response<BoxBody> {
match self {
ServerError::ValidationError(_) => {
let message = format!("Input validation error: [{}]", self).replace("\n", ", ");
+2 -2
View File
@@ -6,7 +6,7 @@
use axum::{
async_trait,
body::{Bytes, Full},
body::BoxBody,
extract::{FromRequest, Path, RequestParts},
http::{Response, StatusCode},
response::IntoResponse,
@@ -51,7 +51,7 @@ impl<B> FromRequest<B> for Version
where
B: Send,
{
type Rejection = Response<Full<Bytes>>;
type Rejection = Response<BoxBody>;
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
let params = Path::<HashMap<String, String>>::from_request(req)