use http::StatusCode;
use http::header::LOCATION;
use crate::err;
use crate::error::Error;
use crate::response::Response;
pub struct Redirect;
impl Redirect {
pub fn found(location: &str) -> Result<Response, Error> {
Self::with_status(location, StatusCode::FOUND)
}
pub fn see_other(location: &str) -> Result<Response, Error> {
Self::with_status(location, StatusCode::SEE_OTHER)
}
pub fn temporary(location: &str) -> Result<Response, Error> {
Self::with_status(location, StatusCode::TEMPORARY_REDIRECT)
}
pub fn permanent(location: &str) -> Result<Response, Error> {
Self::with_status(location, StatusCode::PERMANENT_REDIRECT)
}
pub fn with_status(location: &str, status: StatusCode) -> Result<Response, Error> {
if status.is_redirection() {
Response::build()
.status(status)
.header(LOCATION, location)
.finish()
} else {
if cfg!(debug_assertions) {
eprintln!("error(via): redirect status out of range {}", status);
}
Err(err!(500, "internal server error."))
}
}
}