PkmnLib_rs/src/static_data/libraries/library_settings.rs

50 lines
1.7 KiB
Rust
Executable File

use crate::defines::LevelInt;
use anyhow::Result;
use anyhow_ext::ensure;
use std::fmt::Debug;
/// This library holds several misc settings for the library.
pub trait LibrarySettings: Debug {
/// The highest level a Pokemon can be.
fn maximum_level(&self) -> LevelInt;
/// The chance of a Pokemon being shiny, as the denominator of a fraction, where the nominator
/// is 1. For example, if this is 1000, then the chance of a Pokemon being shiny is 1/1000.
fn shiny_rate(&self) -> u32;
}
/// This library holds several misc settings for the library.
#[derive(Debug)]
pub struct LibrarySettingsImpl {
/// The highest level a Pokemon can be.
maximum_level: LevelInt,
/// The chance of a Pokemon being shiny, as the denominator of a fraction, where the nominator
/// is 1. For example, if this is 1000, then the chance of a Pokemon being shiny is 1/1000.
shiny_rate: u32,
}
impl LibrarySettingsImpl {
/// Creates a new settings library.
/// - `maximum_level` is the highest level a Pokemon can be.
/// - `shiny_rate` is the chance of a Pokemon being shiny, as the denominator of a fraction, where
/// the nominator is 1. For example, if this is 1000, then the chance of a Pokemon being shiny is
/// 1/1000.
pub fn new(maximum_level: LevelInt, shiny_rate: u32) -> Result<Self> {
ensure!(shiny_rate >= 1);
ensure!(maximum_level >= 1);
Ok(Self {
maximum_level,
shiny_rate,
})
}
}
impl LibrarySettings for LibrarySettingsImpl {
fn maximum_level(&self) -> LevelInt {
self.maximum_level
}
fn shiny_rate(&self) -> u32 {
self.shiny_rate
}
}