|
|
|
use actix_web::http::header::IntoHeaderValue;
|
|
|
|
use actix_web::{HttpResponse, ResponseError};
|
|
|
|
use std::fmt::{Debug, Display};
|
|
|
|
use std::str::FromStr;
|
|
|
|
use yopa::StorageError;
|
|
|
|
|
|
|
|
pub fn redirect(path: impl IntoHeaderValue) -> actix_web::Result<HttpResponse> {
|
|
|
|
Ok(HttpResponse::SeeOther()
|
|
|
|
.header("location", path) // back - to where?
|
|
|
|
.finish())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait ParseOrBadReq {
|
|
|
|
fn parse_or_bad_request<T, E>(&self) -> actix_web::Result<T>
|
|
|
|
where
|
|
|
|
T: FromStr<Err = E>,
|
|
|
|
E: Display + Debug + 'static;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ParseOrBadReq for &str {
|
|
|
|
fn parse_or_bad_request<T, E>(&self) -> actix_web::Result<T>
|
|
|
|
where
|
|
|
|
T: FromStr<Err = E>,
|
|
|
|
E: Display + Debug + 'static,
|
|
|
|
{
|
|
|
|
self.parse::<T>().map_err(|e| {
|
|
|
|
error!("Parse error for \"{}\"", self);
|
|
|
|
actix_web::error::ErrorBadRequest(e)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ParseOrBadReq for String {
|
|
|
|
fn parse_or_bad_request<T, E>(&self) -> actix_web::Result<T>
|
|
|
|
where
|
|
|
|
T: FromStr<Err = E>,
|
|
|
|
E: Display + Debug + 'static,
|
|
|
|
{
|
|
|
|
self.as_str().parse_or_bad_request()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum ActixErrorWrapper {
|
|
|
|
#[error(transparent)]
|
|
|
|
Storage(#[from] StorageError),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ResponseError for ActixErrorWrapper {}
|
|
|
|
|
|
|
|
pub trait StorageErrorIntoResponseError<T> {
|
|
|
|
fn err_to_500(self) -> Result<T, ActixErrorWrapper>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> StorageErrorIntoResponseError<T> for Result<T, StorageError> {
|
|
|
|
fn err_to_500(self) -> Result<T, ActixErrorWrapper> {
|
|
|
|
self.map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
}
|