Support for new error handling.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2023-04-15 14:34:42 +02:00
parent 3058739ea0
commit feffb5f030
36 changed files with 466 additions and 274 deletions

View File

@@ -1,3 +1,4 @@
use anyhow::Result;
use std::fmt;
use std::fmt::{Debug, Formatter};
@@ -12,7 +13,7 @@ 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;
/// Calculates the experience for a given growth key name and a certain level.
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> u32;
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> Result<u32>;
/// Adds a new growth rate with a name and value.
fn add_growth_rate(&mut self, key: &StringKey, value: Box<dyn GrowthRate>);
}
@@ -41,7 +42,7 @@ impl GrowthRateLibrary for GrowthRateLibraryImpl {
self.growth_rates[growth_rate].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) -> u32 {
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> Result<u32> {
self.growth_rates[growth_rate].calculate_experience(level)
}
@@ -89,7 +90,7 @@ pub mod tests {
pub GrowthRateLibrary{}
impl GrowthRateLibrary for GrowthRateLibrary {
fn calculate_level(&self, growth_rate: &StringKey, experience: u32) -> LevelInt;
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> u32;
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> Result<u32>;
fn add_growth_rate(&mut self, key: &StringKey, value: Box<dyn GrowthRate>);
}
impl ValueIdentifiable for GrowthRateLibrary {
@@ -109,7 +110,7 @@ pub mod tests {
#[test]
fn add_growth_rate_to_library_and_calculate_experience() {
let lib = build();
assert_eq!(lib.calculate_experience(&"test_growthrate".into(), 1), 0);
assert_eq!(lib.calculate_experience(&"test_growthrate".into(), 3), 10);
assert_eq!(lib.calculate_experience(&"test_growthrate".into(), 1).unwrap(), 0);
assert_eq!(lib.calculate_experience(&"test_growthrate".into(), 3).unwrap(), 10);
}
}