feat: add display to Sum
This commit is contained in:
@@ -2,3 +2,4 @@ pub mod dice_grab;
|
||||
pub mod dice_roll;
|
||||
pub mod dice_sum;
|
||||
pub mod die_roll;
|
||||
pub mod sum_display;
|
||||
|
||||
17
01.workspace/oxydice_lib/src/imp/sum_display.rs
Normal file
17
01.workspace/oxydice_lib/src/imp/sum_display.rs
Normal 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(())
|
||||
}
|
||||
}
|
||||
@@ -2,3 +2,4 @@ pub mod dice_grab;
|
||||
pub mod dice_roll;
|
||||
pub mod dice_sum;
|
||||
pub mod die_roll;
|
||||
pub mod sum_display;
|
||||
|
||||
9
01.workspace/oxydice_lib/src/tst/sum_display.rs
Normal file
9
01.workspace/oxydice_lib/src/tst/sum_display.rs
Normal 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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user