From a0853ed3dea80aafdf4bcb11f81c4b31147eae1a Mon Sep 17 00:00:00 2001 From: Dmitry Date: Sun, 20 Aug 2023 13:44:17 +0300 Subject: [PATCH] static template context for errors --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 11 +++++++---- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2cb9a71..986983b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1455,6 +1455,7 @@ name = "qchgk_web" version = "0.2.0" dependencies = [ "chgk_ledb_lib", + "lazy_static", "mini-moka", "rand", "rocket", diff --git a/Cargo.toml b/Cargo.toml index 7183f0a..b71d99f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ rocket = { version = "=0.5.0-rc.3", features = ["json"] } rocket_dyn_templates = { version = "=0.1.0-rc.3", features = ["tera"] } chgk_ledb_lib = {git = "https://gitea.b4tman.ru/b4tman/chgk_ledb.git", rev="699478f85e", package="chgk_ledb_lib", features=["async"]} mini-moka = "0.10.0" +lazy_static = "1.4.0" [profile.release] opt-level = 3 diff --git a/src/main.rs b/src/main.rs index 6519446..8cf4797 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ use rocket_dyn_templates::Template; use rand::distributions::Uniform; use rand::Rng; +use lazy_static::lazy_static; use std::collections::HashMap; use std::ops::Deref; use std::sync::Arc; @@ -26,6 +27,10 @@ use std::time::Duration; const DB_FILENAME: &str = "db.dat"; +lazy_static! { + static ref EMPTY_MAP: HashMap = HashMap::new(); +} + trait ErrorEmpty { type Output; fn err_empty(self) -> Result; @@ -48,12 +53,10 @@ enum WebError { impl WebError { fn not_found() -> Self { - let context: HashMap = HashMap::new(); - WebError::NotFound(Template::render("404", context)) + WebError::NotFound(Template::render("404", EMPTY_MAP.deref())) } fn server_error() -> Self { - let context: HashMap = HashMap::new(); - WebError::ServerError(Template::render("500", context)) + WebError::ServerError(Template::render("500", EMPTY_MAP.deref())) } }