From 6afc431858e2c7745662e79d4b8e13bbf8c54cab Mon Sep 17 00:00:00 2001 From: Luca Palmieri Date: Wed, 2 Feb 2022 15:17:17 +0000 Subject: [PATCH] Update template-engines/handlerbars to v4 (#497) --- Cargo.lock | 7 ++-- template_engines/handlebars/Cargo.toml | 5 ++- template_engines/handlebars/src/main.rs | 47 ++++++++++++------------- 3 files changed, 28 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c85fb22c..40b07b8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2736,9 +2736,9 @@ dependencies = [ [[package]] name = "handlebars" -version = "3.5.5" +version = "4.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4498fc115fa7d34de968184e473529abb40eeb6be8bc5f7faba3d08c316cb3e3" +checksum = "25546a65e5cf1f471f3438796fc634650b31d7fcde01d444c309aeb28b92e3a8" dependencies = [ "log", "pest", @@ -5818,8 +5818,7 @@ dependencies = [ name = "template_handlebars" version = "1.0.0" dependencies = [ - "actix-http 2.2.2", - "actix-web 3.3.3", + "actix-web 4.0.0-beta.21", "handlebars", "serde_json", ] diff --git a/template_engines/handlebars/Cargo.toml b/template_engines/handlebars/Cargo.toml index 09546af1..1b2c77b7 100644 --- a/template_engines/handlebars/Cargo.toml +++ b/template_engines/handlebars/Cargo.toml @@ -4,7 +4,6 @@ version = "1.0.0" edition = "2021" [dependencies] -actix-http = "2" -actix-web = "3" -handlebars = { version = "3.0.0", features = ["dir_source"] } +actix-web = "4.0.0-beta.21" +handlebars = { version = "4.2.1", features = ["dir_source"] } serde_json = "1.0" diff --git a/template_engines/handlebars/src/main.rs b/template_engines/handlebars/src/main.rs index afd24c35..9ebd6d27 100644 --- a/template_engines/handlebars/src/main.rs +++ b/template_engines/handlebars/src/main.rs @@ -1,17 +1,11 @@ -#[macro_use] -extern crate actix_web; - -#[macro_use] -extern crate serde_json; - -use actix_http::{body::Body, Response}; +use actix_web::body::BoxBody; use actix_web::dev::ServiceResponse; +use actix_web::http::header::ContentType; use actix_web::http::StatusCode; -use actix_web::middleware::errhandlers::{ErrorHandlerResponse, ErrorHandlers}; -use actix_web::{web, App, HttpResponse, HttpServer, Result}; - +use actix_web::middleware::{ErrorHandlerResponse, ErrorHandlers}; +use actix_web::{get, web, App, HttpResponse, HttpServer, Result}; use handlebars::Handlebars; - +use serde_json::json; use std::io; // Macro documentation can be found in the actix_web_codegen crate @@ -28,8 +22,9 @@ async fn index(hb: web::Data>) -> HttpResponse { #[get("/{user}/{data}")] async fn user( hb: web::Data>, - web::Path(info): web::Path<(String, String)>, + path: web::Path<(String, String)>, ) -> HttpResponse { + let info = path.into_inner(); let data = json!({ "user": info.0, "data": info.1 @@ -63,27 +58,31 @@ async fn main() -> io::Result<()> { } // Custom error handlers, to return HTML responses when an error occurs. -fn error_handlers() -> ErrorHandlers { +fn error_handlers() -> ErrorHandlers { ErrorHandlers::new().handler(StatusCode::NOT_FOUND, not_found) } // Error handler for a 404 Page not found error. -fn not_found(res: ServiceResponse) -> Result> { +fn not_found(res: ServiceResponse) -> Result> { let response = get_error_response(&res, "Page not found"); - Ok(ErrorHandlerResponse::Response( - res.into_response(response.into_body()), - )) + Ok(ErrorHandlerResponse::Response(ServiceResponse::new( + res.into_parts().0, + response.map_into_left_body(), + ))) } // Generic error handler. -fn get_error_response(res: &ServiceResponse, error: &str) -> Response { +fn get_error_response( + res: &ServiceResponse, + error: &str, +) -> HttpResponse { let request = res.request(); // Provide a fallback to a simple plain text response in case an error occurs during the // rendering of the error page. let fallback = |e: &str| { - Response::build(res.status()) - .content_type("text/plain") + HttpResponse::build(res.status()) + .content_type(ContentType::plaintext()) .body(e.to_string()) }; @@ -99,12 +98,12 @@ fn get_error_response(res: &ServiceResponse, error: &str) -> Response Response::build(res.status()) - .content_type("text/html") + Ok(body) => HttpResponse::build(res.status()) + .content_type(ContentType::html()) .body(body), - Err(_) => fallback(error), + Err(_) => fallback(error).into(), } } - None => fallback(error), + None => fallback(error).into(), } }