use http::StatusCode;
use http::header::LOCATION;
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() {
if cfg!(debug_assertions) {
eprintln!("error: redirect status out of range {}", status);
}
crate::raise!(500);
}
Response::build()
.status(status)
.header(LOCATION, location)
.finish()
}
}