From 2e2a6ac6f7941c450ba56d754a9e214a0c07f508 Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Sat, 18 Apr 2020 02:00:54 +0200 Subject: [PATCH] refactor: simplify error page impl --- src/error_page.rs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/error_page.rs b/src/error_page.rs index 3af0272..34c1aa7 100644 --- a/src/error_page.rs +++ b/src/error_page.rs @@ -1,5 +1,3 @@ -extern crate iron; - use iron::mime; use iron::prelude::*; use iron::status; @@ -7,6 +5,10 @@ use iron::AfterMiddleware; use std::fs; use std::path::Path; +static PAGE_404: &str = "

404

Content could not found

"; +static PAGE_50X: &str = + "

50x

Service is temporarily unavailable due an unexpected error

"; + /// Custom Error pages middleware for Iron pub struct ErrorPage { /// HTML file content for 404 errors. @@ -18,22 +20,17 @@ pub struct ErrorPage { impl ErrorPage { /// Create a new instance of `ErrorPage` middleware with a given html pages. pub fn new>(page_404_path: P, page_50x_path: P) -> ErrorPage { - let page404: String; - let page50x: String; - - if Path::new(&page_404_path.as_ref()).exists() { - page404 = fs::read_to_string(page_404_path).unwrap(); + let page404 = if Path::new(&page_404_path.as_ref()).exists() { + fs::read_to_string(page_404_path).unwrap() } else { - page404 = String::from("

404

Content could not found

"); - } + String::from(PAGE_404) + }; - if Path::new(&page_50x_path.as_ref()).exists() { - page50x = fs::read_to_string(page_50x_path).unwrap(); + let page50x = if Path::new(&page_50x_path.as_ref()).exists() { + fs::read_to_string(page_50x_path).unwrap() } else { - page50x = String::from( - "

50x

Service is temporarily unavailable due an unexpected error

", - ); - } + String::from(PAGE_50X) + }; ErrorPage { page404, page50x } } -- libgit2 1.7.2