use response::{Response, Responder};
use http::{Status, ContentType};
#[derive(Debug, Clone, PartialEq)]
pub struct Content<R>(pub ContentType, pub R);
impl<'r, R: Responder<'r>> Responder<'r> for Content<R> {
#[inline(always)]
fn respond(self) -> Result<Response<'r>, Status> {
Response::build()
.merge(self.1.respond()?)
.header(self.0)
.ok()
}
}
macro_rules! ctrs {
($($name:ident: $name_str:expr, $ct_str:expr),+) => {
$(
#[doc="Override the `Content-Type` of the response to <b>"]
#[doc=$name_str]
#[doc="</b>, or <i>"]
#[doc=$ct_str]
#[doc="</i>."]
#[derive(Debug, Clone, PartialEq)]
pub struct $name<R>(pub R);
impl<'r, R: Responder<'r>> Responder<'r> for $name<R> {
fn respond(self) -> Result<Response<'r>, Status> {
Content(ContentType::$name, self.0).respond()
}
}
)+
}
}
ctrs! {
JSON: "JSON", "application/json",
XML: "XML", "text/xml",
HTML: "HTML", "text/html",
Plain: "plain text", "text/plain",
CSS: "CSS", "text/css",
JavaScript: "JavaScript", "application/javascript"
}