use std::fmt; use std::fmt::Formatter; use rand::Rng; pub struct RollResult { rolls: Vec, } impl RollResult { fn new() -> Self { RollResult { rolls: Vec::new(), } } fn add_roll(&mut self, roll: u32) { self.rolls.push(roll); } fn total(&self) -> u32 { self.rolls.iter().sum() } } impl fmt::Display for RollResult { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "{}", self.total()) } } /// Roll a single die with the specified number of sides. pub fn roll_die(sides: u32) -> u32 { let mut rng = rand::thread_rng(); rng.gen_range(1..=sides) } /// Parse and roll dice from a dice formula (e.g. "4d6+2" or "d12*3"). pub fn roll_formula(formula: &str) -> Option { let delimiters = ['d', '+', '-', '*', '/']; // Only d implemented right now. let parts: Vec<&str> = formula.split(|c| delimiters.contains(&c)).collect(); if parts.len() < 2 { return None; } let num_dice = if parts[0].is_empty() { 1 } else { match parts[0].parse::() { Ok(num) => num, Err(_) => return None, } }; let sides = match parts[1].parse::() { Ok(num) => num, Err(_) => return None, }; let mut roll_result = RollResult::new(); for _ in 0..num_dice { let roll = roll_die(sides); roll_result.add_roll(roll); } Some(roll_result) }