mod future;
pub use future::{IntoResponseFuture, ResponseFuture};
use crate::error::BoxError;
use crate::hyper::{header, Response, StatusCode};
use std::error::Error as StdError;
#[cfg_attr(
feature = "json",
doc = r##"
### JSON payloads
This is only supported when the `json` feature flag is set.
```rust
# use mock_http_connector::IntoResponse;
# use serde_json::json;
let payload = json!({ "message": "some response" });
let res = payload.into_response();
```
"##
)]
pub trait IntoResponse {
fn into_response(self) -> Result<Response<String>, BoxError>;
}
impl<R, E> IntoResponse for Result<R, E>
where
R: IntoResponse,
E: StdError + Send + Sync + 'static,
{
fn into_response(self) -> Result<Response<String>, BoxError> {
self.map_err(Into::into).and_then(|r| r.into_response())
}
}
impl<B> IntoResponse for Response<B>
where
B: ToString,
{
fn into_response(self) -> Result<Response<String>, BoxError> {
Ok(self.map(|b| b.to_string()))
}
}
impl IntoResponse for &'_ str {
fn into_response(self) -> Result<Response<String>, BoxError> {
Ok(Response::builder()
.status(StatusCode::OK)
.body(self.to_string())?)
}
}
impl IntoResponse for String {
fn into_response(self) -> Result<Response<String>, BoxError> {
Ok(Response::builder().status(StatusCode::OK).body(self)?)
}
}
impl<S, B> IntoResponse for (S, B)
where
S: TryInto<StatusCode> + 'static,
S::Error: StdError + Send + Sync + 'static,
B: ToString + 'static,
{
fn into_response(self) -> Result<Response<String>, BoxError> {
let status = self.0.try_into();
let body = self.1.to_string();
Ok(Response::builder().status(status?).body(body)?)
}
}
#[cfg(feature = "json")]
impl IntoResponse for serde_json::Value {
fn into_response(self) -> Result<Response<String>, BoxError> {
Ok(Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "application/json")
.body(serde_json::to_string(&self)?)?)
}
}