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 { Ok(HttpResponse::SeeOther() .header("location", path) // back - to where? .finish()) } pub trait ParseOrBadReq { fn parse_or_bad_request(&self) -> actix_web::Result where T: FromStr, E: Display + Debug + 'static; } impl ParseOrBadReq for &str { fn parse_or_bad_request(&self) -> actix_web::Result where T: FromStr, E: Display + Debug + 'static, { self.parse::().map_err(|e| { error!("Parse error for \"{}\"", self); actix_web::error::ErrorBadRequest(e) }) } } impl ParseOrBadReq for String { fn parse_or_bad_request(&self) -> actix_web::Result where T: FromStr, 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 { fn err_to_500(self) -> Result; } impl StorageErrorIntoResponseError for Result { fn err_to_500(self) -> Result { self.map_err(|e| e.into()) } }