From f4312463215b5763e6e2970d32cbfa46b744f1f4 Mon Sep 17 00:00:00 2001 From: Delta1925 Date: Tue, 18 Apr 2023 18:18:16 +0200 Subject: [PATCH] Refactor some code --- backend/src/confirmation.rs | 2 +- backend/src/main.rs | 6 +++--- backend/src/management.rs | 7 +++---- backend/src/utils.rs | 28 +++++++++------------------- 4 files changed, 16 insertions(+), 27 deletions(-) diff --git a/backend/src/confirmation.rs b/backend/src/confirmation.rs index 20ae5fd..cc39d40 100644 --- a/backend/src/confirmation.rs +++ b/backend/src/confirmation.rs @@ -13,7 +13,7 @@ use std::fs; use std::path::Path; pub fn confirm_action(token: &str) -> Result<(Action, String)> { - let pending_path = pending_path!().join(token); + let pending_path = pending_path().join(token); let content = read_file(&pending_path)?; let key = toml::from_str::(&content)?; if Utc::now().timestamp() - key.timestamp() > SETTINGS.max_age { diff --git a/backend/src/main.rs b/backend/src/main.rs index f96387d..dfe17c7 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -24,7 +24,7 @@ use std::env; use std::fs; use std::path::Path; use tokio::{task, time}; -use utils::init_logger; +use utils::{init_logger, pending_path, webpage_path}; #[derive(Deserialize, Debug)] struct Key { @@ -49,7 +49,7 @@ async fn main() -> std::io::Result<()> { if init_logger().is_err() { panic!("Could not set up logger!") }; - fs::create_dir_all(pending_path!())?; + fs::create_dir_all(pending_path())?; task::spawn(async { let mut metronome = time::interval(time::Duration::from_secs(SETTINGS.cleanup_interval)); loop { @@ -74,7 +74,7 @@ async fn main() -> std::io::Result<()> { } async fn index(req: HttpRequest) -> Result { - let path = webpage_path!().join(req.match_info().query("filename")); + let path = webpage_path().join(req.match_info().query("filename")); for file in &["", "index.html"] { let path = if file.is_empty() { path.to_owned() diff --git a/backend/src/management.rs b/backend/src/management.rs index 35ca412..4b40138 100644 --- a/backend/src/management.rs +++ b/backend/src/management.rs @@ -1,6 +1,5 @@ -use crate::pending_path; use crate::settings::ROOT_FOLDER; -use crate::utils::{get_user_file_path, key_exists, read_file}; +use crate::utils::{get_user_file_path, key_exists, read_file, pending_path}; use anyhow::Result; use chrono::Utc; @@ -55,7 +54,7 @@ impl Pending { fn store_pending(pending: &Pending, token: &str) -> Result<()> { let serialized = toml::to_string(pending)?; - fs::write(pending_path!().join(token), serialized)?; + fs::write(pending_path().join(token), serialized)?; Ok(()) } @@ -73,7 +72,7 @@ pub fn store_pending_deletion(email: String, token: &str) -> Result<()> { } pub fn clean_stale(max_age: i64) { - for path in fs::read_dir(pending_path!()).unwrap().flatten() { + for path in fs::read_dir(pending_path()).unwrap().flatten() { let file_path = path.path(); let content = match read_file(&file_path) { Ok(content) => content, diff --git a/backend/src/utils.rs b/backend/src/utils.rs index 3d03cb5..c985572 100644 --- a/backend/src/utils.rs +++ b/backend/src/utils.rs @@ -21,20 +21,6 @@ use std::{ path::{Path, PathBuf}, }; -#[macro_export] -macro_rules! pending_path { - () => { - Path::new(&ROOT_FOLDER).join("pending") - }; -} - -#[macro_export] -macro_rules! webpage_path { - () => { - Path::new("assets").join("webpage") - }; -} - #[macro_export] macro_rules! validate_cert { ( $x:expr ) => { @@ -45,6 +31,14 @@ macro_rules! validate_cert { }; } +pub fn pending_path() -> PathBuf { + Path::new(&ROOT_FOLDER).join("pending") +} + +pub fn webpage_path() -> PathBuf { + Path::new("assets").join("webpage") +} + pub fn read_file(path: &PathBuf) -> Result { if path.is_file() { Ok(fs::read_to_string(path)?) @@ -101,10 +95,6 @@ pub fn key_exists(email: &str) -> Result { Ok(true) } -pub fn _get_filename(path: &Path) -> Option<&str> { - path.file_name()?.to_str() -} - pub fn custom_color_format( w: &mut dyn std::io::Write, now: &mut DeferredNow, @@ -150,7 +140,7 @@ pub fn init_logger() -> Result { } pub fn return_outcome(data: Result<&str, &CompatErr>) -> Result { - let path = webpage_path!().join("status").join("index.html"); + let path = webpage_path().join("status").join("index.html"); let template = read_file(&path)?; let (page, message) = match data { Ok(message) => (template.replace("((%s))", "Success!"), message.to_string()),