From 97fe08fe8dc1956857136a4d6e75856940743666 Mon Sep 17 00:00:00 2001 From: Darrel Malkin Date: Sat, 2 Nov 2024 13:31:51 +1100 Subject: [PATCH] Added Better Error Handling in Main and Error Changes - Added function utils::formatError() to create coloured error messages - Implemented utils::formatError() into errors in saveLoader::Save::load() - Updated saveloader::Save::load() tests to correctly test for new errors - Updated main.rs to catch and handle errors correctly (now exits gracefully instead of panicking) --- src/PKRust/saveLoader.rs | 12 ++++++------ src/PKRust/utils.rs | 9 +++++++++ src/main.rs | 11 +++++++++-- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/PKRust/saveLoader.rs b/src/PKRust/saveLoader.rs index 80815b9..fa847eb 100644 --- a/src/PKRust/saveLoader.rs +++ b/src/PKRust/saveLoader.rs @@ -4,7 +4,7 @@ use std::path::PathBuf; use super::creatureData::pokemonMove::Move; use super::creatureData::pokemon::*; use super::addresses::*; -use super::utils::{textDecode, integrityCheck}; +use super::utils::{textDecode, integrityCheck, formatError}; #[derive(Debug)] @@ -35,21 +35,21 @@ impl Save { let filePathBuf:PathBuf = std::path::PathBuf::from(file); - println!("{:?}", filePathBuf); + // println!("{:?}", filePathBuf); // First we load the save file and check for if it exists // If not, an error result will be returned let save = match fs::read(filePathBuf) { Ok(result) => result, Err(error) => match error.kind() { - std::io::ErrorKind::NotFound => return Err(format!("Save: {} does not exist",file)), + std::io::ErrorKind::NotFound => return Err(formatError(format!("Save \"{}\" does not exist",file))), _ => return Err(format!("Unexpected Error: {}",error.kind())) } }; // Then we check if the file has integrity (Check if it's valid) if integrityCheck(&save) == false { - return Err(String::from("Error: File does not seem to be a Gen 1 Save File")); + return Err(formatError(String::from("File does not seem to be a Gen 1 Save File"))); } let pc = Self::getPCBoxesFromSave(&save); @@ -354,7 +354,7 @@ mod tests { let saveFile = Save::load(fileName); assert!(saveFile.is_err()); - assert_eq!(saveFile.unwrap_err(), "Error: File does not seem to be a Gen 1 Save File"); + assert_eq!(saveFile.unwrap_err(), "\u{1b}[0;31mError\u{1b}[0m: File does not seem to be a Gen 1 Save File"); } @@ -372,7 +372,7 @@ mod tests { let saveFile = Save::load(fileName); assert!(saveFile.is_err()); - assert_eq!(saveFile.unwrap_err(), format!("Save: {} does not exist",fileName)); + assert_eq!(saveFile.unwrap_err(), format!("\u{1b}[0;31mError\u{1b}[0m: Save \"{}\" does not exist",fileName)); } diff --git a/src/PKRust/utils.rs b/src/PKRust/utils.rs index a49ecdd..76909b6 100644 --- a/src/PKRust/utils.rs +++ b/src/PKRust/utils.rs @@ -180,6 +180,15 @@ pub fn integrityCheck(saveFile: &Vec) -> bool { } +/// A function for formatting error messages. +/// +/// Saves me from doing ANSI code tomfoolery every time I want to use an error +pub fn formatError(msg: String) -> String { + // Should ring the terminal bell + print!("\x07"); + return format!("\x1B[0;31mError\x1b[0m: {}", msg); +} + // ================ TESTS ================ #[cfg(test)] mod tests { diff --git a/src/main.rs b/src/main.rs index dd8f143..67bdc9c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ #![allow(non_snake_case)] pub mod PKRust; -use std::io::{self, Write}; +use std::{io::{self, Write}, process}; use PKRust::saveLoader::Save; @@ -23,6 +23,13 @@ fn main() { saveLoc = saveLocInput.strip_suffix("\n").unwrap(); } - let saveFile = Save::load(saveLoc).unwrap(); + let saveFile = match Save::load(saveLoc) { + // Get the correct Save if no errors + Ok(correctSave) => correctSave, + // Print the error message and exit the program + // Exits with status 1 as it is a "general error" + Err(error) => {eprintln!("{}",error); process::exit(1);} + + }; saveFile.print(); }