Replace most panics in the core library with results
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use anyhow_ext::{ensure, Result};
|
||||
use hashbrown::HashSet;
|
||||
use std::fmt::Debug;
|
||||
|
||||
@@ -5,8 +6,8 @@ use crate::static_data::Statistic;
|
||||
use crate::static_data::TypeIdentifier;
|
||||
use crate::static_data::{Ability, StaticStatisticSet};
|
||||
use crate::static_data::{AbilityIndex, LearnableMoves};
|
||||
use crate::StringKey;
|
||||
use crate::{Random, ValueIdentifiable, ValueIdentifier};
|
||||
use crate::{StringKey, VecExt};
|
||||
|
||||
/// A form is a variant of a specific species. A species always has at least one form, but can have
|
||||
/// many more.
|
||||
@@ -34,7 +35,7 @@ pub trait Form: ValueIdentifiable + Debug {
|
||||
fn flags(&self) -> &HashSet<StringKey>;
|
||||
|
||||
/// Get a type of the move at a certain index.
|
||||
fn get_type(&self, index: usize) -> TypeIdentifier;
|
||||
fn get_type(&self, index: usize) -> Result<TypeIdentifier>;
|
||||
|
||||
/// Gets a single base stat value.
|
||||
fn get_base_stat(&self, stat: Statistic) -> u16;
|
||||
@@ -42,12 +43,12 @@ pub trait Form: ValueIdentifiable + Debug {
|
||||
fn find_ability_index(&self, ability: &dyn Ability) -> Option<AbilityIndex>;
|
||||
|
||||
/// Gets an ability from the form.
|
||||
fn get_ability(&self, index: AbilityIndex) -> &StringKey;
|
||||
fn get_ability(&self, index: AbilityIndex) -> Result<&StringKey>;
|
||||
|
||||
/// Gets a random ability from the form.
|
||||
fn get_random_ability(&self, rand: &mut Random) -> &StringKey;
|
||||
fn get_random_ability(&self, rand: &mut Random) -> Result<&StringKey>;
|
||||
/// Gets a random hidden ability from the form.
|
||||
fn get_random_hidden_ability(&self, rand: &mut Random) -> &StringKey;
|
||||
fn get_random_hidden_ability(&self, rand: &mut Random) -> Result<&StringKey>;
|
||||
|
||||
/// Check if the form has a specific flag set.
|
||||
fn has_flag(&self, key: &StringKey) -> bool;
|
||||
@@ -158,8 +159,8 @@ impl Form for FormImpl {
|
||||
}
|
||||
|
||||
/// Get a type of the move at a certain index.
|
||||
fn get_type(&self, index: usize) -> TypeIdentifier {
|
||||
self.types[index]
|
||||
fn get_type(&self, index: usize) -> Result<TypeIdentifier> {
|
||||
Ok(*self.types.get_res(index)?)
|
||||
}
|
||||
|
||||
/// Gets a single base stat value.
|
||||
@@ -189,21 +190,25 @@ impl Form for FormImpl {
|
||||
}
|
||||
|
||||
/// Gets an ability from the form.
|
||||
fn get_ability(&self, index: AbilityIndex) -> &StringKey {
|
||||
fn get_ability(&self, index: AbilityIndex) -> Result<&StringKey> {
|
||||
if index.hidden {
|
||||
&self.hidden_abilities[index.index as usize]
|
||||
Ok(self.hidden_abilities.get_res(index.index as usize)?)
|
||||
} else {
|
||||
&self.abilities[index.index as usize]
|
||||
Ok(self.abilities.get_res(index.index as usize)?)
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets a random ability from the form.
|
||||
fn get_random_ability(&self, rand: &mut Random) -> &StringKey {
|
||||
&self.abilities[rand.get_between_unsigned(0, self.abilities.len() as u32) as usize]
|
||||
fn get_random_ability(&self, rand: &mut Random) -> Result<&StringKey> {
|
||||
ensure!(!self.abilities.is_empty(), "No abilities on form");
|
||||
self.abilities
|
||||
.get_res(rand.get_between_unsigned(0, self.abilities.len() as u32) as usize)
|
||||
}
|
||||
/// Gets a random hidden ability from the form.
|
||||
fn get_random_hidden_ability(&self, rand: &mut Random) -> &StringKey {
|
||||
&self.hidden_abilities[rand.get_between_unsigned(0, self.hidden_abilities.len() as u32) as usize]
|
||||
fn get_random_hidden_ability(&self, rand: &mut Random) -> Result<&StringKey> {
|
||||
ensure!(!self.hidden_abilities.is_empty(), "No hidden abilities on form");
|
||||
self.hidden_abilities
|
||||
.get_res(rand.get_between_unsigned(0, self.hidden_abilities.len() as u32) as usize)
|
||||
}
|
||||
|
||||
/// Check if the form has a specific flag set.
|
||||
@@ -223,6 +228,8 @@ impl ValueIdentifiable for FormImpl {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -240,12 +247,12 @@ pub(crate) mod tests {
|
||||
fn hidden_abilities(&self) -> &Vec<StringKey>;
|
||||
fn moves(&self) -> &Box<dyn LearnableMoves>;
|
||||
fn flags(&self) -> &HashSet<StringKey>;
|
||||
fn get_type(&self, index: usize) -> TypeIdentifier;
|
||||
fn get_type(&self, index: usize) -> Result<TypeIdentifier>;
|
||||
fn get_base_stat(&self, stat: Statistic) -> u16;
|
||||
fn find_ability_index(&self, ability: &dyn Ability) -> Option<AbilityIndex>;
|
||||
fn get_ability(&self, index: AbilityIndex) -> &StringKey;
|
||||
fn get_random_ability(&self, rand: &mut Random) -> &StringKey;
|
||||
fn get_random_hidden_ability(&self, rand: &mut Random) -> &StringKey;
|
||||
fn get_ability<'a>(&'a self, index: AbilityIndex) -> Result<&'a StringKey>;
|
||||
fn get_random_ability<'a>(&'a self, rand: &mut Random) -> Result<&'a StringKey>;
|
||||
fn get_random_hidden_ability<'a>(&'a self, rand: &mut Random) -> Result<&'a StringKey>;
|
||||
fn has_flag(&self, key: &StringKey) -> bool;
|
||||
fn has_flag_by_hash(&self, key_hash: u32) -> bool;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user