A bunch more work on replacing every potential panic with results
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-04-16 19:57:21 +02:00
parent 1b8403ecda
commit 00d596d656
37 changed files with 526 additions and 300 deletions

View File

@@ -1,4 +1,5 @@
use crate::defines::LevelInt;
use crate::VecExt;
use anyhow::Result;
use anyhow_ext::ensure;
@@ -37,9 +38,12 @@ impl GrowthRate for LookupGrowthRate {
fn calculate_experience(&self, level: LevelInt) -> Result<u32> {
ensure!(level > 0, "Level must be greater than 0, but was {}", level);
if level >= self.experience.len() as LevelInt {
Ok(*self.experience.last().unwrap())
match self.experience.last() {
Some(v) => Ok(*v),
None => anyhow::bail!("No experience values found"),
}
} else {
Ok(self.experience[(level - 1) as usize])
Ok(*self.experience.get_res((level - 1) as usize)?)
}
}
}

View File

@@ -1,3 +1,4 @@
use anyhow_ext::Result;
use std::sync::Arc;
use indexmap::IndexMap;
@@ -48,8 +49,11 @@ pub trait DataLibrary<T: ?Sized> {
}
/// Gets a random value from the library.
fn random_value(&self, rand: &mut Random) -> &Arc<T> {
fn random_value(&self, rand: &mut Random) -> Result<&Arc<T>> {
let i = rand.get_between(0, self.len() as i32);
return self.map().get_index(i as usize).unwrap().1;
match self.map().get_index(i as usize) {
Some(v) => Ok(v.1),
None => anyhow::bail!("No value found for index {}", i),
}
}
}