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

@@ -11,7 +11,7 @@ use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
/// A library to store all growth rates.
pub trait GrowthRateLibrary: Debug + ValueIdentifiable {
/// Calculates the level for a given growth key name and a certain experience.
fn calculate_level(&self, growth_rate: &StringKey, experience: u32) -> LevelInt;
fn calculate_level(&self, growth_rate: &StringKey, experience: u32) -> Result<LevelInt>;
/// Calculates the experience for a given growth key name and a certain level.
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> Result<u32>;
/// Adds a new growth rate with a name and value.
@@ -38,12 +38,19 @@ impl GrowthRateLibraryImpl {
impl GrowthRateLibrary for GrowthRateLibraryImpl {
/// Calculates the level for a given growth key name and a certain experience.
fn calculate_level(&self, growth_rate: &StringKey, experience: u32) -> LevelInt {
self.growth_rates[growth_rate].calculate_level(experience)
fn calculate_level(&self, growth_rate: &StringKey, experience: u32) -> Result<LevelInt> {
Ok(self
.growth_rates
.get(growth_rate)
.ok_or_else(|| anyhow::anyhow!("No growth rate found with key {}", growth_rate.to_string()))?
.calculate_level(experience))
}
/// Calculates the experience for a given growth key name and a certain level.
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> Result<u32> {
self.growth_rates[growth_rate].calculate_experience(level)
self.growth_rates
.get(growth_rate)
.ok_or_else(|| anyhow::anyhow!("No growth rate found with key {}", growth_rate.to_string()))?
.calculate_experience(level)
}
/// Adds a new growth rate with a name and value.
@@ -65,6 +72,8 @@ impl Debug for GrowthRateLibraryImpl {
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::indexing_slicing)]
pub mod tests {
use super::*;
use crate::static_data::growth_rates::LookupGrowthRate;
@@ -89,7 +98,7 @@ pub mod tests {
#[derive(Debug)]
pub GrowthRateLibrary{}
impl GrowthRateLibrary for GrowthRateLibrary {
fn calculate_level(&self, growth_rate: &StringKey, experience: u32) -> LevelInt;
fn calculate_level(&self, growth_rate: &StringKey, experience: u32) -> Result<LevelInt>;
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> Result<u32>;
fn add_growth_rate(&mut self, key: &StringKey, value: Box<dyn GrowthRate>);
}
@@ -103,8 +112,8 @@ pub mod tests {
#[test]
fn add_growth_rate_to_library_and_calculate_level() {
let lib = build();
assert_eq!(lib.calculate_level(&"test_growthrate".into(), 3), 1);
assert_eq!(lib.calculate_level(&"test_growthrate".into(), 50), 3);
assert_eq!(lib.calculate_level(&"test_growthrate".into(), 3).unwrap(), 1);
assert_eq!(lib.calculate_level(&"test_growthrate".into(), 50).unwrap(), 3);
}
#[test]