Replace most panics in the core library with results
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2023-04-19 18:44:11 +02:00
parent 00d596d656
commit c0e4702e45
33 changed files with 222 additions and 102 deletions

View File

@@ -1,5 +1,7 @@
use crate::static_data::Nature;
use crate::{Random, StringKey, ValueIdentifiable, ValueIdentifier};
use anyhow::bail;
use anyhow_ext::{ensure, Result};
use indexmap::IndexMap;
use std::fmt::Debug;
use std::sync::Arc;
@@ -11,9 +13,9 @@ pub trait NatureLibrary: Debug + ValueIdentifiable {
/// Gets a nature by name.
fn get_nature(&self, key: &StringKey) -> Option<Arc<dyn Nature>>;
/// Gets a random nature.
fn get_random_nature(&self, rand: &mut Random) -> Arc<dyn Nature>;
fn get_random_nature(&self, rand: &mut Random) -> Result<Arc<dyn Nature>>;
/// Finds a nature name by nature.
fn get_nature_name(&self, nature: &Arc<dyn Nature>) -> StringKey;
fn get_nature_name(&self, nature: &Arc<dyn Nature>) -> Result<StringKey>;
}
/// A library of all natures that can be used, stored by their names.
@@ -46,21 +48,28 @@ impl NatureLibrary for NatureLibraryImpl {
self.map.get(key).cloned()
}
fn get_random_nature(&self, rand: &mut Random) -> Arc<dyn Nature> {
fn get_random_nature(&self, rand: &mut Random) -> Result<Arc<dyn Nature>> {
ensure!(!self.map.is_empty(), "No natures were loaded into the library.");
let i = rand.get_between(0, self.map.len() as i32);
return self.map.get_index(i as usize).unwrap().1.clone();
Ok(self
.map
.get_index(i as usize)
// this should never happen, but we'll check anyway.
.ok_or(anyhow_ext::anyhow!("Failed to get a random nature from the library."))?
.1
.clone())
}
/// Finds a nature name by nature.
fn get_nature_name(&self, nature: &Arc<dyn Nature>) -> StringKey {
fn get_nature_name(&self, nature: &Arc<dyn Nature>) -> Result<StringKey> {
for kv in &self.map {
// As natures can't be copied, and should always be the same reference as the value
// in the map, we just compare by reference.
if kv.1.value_identifier() == nature.value_identifier() {
return kv.0.clone();
return Ok(kv.0.clone());
}
}
panic!("No name was found for the given nature. This should never happen.");
bail!("No name was found for the given nature. The nature may not be in the library.");
}
}
@@ -71,6 +80,9 @@ impl ValueIdentifiable for NatureLibraryImpl {
}
#[cfg(test)]
#[allow(clippy::indexing_slicing)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::expect_used)]
pub mod tests {
use super::*;
use crate::static_data::statistics::Statistic;
@@ -93,8 +105,8 @@ pub mod tests {
impl NatureLibrary for NatureLibrary {
fn load_nature(&mut self, name: StringKey, nature: Arc<dyn Nature>);
fn get_nature(&self, key: &StringKey) -> Option<Arc<dyn Nature>>;
fn get_nature_name(&self, nature: &Arc<dyn Nature>) -> StringKey;
fn get_random_nature(&self, rand: &mut Random) -> Arc<dyn Nature>;
fn get_nature_name(&self, nature: &Arc<dyn Nature>) -> Result<StringKey>;
fn get_random_nature(&self, rand: &mut Random) -> Result<Arc<dyn Nature>>;
}
impl ValueIdentifiable for NatureLibrary {
fn value_identifier(&self) -> ValueIdentifier{
@@ -134,10 +146,10 @@ pub mod tests {
);
let n1 = lib.get_nature(&"foo".into()).expect("Nature was not found");
let name = lib.get_nature_name(&n1);
let name = lib.get_nature_name(&n1).unwrap();
assert_eq!(name, "foo".into());
let n2 = lib.get_nature(&"bar".into()).expect("Nature was not found");
let name2 = lib.get_nature_name(&n2);
let name2 = lib.get_nature_name(&n2).unwrap();
assert_eq!(name2, "bar".into());
}
@@ -148,7 +160,7 @@ pub mod tests {
"foo".into(),
NatureImpl::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
);
let n1 = lib.get_random_nature(&mut Random::new(0));
let n1 = lib.get_random_nature(&mut Random::new(0)).unwrap();
assert_eq!(n1.increased_stat(), Statistic::HP);
assert_eq!(n1.decreased_stat(), Statistic::Attack);
assert_eq!(n1.get_stat_modifier(n1.increased_stat()), 1.1);