a small relational database with user-editable schema for manual data entry
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
yopa/yopa-web/src/utils.rs

61 lines
1.5 KiB

use actix_web::http::header::IntoHeaderValue;
use actix_web::{HttpResponse, ResponseError, Error};
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())
}
}