Skip to content

Commit

Permalink
Added Ability to Select Save From CLI
Browse files Browse the repository at this point in the history
- Added input for user to select location of save file
- Renamed unexpected file loading error from 'Error' to 'Unexpected Error'
  • Loading branch information
CatRass committed Oct 31, 2024
1 parent ee8f21c commit 17f934b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/PKRust/saveLoader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@ impl Save {
}
}

pub fn load(file: &'static str) -> Result<Save, String>{
pub fn load(file: &str) -> Result<Save, String>{

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

Expand Down
21 changes: 20 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 17f934b

Please sign in to comment.