use http::header::{HeaderValue, CONTENT_TYPE};
use http::response::Response;
use http::status::StatusCode;
macro_rules! render_error_message {
($title:literal) => {
concat!(
"<!DOCTYPE html><html><head><title>",
$title,
"</title></head><body style=\"background-color:#FFFFF0; color:#000040; font-family:roboto, 'open sans', sans-serif\"><h1>",
$title,
"</h1></body></html>"
)
}
}
const HTML: HeaderValue = HeaderValue::from_static("text/html; charset=utf-8");
fn build_error(status: StatusCode, body: &'static str) -> Response<Vec<u8>> {
let mut response = Response::new(body.as_bytes().to_owned());
*response.status_mut() = status;
response.headers_mut().insert(CONTENT_TYPE, HTML);
response
}
pub fn build_404() -> Response<Vec<u8>> {
build_error(StatusCode::NOT_FOUND, render_error_message!("404 Not Found"))
}
pub fn build_400() -> Response<Vec<u8>> {
build_error(StatusCode::BAD_REQUEST, render_error_message!("400 Bad Request"))
}
pub fn build_500() -> Response<Vec<u8>> {
build_error(StatusCode::INTERNAL_SERVER_ERROR, render_error_message!("500 Internal Server Error"))
}