feat: add display to Sum

This commit is contained in:
2025-11-15 16:34:07 +01:00
parent 3e5afffb33
commit dcf97956c2
4 changed files with 28 additions and 0 deletions

View File

@@ -2,3 +2,4 @@ pub mod dice_grab;
pub mod dice_roll;
pub mod dice_sum;
pub mod die_roll;
pub mod sum_display;

View File

@@ -0,0 +1,17 @@
use crate::*;
impl fmt::Display for Sum {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let str_results: Vec<String> = self
.roll_results
.iter()
.map(|r| r.checked_add(1))
.map(|o| o.unwrap_or(0))
.map(|r| r.to_string())
.collect();
write!(f, "{}", format!("[{}]", str_results.join(", ")))?;
write!(f, " -> ")?;
write!(f, "{}", self.sum)?;
Ok(())
}
}

View File

@@ -2,3 +2,4 @@ pub mod dice_grab;
pub mod dice_roll;
pub mod dice_sum;
pub mod die_roll;
pub mod sum_display;

View File

@@ -0,0 +1,9 @@
#[cfg(test)]
mod tests {
use crate::*;
#[test]
pub fn sum_display_format_should_be_correct() {
let result = Dice::grab(3, 1).roll().sum().unwrap();
assert_eq!(&result.to_string(), "[1, 1, 1] -> 3");
}
}