From 1f4053f361b67f11219341d912e229e815abc832 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Wed, 23 Feb 2022 09:55:06 +0530 Subject: [PATCH] feat: load embedded templates --- src/pages/auth/login.rs | 4 ++-- src/pages/auth/mod.rs | 18 +++++------------- src/pages/auth/register.rs | 4 ++-- src/pages/errors.rs | 10 +++------- src/pages/mod.rs | 4 ++++ 5 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/pages/auth/login.rs b/src/pages/auth/login.rs index 8a01ce9..fe749ff 100644 --- a/src/pages/auth/login.rs +++ b/src/pages/auth/login.rs @@ -32,7 +32,7 @@ pub struct Login { ctx: RefCell, } -pub const LOGIN: &str = "login"; +pub const LOGIN: TemplateFile = TemplateFile::new("login", "pages/auth/login.html"); impl CtxError for Login { fn with_error(&self, e: &ReadableError) -> String { @@ -51,7 +51,7 @@ impl Login { } pub fn render(&self) -> String { - TEMPLATES.render(LOGIN, &self.ctx.borrow()).unwrap() + TEMPLATES.render(LOGIN.name, &self.ctx.borrow()).unwrap() } pub fn page(s: &Settings) -> String { diff --git a/src/pages/auth/mod.rs b/src/pages/auth/mod.rs index 33c0a81..f40fc8c 100644 --- a/src/pages/auth/mod.rs +++ b/src/pages/auth/mod.rs @@ -17,27 +17,19 @@ use actix_identity::Identity; use actix_web::*; -pub use super::{context, Footer, PAGES, PAYLOAD_KEY, TEMPLATES}; +pub use super::{context, Footer, TemplateFile, PAGES, PAYLOAD_KEY, TEMPLATES}; pub mod login; pub mod register; #[cfg(test)] mod test; -pub const HOME_BASE: &str = "homebase"; +pub const AUTH_BASE: TemplateFile = TemplateFile::new("authbase", "pages/auth/base.html"); pub fn register_templates(t: &mut tera::Tera) { - if let Err(e) = t.add_template_files(vec![ - ("templates/pages/home/login.html", Some(login::LOGIN)), - ( - "templates/pages/home/register.html", - Some(register::REGISTER), - ), - ("templates/pages/home/base.html", Some(HOME_BASE)), - ]) { - println!("Parsing error(s): {}", e); - ::std::process::exit(1); - }; + for template in [AUTH_BASE, login::LOGIN, register::REGISTER].iter() { + template.register(t).expect(template.name); + } } pub fn services(cfg: &mut web::ServiceConfig) { diff --git a/src/pages/auth/register.rs b/src/pages/auth/register.rs index 03fa49e..818044f 100644 --- a/src/pages/auth/register.rs +++ b/src/pages/auth/register.rs @@ -25,7 +25,7 @@ use crate::AppData; pub use super::*; -pub const REGISTER: &str = "register"; +pub const REGISTER: TemplateFile = TemplateFile::new("login", "pages/auth/register.html"); pub struct Register { ctx: RefCell, @@ -48,7 +48,7 @@ impl Register { } pub fn render(&self) -> String { - TEMPLATES.render(REGISTER, &self.ctx.borrow()).unwrap() + TEMPLATES.render(REGISTER.name, &self.ctx.borrow()).unwrap() } pub fn page(s: &Settings) -> String { diff --git a/src/pages/errors.rs b/src/pages/errors.rs index 9e77ffb..5c5e116 100644 --- a/src/pages/errors.rs +++ b/src/pages/errors.rs @@ -26,17 +26,13 @@ use derive_more::Error; use serde::*; use crate::errors::ServiceError; +use super::TemplateFile; pub const ERROR_KEY: &str = "error"; -pub const ERROR_PAGE: &str = "error_comp"; +pub const ERROR_TEMPLATE: TemplateFile = TemplateFile::new("error_comp", "components/error.html"); pub fn register_templates(t: &mut tera::Tera) { - if let Err(e) = - t.add_template_files(vec![("templates/components/error.html", Some(ERROR_PAGE))]) - { - println!("Parsing error(s): {}", e); - ::std::process::exit(1); - }; + ERROR_TEMPLATE.register(t).expect(ERROR_TEMPLATE.name); } /// Render template with error context diff --git a/src/pages/mod.rs b/src/pages/mod.rs index 7b929f2..bd986b0 100644 --- a/src/pages/mod.rs +++ b/src/pages/mod.rs @@ -136,6 +136,10 @@ mod tests { BASE, FOOTER, PUB_NAV, + auth::AUTH_BASE, + auth::login::LOGIN, + auth::register::REGISTER, + errors::ERROR_TEMPLATE, ] .iter() {