review: change Die to enum to handle range and value list in the future

This commit is contained in:
2025-11-16 10:01:13 +01:00
parent 4d4810b52b
commit 23185bfd86
6 changed files with 49 additions and 21 deletions

View File

@@ -1,10 +0,0 @@
use crate::*;
impl Die {
pub fn new(sides: u16) -> Self {
Self { sides }
}
}
// #[cfg(test)]
// mod unit_tests { use super::*; }

View File

@@ -2,7 +2,12 @@ use crate::*;
impl Die { impl Die {
pub fn roll(&self) -> i32 { pub fn roll(&self) -> i32 {
fn roll_sides(sides: u16) -> i32 {
let mut rng = rand::rng(); let mut rng = rand::rng();
rng.random_range(1..=self.sides) as i32 rng.random_range(1..=sides) as i32
}
match self {
Die::Sides(sides) => roll_sides(*sides),
}
} }
} }

View File

@@ -4,7 +4,7 @@ impl Handful {
pub fn grab(dice_num: u16, sides: u16) -> Self { pub fn grab(dice_num: u16, sides: u16) -> Self {
let mut dice = Vec::<Die>::new(); let mut dice = Vec::<Die>::new();
for _ in 1..=dice_num { for _ in 1..=dice_num {
dice.push(Die::new(sides)); dice.push(Die::Sides(sides));
} }
Self { dice } Self { dice }
} }

View File

@@ -1,4 +1,3 @@
pub mod die_new;
pub mod die_roll; pub mod die_roll;
pub mod handful_grab; pub mod handful_grab;
pub mod handful_roll; pub mod handful_roll;

View File

@@ -8,7 +8,7 @@ mod str;
mod trt; mod trt;
mod tst; mod tst;
pub(crate) use crate::str::die::O as Die; pub(crate) use crate::str::die::E as Die;
pub(crate) use crate::str::failed_to::E as FailedTo; pub(crate) use crate::str::failed_to::E as FailedTo;
pub(crate) use crate::str::handful::O as Handful; pub(crate) use crate::str::handful::O as Handful;
pub(crate) use crate::str::outcome::E as Outcome; pub(crate) use crate::str::outcome::E as Outcome;

View File

@@ -1,17 +1,51 @@
use crate::*; use crate::*;
// use std::fmt::Display;
// use std::str::FromStr;
#[derive(Debug, Default, PartialEq, Clone)] #[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Hash)]
pub struct O { pub enum E {
pub(crate) sides: u16, Sides(u16),
} }
// impl std::fmt::Display for O { // impl Display for E {
// fn fmt(&self, _f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { // fn fmt(&self, f: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
// todo!(); // match self {
// Self::NoValue => write!(f, "NoValue"),
// }
// }
// }
// impl FromStr for E {
// type Err = Box<dyn std::error::Error>;
// fn from_str(value: &str) -> std::result::Result<Self, Box<dyn std::error::Error>> {
// match value {
// "NoValue" => Ok(Self::NoValue),
// _ => unreachable!(),
// }
// } // }
// } // }
// #[cfg(test)] // #[cfg(test)]
// mod unit_tests { // mod unit_tests {
// use super::*; // use super::*;
// #[test]
// fn check_001() {
// for value in [E::NoValue] {
// match value {
// E::NoValue => assert_eq!(
// E::NoValue,
// E::from_str("NoValue").unwrap()
// ),
// }
// }
// }
//
// #[test]
// fn check_002() {
// for value in [E::NoValue] {
// match value {
// E::NoValue => assert_eq!(&E::NoValue.to_string(), "NoValue"),
// }
// }
// }
// } // }