use request::Request;
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_to(self, req: &Request) -> Result<Response<'r>, Status> {
Response::build()
.merge(self.1.respond_to(req)?)
.header(self.0)
.ok()
}
}
macro_rules! ctrs {
($($name:ident: $ct: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_to(self, req: &Request) -> Result<Response<'r>, Status> {
Content(ContentType::$ct, self.0).respond_to(req)
}
}
)+
}
}
ctrs! {
Json: JSON, "JSON", "application/json",
Xml: XML, "XML", "text/xml",
MsgPack: MsgPack, "MessagePack", "application/msgpack",
Html: HTML, "HTML", "text/html",
Plain: Plain, "plain text", "text/plain",
Css: CSS, "CSS", "text/css",
JavaScript: JavaScript, "JavaScript", "application/javascript"
}