#![deny(unsafe_code)]
#![deny(warnings)]
#![deny(clippy::all)]
#![deny(missing_docs)]
#![deny(rustdoc::missing_doc_code_examples)]
#[cfg(any(feature = "json", feature = "msgpack"))]
use rocket::serde;
use rocket::{
fs::NamedFile,
http::Status,
response::{
content::{RawCss, RawHtml, RawJavaScript, RawJson, RawMsgPack, RawText, RawXml},
status::{
Accepted, BadRequest, Conflict, Created, Forbidden, NoContent, NotFound, Unauthorized,
},
Flash, Redirect,
},
serde::Serialize,
tokio, Responder,
};
#[cfg(any(feature = "templates-tera", feature = "templates-handlebars"))]
use rocket_dyn_templates::Template;
use std::fs::File;
#[derive(Responder)]
pub enum RocketResponse {
Accepted(Accepted<&'static str>),
BadRequest(BadRequest<&'static str>),
Conflict(Conflict<&'static str>),
Created(Created<&'static str>),
Css(RawCss<&'static str>),
File(File),
Flash(Flash<&'static str>),
Forbidden(Forbidden<&'static str>),
Html(RawHtml<&'static str>),
JavaScript(RawJavaScript<&'static str>),
Json(RawJson<&'static str>),
MsgPack(RawMsgPack<&'static str>),
NamedFiled(NamedFile),
NotFound(NotFound<&'static str>),
NoContent(NoContent),
Plain(RawText<&'static str>),
Redirect(Redirect),
#[cfg(feature = "json")]
SerdeJson(serde::json::Json<&'static str>),
#[cfg(feature = "msgpack")]
SerdeMsgPack(serde::msgpack::MsgPack<&'static str>),
#[cfg(feature = "json")]
SerdeValue(serde::json::Value),
StaticSlice(&'static [u8]),
StaticStr(&'static str),
String(String),
Status(Status),
#[cfg(any(feature = "templates-tera", feature = "templates-handlebars"))]
Template(Template),
TokioFile(tokio::fs::File),
Unauthorized(Unauthorized<&'static str>),
Vec(Vec<u8>),
Xml(RawXml<&'static str>),
}
#[derive(Responder)]
pub enum RocketResponseGeneric<T>
where
T: Serialize,
{
Accepted(Accepted<T>),
BadRequest(BadRequest<T>),
Conflict(Conflict<T>),
Created(Created<T>),
Css(RawCss<T>),
File(File),
Flash(Flash<T>),
Forbidden(Forbidden<T>),
Html(RawHtml<T>),
JavaScript(RawJavaScript<T>),
Json(RawJson<T>),
MsgPack(RawMsgPack<T>),
NamedFiled(NamedFile),
NotFound(NotFound<T>),
NoContent(NoContent),
Plain(RawText<T>),
Redirect(Redirect),
#[cfg(feature = "json")]
SerdeJson(serde::json::Json<T>),
#[cfg(feature = "msgpack")]
SerdeMsgPack(serde::msgpack::MsgPack<T>),
#[cfg(feature = "json")]
SerdeValue(serde::json::Value),
StaticSlice(&'static [u8]),
StaticStr(&'static str),
String(String),
Status(Status),
#[cfg(any(feature = "templates-tera", feature = "templates-handlebars"))]
Template(Template),
TokioFile(tokio::fs::File),
Unauthorized(Unauthorized<T>),
Vec(Vec<u8>),
Xml(RawXml<T>),
}
#[derive(Responder)]
pub enum RocketResponseGeneric2<T, U>
where
T: Serialize,
{
Accepted(Accepted<T>),
BadRequest(BadRequest<T>),
Conflict(Conflict<T>),
Created(Created<T>),
Css(RawCss<T>),
File(File),
Flash(Flash<U>),
Forbidden(Forbidden<T>),
Html(RawHtml<T>),
JavaScript(RawJavaScript<T>),
Json(RawJson<T>),
MsgPack(RawMsgPack<T>),
NamedFiled(NamedFile),
NotFound(NotFound<T>),
NoContent(NoContent),
Plain(RawText<T>),
Redirect(Redirect),
#[cfg(feature = "json")]
SerdeJson(serde::json::Json<T>),
#[cfg(feature = "msgpack")]
SerdeMsgPack(serde::msgpack::MsgPack<T>),
#[cfg(feature = "json")]
SerdeValue(serde::json::Value),
StaticSlice(&'static [u8]),
StaticStr(&'static str),
String(String),
Status(Status),
#[cfg(any(feature = "templates-tera", feature = "templates-handlebars"))]
Template(Template),
TokioFile(tokio::fs::File),
Unauthorized(Unauthorized<T>),
Vec(Vec<u8>),
Xml(RawXml<T>),
}
#[cfg(test)]
mod tests {
use super::{RocketResponse, RocketResponseGeneric, RocketResponseGeneric2};
use rocket::{
get,
http::ContentType,
http::Status,
local::blocking::Client,
response::{self, status, Redirect},
routes,
};
#[get("/response/<id>")]
pub(crate) fn route_response(id: usize) -> RocketResponse {
match id {
0 => RocketResponse::NoContent(response::status::NoContent),
1 => RocketResponse::Redirect(Redirect::to("/admin")),
_ => RocketResponse::StaticStr("Hello world"),
}
}
#[get("/response_generic/<id>")]
pub(crate) fn route_response_generic(id: usize) -> RocketResponseGeneric<&'static str> {
match id {
0 => RocketResponseGeneric::NoContent(status::NoContent),
1 => RocketResponseGeneric::Unauthorized(status::Unauthorized(Some(
"admin need authentication",
))),
_ => RocketResponseGeneric::Html(response::content::RawHtml(
"<html><body>Hello world</body></html",
)),
}
}
#[get("/response_generic2/<id>")]
pub(crate) fn route_response_generic2(
id: usize,
) -> RocketResponseGeneric2<&'static str, Redirect> {
match id {
0 => RocketResponseGeneric2::Flash(response::Flash::error(
Redirect::to("/"),
format!("Invalid id {}", id),
)),
1 => RocketResponseGeneric2::Unauthorized(status::Unauthorized(Some(
"admin need authentication",
))),
_ => RocketResponseGeneric2::Html(response::content::RawHtml(
"<html><body>Hello world</body></html",
)),
}
}
#[test]
fn test_rocket_response() {
let rocket = rocket::build().mount("/", routes![route_response]);
let client = Client::tracked(rocket).expect("no rocket instance");
let req = client.get("/response/2");
let res = req.dispatch();
assert_eq!(Status::Ok, res.status());
assert_eq!(ContentType::Plain, res.content_type().unwrap());
}
#[test]
fn test_rocket_response_generic() {
let rocket = rocket::build().mount("/", routes![route_response_generic]);
let client = Client::tracked(rocket).expect("no rocket instance");
let req = client.get("/response_generic/0");
let res = req.dispatch();
assert_eq!(Status::NoContent, res.status());
}
#[test]
fn test_rocket_response_generic2() {
let rocket = rocket::build().mount("/", routes![route_response_generic2]);
let client = Client::tracked(rocket).expect("no rocket instance");
let req = client.get("/response_generic2/0");
let res = req.dispatch();
assert_eq!(Status::SeeOther, res.status());
}
}