use crate::http::{Response, Status};
use std::borrow::Cow;
pub trait Responsable: Send + 'static {
fn to_response(self) -> Response;
}
impl Responsable for &'static str {
fn to_response(self) -> Response {
Response::with_body(
Status::Ok,
"text/html; charset=utf-8",
self.as_bytes().to_vec(),
)
}
}
impl Responsable for String {
fn to_response(self) -> Response {
Response::with_body(Status::Ok, "text/html; charset=utf-8", self.into_bytes())
}
}
impl Responsable for Cow<'static, str> {
fn to_response(self) -> Response {
let bytes = match self {
Cow::Borrowed(s) => s.as_bytes().to_vec(),
Cow::Owned(s) => s.into_bytes(),
};
Response::with_body(Status::Ok, "text/html; charset=utf-8", bytes)
}
}
impl Responsable for Box<str> {
fn to_response(self) -> Response {
Response::with_body(
Status::Ok,
"text/html; charset=utf-8",
String::from(self).into_bytes(),
)
}
}
impl Responsable for Vec<u8> {
fn to_response(self) -> Response {
Response::with_body(Status::Ok, "application/octet-stream", self)
}
}
impl Responsable for &'static [u8] {
fn to_response(self) -> Response {
Response::with_body(Status::Ok, "application/octet-stream", self.to_vec())
}
}
impl<T: Responsable> Responsable for (Status, T) {
fn to_response(self) -> Response {
let mut res = self.1.to_response();
res.set_status(self.0);
res
}
}
impl Responsable for () {
fn to_response(self) -> Response {
Response::ok()
}
}
impl<T: Responsable> Responsable for (u16, T) {
fn to_response(self) -> Response {
let mut res = self.1.to_response();
if let Some(status) = Status::from_code(self.0) {
res.set_status(status);
}
res
}
}