From 2eafbaad41ff70650bc836ef2eb208124ae3c2de Mon Sep 17 00:00:00 2001 From: tuxmain Date: Tue, 8 Dec 2020 11:05:59 +0100 Subject: [PATCH] Clean code --- src/cli.rs | 2 +- src/config.rs | 2 +- src/db.rs | 4 ++-- src/static_files.rs | 6 +++--- src/templates.rs | 10 +++++----- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index a5dd098..0e88715 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -11,7 +11,7 @@ impl Default for ConfigPath { fn default() -> ConfigPath { ConfigPath( dirs::config_dir() - .unwrap_or(Path::new(".config").to_path_buf()) + .unwrap_or_else(|| Path::new(".config").to_path_buf()) .join("gmarche"), ) } diff --git a/src/config.rs b/src/config.rs index 17a86f2..68ed484 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,7 +4,7 @@ use std::{ path::Path, }; -const CONFIG_FILE: &'static str = "config.json"; +const CONFIG_FILE: &str = "config.json"; #[derive(Deserialize, Serialize)] pub struct Config { diff --git a/src/db.rs b/src/db.rs index 7e3a480..ba9fcf2 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,9 +1,9 @@ use sled::{Db, Tree}; use std::path::Path; -const DB_DIR: &'static str = "db"; +const DB_DIR: &str = "db"; -const DB_NAME_ADS: &'static str = "ads"; +const DB_NAME_ADS: &str = "ads"; pub struct Dbs { pub db: Db, diff --git a/src/static_files.rs b/src/static_files.rs index d9f8456..9154e21 100644 --- a/src/static_files.rs +++ b/src/static_files.rs @@ -1,7 +1,7 @@ use std::path::Path; -pub const STATIC_DIR: &'static str = "static"; -static STATIC_FILES: &'static [(&str, &[u8])] = &[ +pub const STATIC_DIR: &str = "static"; +static STATIC_FILES: &[(&str, &[u8])] = &[ ("style1.css", include_bytes!("../static/style1.css")), ("standgm.png", include_bytes!("../static/standgm.png")), ("banner.jpg", include_bytes!("../static/banner.jpg")), @@ -21,7 +21,7 @@ pub fn init_static_files(dir: &Path) { let file_path = dir.join(file); if !file_path.is_file() { std::fs::write(file_path, default) - .expect(&format!("Cannot write template file {}", file)); + .unwrap_or_else(|_| panic!("Cannot write template file {}", file)); } } } diff --git a/src/templates.rs b/src/templates.rs index 6ca18ae..f23dba2 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -1,8 +1,8 @@ use handlebars::Handlebars; use std::path::Path; -const TEMPLATES_DIR: &'static str = "templates"; -static TEMPLATE_FILES: &'static [(&str, &str)] = &[ +const TEMPLATES_DIR: &str = "templates"; +static TEMPLATE_FILES: &[(&str, &str)] = &[ ("index.html", include_str!("../templates/index.html")), ("admin.html", include_str!("../templates/admin.html")), ( @@ -23,7 +23,7 @@ pub fn load_templates<'reg>(dir: &Path) -> Templates<'reg> { let file_path = dir.join(file); if !file_path.is_file() { std::fs::write(file_path, default) - .expect(&format!("Cannot write template file {}", file)); + .unwrap_or_else(|_| panic!("Cannot write template file {}", file)); } } @@ -33,9 +33,9 @@ pub fn load_templates<'reg>(dir: &Path) -> Templates<'reg> { file, std::str::from_utf8( &std::fs::read(dir.join(file)) - .expect(&format!("Cannot read template file`{}`", file)), + .unwrap_or_else(|_| panic!("Cannot read template file`{}`", file)), ) - .expect(&format!("Bad encoding in template file `{}`", file)), + .unwrap_or_else(|_| panic!("Bad encoding in template file `{}`", file)), ) .unwrap(); }