Skip to content

Commit

Permalink
Merge branch 'rewrite/error_handling'
Browse files Browse the repository at this point in the history
  • Loading branch information
CatRass committed Nov 2, 2024
2 parents 8123d8d + 97fe08f commit 5d1d7fb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/PKRust/saveLoader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");

}

Expand All @@ -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));

}

Expand Down
9 changes: 9 additions & 0 deletions src/PKRust/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ pub fn integrityCheck(saveFile: &Vec<u8>) -> 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 {
Expand Down
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -16,6 +16,13 @@ fn main() {
// The read_line collects line endings, so we need to remove them.
let saveLoc = saveLocInput.trim();

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();
}

0 comments on commit 5d1d7fb

Please sign in to comment.