From 17f934b214651873dd3c00b6935cf4a8598439ae Mon Sep 17 00:00:00 2001 From: Darrel Malkin Date: Thu, 31 Oct 2024 14:41:11 +1100 Subject: [PATCH] Added Ability to Select Save From CLI - Added input for user to select location of save file - Renamed unexpected file loading error from 'Error' to 'Unexpected Error' --- src/PKRust/saveLoader.rs | 6 ++++-- src/main.rs | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/PKRust/saveLoader.rs b/src/PKRust/saveLoader.rs index 64e745f..80815b9 100644 --- a/src/PKRust/saveLoader.rs +++ b/src/PKRust/saveLoader.rs @@ -31,17 +31,19 @@ impl Save { } } - pub fn load(file: &'static str) -> Result{ + pub fn load(file: &str) -> Result{ let filePathBuf:PathBuf = std::path::PathBuf::from(file); + 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)), - _ => return Err(format!("Error: {}",error.kind())) + _ => return Err(format!("Unexpected Error: {}",error.kind())) } }; diff --git a/src/main.rs b/src/main.rs index 940becb..dd8f143 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,28 @@ #![allow(non_snake_case)] pub mod PKRust; +use std::io::{self, Write}; + use PKRust::saveLoader::Save; fn main() { - let saveFile = Save::load("./test/POKEMON YELLOW 2.sav").unwrap(); + + io::stdout().write_all(b"Please Select a Save File: ").unwrap(); + io::stdout().flush().unwrap(); + + let mut saveLocInput = String::new(); + io::stdin().read_line(&mut saveLocInput).expect("Error recieving User Input"); + + let mut saveLoc = ""; + // The read_line collects line endings, so we need to remove them. + // This varies across different OS' though, so I've split it + // into Unix and Windows + if cfg!(windows) { + saveLoc = saveLocInput.strip_suffix("\r\n").unwrap(); + } else if cfg!(unix) { + saveLoc = saveLocInput.strip_suffix("\n").unwrap(); + } + + let saveFile = Save::load(saveLoc).unwrap(); saveFile.print(); }