use serde_json::Value;
use super::{Body, HttpResponse};
pub trait IntoResponse {
type Response;
fn to_response(&self) -> Self::Response;
}
impl IntoResponse for HttpResponse {
type Response = Self;
fn to_response(&self) -> Self {
self.clone()
}
}
impl IntoResponse for Body {
type Response = HttpResponse;
fn to_response(&self) -> Self::Response {
HttpResponse {
body: Some(self.clone()),
..HttpResponse::new()
}
}
}
impl IntoResponse for u16 {
type Response = HttpResponse;
fn to_response(&self) -> Self::Response {
HttpResponse {
status: *self,
..HttpResponse::new()
}
}
}
impl IntoResponse for Vec<(String, String)> {
type Response = HttpResponse;
fn to_response(&self) -> Self::Response {
HttpResponse {
headers: self.clone(),
..HttpResponse::new()
}
}
}
impl IntoResponse for (u16, Body) {
type Response = HttpResponse;
fn to_response(&self) -> Self::Response {
HttpResponse {
body: Some(self.1.clone()),
status: self.0,
..HttpResponse::new()
}
}
}
impl IntoResponse for Value {
type Response = HttpResponse;
fn to_response(&self) -> Self::Response {
HttpResponse {
body: Some(Body::Json(self.clone())),
headers: vec![("Content-Type".to_string(), "application/json".to_string())],
..HttpResponse::new()
}
}
}
impl IntoResponse for String {
type Response = HttpResponse;
fn to_response(&self) -> Self::Response {
HttpResponse {
body: Some(Body::Text(self.clone())),
..HttpResponse::new()
}
}
}
impl IntoResponse for &'static str {
type Response = HttpResponse;
fn to_response(&self) -> Self::Response {
HttpResponse {
body: Some(Body::Text(self.to_string())),
..HttpResponse::new()
}
}
}