use super::Response;
pub trait ResponseLike {
fn to_response(self) -> Response;
}
impl ResponseLike for Response {
#[inline]
fn to_response(self) -> Response {
self
}
}
impl ResponseLike for () {
#[inline]
fn to_response(self) -> Response {
Response::default()
}
}
impl ResponseLike for &str {
#[inline]
fn to_response(self) -> Response {
crate::response!(ok, self)
}
}
impl ResponseLike for String {
#[inline]
fn to_response(self) -> Response {
crate::response!(ok, self)
}
}
impl ResponseLike for Vec<u8> {
#[inline]
fn to_response(self) -> Response {
crate::response!(ok, self)
}
}
impl<T, E> ResponseLike for Result<T, E>
where
T: ResponseLike,
E: ResponseLike,
{
fn to_response(self) -> Response {
match self {
Ok(res) => res.to_response(),
Err(res) => res.to_response(),
}
}
}
#[cfg(feature = "json")]
impl ResponseLike for serde_json::Error {
#[inline]
fn to_response(self) -> Response {
let bytes = self.to_string().into_bytes();
crate::response!(
bad_request,
bytes,
crate::headers! {
"Content-Type" => "text/plain; charset=utf-8",
}
)
}
}
#[cfg(feature = "json")]
impl ResponseLike for serde_json::Value {
#[inline]
fn to_response(self) -> Response {
let bytes = serde_json::to_vec(&self).unwrap_or_else(|_| self.to_string().into_bytes());
crate::response!(
ok,
bytes,
crate::headers! {
"Content-Type" => "application/json; charset=utf-8",
}
)
}
}