diff options
author | René Kijewski <kijewski@library.vetmed.fu-berlin.de> | 2022-01-05 19:35:53 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2022-01-06 13:05:24 +0100 |
commit | fe620456671cf52c741309b3bdcf7460e1973476 (patch) | |
tree | 63e0597acde56882f682a5eea623cafad10c991d /askama_actix/src | |
parent | f4254dedcfcfce4000abc91b4860c36d3fb91430 (diff) | |
download | askama-fe620456671cf52c741309b3bdcf7460e1973476.tar.gz askama-fe620456671cf52c741309b3bdcf7460e1973476.tar.bz2 askama-fe620456671cf52c741309b3bdcf7460e1973476.zip |
No needless boxing of the error
Diffstat (limited to 'askama_actix/src')
-rw-r--r-- | askama_actix/src/lib.rs | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/askama_actix/src/lib.rs b/askama_actix/src/lib.rs index afc3ea3..7ecf591 100644 --- a/askama_actix/src/lib.rs +++ b/askama_actix/src/lib.rs @@ -1,11 +1,32 @@ #![deny(elided_lifetimes_in_paths)] +use std::fmt; + use actix_web::body::BoxBody; use actix_web::http::StatusCode; use actix_web::{HttpResponse, HttpResponseBuilder, ResponseError}; use askama::mime::extension_to_mime_type; pub use askama::*; +/// Newtype to let askama::Error implement actix_web::ResponseError. +struct ActixError(Error); + +impl fmt::Debug for ActixError { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + <Error as fmt::Debug>::fmt(&self.0, f) + } +} + +impl fmt::Display for ActixError { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + <Error as fmt::Display>::fmt(&self.0, f) + } +} + +impl ResponseError for ActixError {} + pub trait TemplateToResponse { fn to_response(&self) -> HttpResponse<BoxBody>; } @@ -19,9 +40,7 @@ impl<T: askama::Template> TemplateToResponse for T { .content_type(ctype) .body(buffer) } - Err(err) => { - HttpResponse::from_error(Box::new(err) as Box<dyn std::error::Error + 'static>) - } + Err(err) => HttpResponse::from_error(ActixError(err)), } } } |