use crate::request::Request;
use crate::response::{self, Response, Responder};
use crate::http::ContentType;
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, 'o: 'r, R: Responder<'r, 'o>> Responder<'r, 'o> for $name<R> {
fn respond_to(self, req: &'r Request<'_>) -> response::Result<'o> {
(ContentType::$ct, self.0).respond_to(req)
}
}
)+
}
}
ctrs! {
RawJson: JSON, "JSON", "application/json",
RawXml: XML, "XML", "text/xml",
RawMsgPack: MsgPack, "MessagePack", "application/msgpack",
RawHtml: HTML, "HTML", "text/html",
RawText: Text, "plain text", "text/plain",
RawCss: CSS, "CSS", "text/css",
RawJavaScript: JavaScript, "JavaScript", "application/javascript"
}
impl<'r, 'o: 'r, R: Responder<'r, 'o>> Responder<'r, 'o> for (ContentType, R) {
fn respond_to(self, req: &'r Request<'_>) -> response::Result<'o> {
Response::build()
.merge(self.1.respond_to(req)?)
.header(self.0)
.ok()
}
}