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 Oct 24, 2024
2 parents 3c64393 + e50b513 commit c6cb8ff
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/PKRust/CreatureData/pokemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct Pokemon {
impl Pokemon {
/// Constructor for a Pokemon, when being read from a save file
pub fn get(index: i16, level:i8, nickname: String, moves: Vec<Move>, ot: u16, otn: String, hp: i16, evArr: [u16;5], ivArr: [u16;5], statArr: [u16;5]) -> Pokemon {
let species = Species::parse(index);
let species = Species::parse(index).unwrap();

let evs = EVs{hp: evArr[0], atk: evArr[1], def: evArr[2], spd: evArr[3], spc: evArr[4]};
let ivs = IVs{atk: ivArr[0], def: ivArr[1], spd: ivArr[2], spc: ivArr[3], hp: ivArr[4]};
Expand Down
24 changes: 17 additions & 7 deletions src/PKRust/CreatureData/pokemonMove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Move {
}
impl Move {
/// Constructor for a Move, given an input move index
pub fn get(index: u16, pp: u16, ppup: u8) -> Move {
pub fn get(index: u16, pp: u16, ppup: u8) -> Result<Move, String> {
let moveFile = fs::read_to_string("./data/moves.pkmn").unwrap();
let strIndex = format!("{:03}",index);
let mut moveLine: &str = "No Move found";
Expand All @@ -25,9 +25,9 @@ impl Move {

let parsedMove: Vec<&str> = moveLine.split(" ").collect();
let name = parsedMove[1].to_string().replacen('+', " ", 1);
let typing = Type::get(parsedMove[2].parse::<i16>().unwrap());
let typing = Type::get(parsedMove[2].parse::<i16>().map_err(|_| format!("Move with ID {index} not found."))?);

return Move{index,typing,name,pp,ppup};
return Ok(Move{index,typing,name,pp,ppup});
}
/// Constructor for an empty Move slot
pub fn empty() -> Move {
Expand Down Expand Up @@ -65,19 +65,29 @@ pub mod tests {
use super::*;

#[test]
fn get_CorrectMove() {
let testMove:Move = Move::get(001, 3, 0);
fn get_testCorrectMove() {
let testMove:Move = Move::get(001, 3, 0).unwrap();

assert_eq!(testMove.getName(), "Pound");
assert_eq!(testMove.getTyping(), &Type::Normal);
}

#[test]
fn correctStringMove() {
let testMove:Move = Move::get(001, 5, 10);
fn to_string_testCorrectStringMove() {
let testMove:Move = Move::get(001, 5, 10).unwrap();
let stringMove: String = testMove.to_string();

assert_eq!(stringMove, "Pound PP: 5 PP Up: 10")
}

#[test]
fn get_testIncorrectMoveTyping() {
let index = 000;

// Assert that an error is returned
assert!(Move::get(index, 0, 0).is_err());
// Assert that the error is correct
assert_eq!(Move::get(index, 0, 0).unwrap_err(), "Move with ID 0 not found.");
}

}
20 changes: 15 additions & 5 deletions src/PKRust/CreatureData/pokemonSpecies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ pub struct Species {
typing: [Type;2],
}
impl Species {
pub fn parse(index: i16) -> Species {
pub fn parse(index: i16) -> Result<Species, String> {
let speciesFile = fs::read_to_string("./data/species.pkmn").unwrap();
let mut parsedSpecies: &str = "No Pokemon found";
let mut parsedSpecies: &str = " ";

let hexIndex = format!("0x{:02X?}",index);
// println!("{}",hexIndex);
Expand All @@ -32,7 +32,7 @@ impl Species {
}

let info: Vec<&str> = parsedSpecies.split(" ").collect();
let pokedex = info[0].parse::<i16>().unwrap();
let pokedex = info[0].parse::<i16>().map_err(|_| format!("Species with ID {hexIndex} not found."))?;
let name = info[2].to_string();

// PLEASE fix this later, I don't even want to explain what horribleness I wrote here
Expand All @@ -42,7 +42,7 @@ impl Species {
Type::get(types[1].parse::<i16>().unwrap())
];

return Species{index,pokedex,name,typing};
return Ok(Species{index,pokedex,name,typing});
}

pub fn getIndex(&self) -> &i16 {
Expand Down Expand Up @@ -71,7 +71,7 @@ mod tests {
fn parse_testCorrectPokemon() {
// Id of the pokemon
let id = 0x99;
let parsedSpecies: Species = Species::parse(id);
let parsedSpecies: Species = Species::parse(id).unwrap();

let correctSpecies:Species = Species {
index: 0x99,
Expand All @@ -87,4 +87,14 @@ mod tests {
assert_eq!(&correctSpecies.getPokedex(), &parsedSpecies.getPokedex());
}

#[test]
fn parse_testIncorrectIndex() {
let incorrectID: i16 = 0x00;

// Assert that an error is returned
assert!(Species::parse(incorrectID).is_err());
// Assert that the error is the one we expect
assert_eq!(Species::parse(incorrectID).unwrap_err(), "Species with ID 0x00 not found.");
}

}
2 changes: 1 addition & 1 deletion src/PKRust/saveLoader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ impl Save {
if moveIndex == 0 {
returnVec.push(Move::empty());
} else {
returnVec.push(Move::get(moveIndex, currPP, currPPUp));
returnVec.push(Move::get(moveIndex, currPP, currPPUp).unwrap());
}
}

Expand Down

0 comments on commit c6cb8ff

Please sign in to comment.