Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(util): add ResponseExt trait #150

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions http-body-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod either;
mod empty;
mod full;
mod limited;
mod response_ext;
mod stream;

#[cfg(feature = "channel")]
Expand All @@ -22,16 +23,16 @@ mod util;

use self::combinators::{BoxBody, MapErr, MapFrame, UnsyncBoxBody};

#[cfg(feature = "channel")]
pub use self::channel::Channel;
pub use self::collected::Collected;
pub use self::either::Either;
pub use self::empty::Empty;
pub use self::full::Full;
pub use self::limited::{LengthLimitError, Limited};
pub use self::response_ext::ResponseExt;
pub use self::stream::{BodyDataStream, BodyStream, StreamBody};

#[cfg(feature = "channel")]
pub use self::channel::Channel;

/// An extension trait for [`http_body::Body`] adding various combinators and adapters
pub trait BodyExt: http_body::Body {
/// Returns a future that resolves to the next [`Frame`], if any.
Expand Down
116 changes: 116 additions & 0 deletions http-body-util/src/response_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use crate::combinators::{BoxBody, UnsyncBoxBody};

/// An extension trait for [`http::Request`] adding various combinators and adapters
pub trait ResponseExt<B> {
/// Returns a new `http::Response` with the body boxed.
///
/// This is useful when you have need to return a Response where the body type is not known at
/// compile time.
///
/// # Example
///
/// ```
/// use bytes::Bytes;
/// use http::Response;
/// use http_body_util::{Empty, ResponseExt};
///
/// # let some_condition = true;
/// let response = if some_condition {
/// greeting().box_body()
/// } else {
/// empty().box_body()
/// };
///
/// fn greeting() -> Response<String> {
/// Response::new("Hello, World!".to_string())
/// }
///
/// fn empty() -> Response<Empty<Bytes>> {
/// Response::new(Empty::new())
/// }
/// ```
fn box_body(self) -> http::Response<BoxBody<B::Data, B::Error>>
where
B: http_body::Body + Send + Sync + 'static;

/// Returns a new `http::Response` with the body boxed and !Sync.
///
/// This is useful when you have need to return a Response where the body type is not known at
/// compile time and the body is not Sync.
///
/// # Example
///
/// ```
/// use bytes::Bytes;
/// use http::Response;
/// use http_body_util::{Empty, ResponseExt};
///
/// # let some_condition = true;
/// let response = if some_condition {
/// greeting().box_body_unsync()
/// } else {
/// empty().box_body_unsync()
/// };
///
/// fn greeting() -> Response<String> {
/// Response::new("Hello, World!".to_string())
/// }
///
/// fn empty() -> Response<Empty<Bytes>> {
/// Response::new(Empty::new())
/// }
/// ```
fn box_body_unsync(self) -> http::Response<UnsyncBoxBody<B::Data, B::Error>>
where
B: http_body::Body + Send + 'static;
}

impl<B> ResponseExt<B> for http::Response<B> {
fn box_body(self) -> http::Response<BoxBody<B::Data, B::Error>>
where
B: http_body::Body + Send + Sync + 'static,
{
self.map(crate::BodyExt::boxed)
}

fn box_body_unsync(self) -> http::Response<UnsyncBoxBody<B::Data, B::Error>>
where
B: http_body::Body + Send + 'static,
{
self.map(crate::BodyExt::boxed_unsync)
}
}

#[cfg(test)]
mod tests {
use bytes::Bytes;
use http::{Response, StatusCode, Uri};

use super::*;
use crate::{Empty, Full};

#[test]
fn box_body() {
let uri: Uri = "http://example.com".parse().unwrap();
let _response = match uri.path() {
"/greeting" => greeting().box_body(),
"/empty" => empty().box_body(),
_ => not_found().box_body(),
};
}

fn greeting() -> Response<String> {
Response::new("Hello, World!".to_string())
}

fn empty() -> Response<Empty<Bytes>> {
Response::new(Empty::new())
}

fn not_found() -> Response<Full<Bytes>> {
Response::builder()
.status(StatusCode::NOT_FOUND)
.body("Not Found".into())
.unwrap()
}
}