dnd_dice_roller/
dice_result.rs1use std::fmt;
2
3#[derive(PartialEq, Debug)]
5pub struct DiceSetResults {
6 pub dice_results: Vec<RollResult>,
8 pub final_result: i32,
10}
11
12impl DiceSetResults {
13 pub(crate) fn new(results: Vec<RollResult>, final_result: i32) -> Self {
14 DiceSetResults {
15 dice_results: results,
16 final_result,
17 }
18 }
19}
20
21#[derive(PartialEq, Debug)]
22pub struct RollResult {
23 pub first_roll: Vec<u32>,
25 pub second_roll: Option<Vec<u32>>,
27 pub result: i32,
28}
29
30impl RollResult {
31 pub(crate) fn new(first_roll: Vec<u32>, second_roll: Option<Vec<u32>>, result: i32) -> Self {
32 RollResult {
33 first_roll,
34 second_roll,
35 result,
36 }
37 }
38}
39
40impl fmt::Display for RollResult {
41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 match &self.second_roll {
43 None => write!(f, "{:?}", self.first_roll),
44 Some(second_roll) => write!(f, "[{:?}, {:?}]", self.first_roll, second_roll),
45 }
46 }
47}
48
49#[cfg(test)]
50mod test {
51 use super::*;
52
53 #[test]
54 fn format_raw_result_with_only_one_roll() {
55 let raw_result = RollResult::new(vec![1, 2, 3, 4], None, 7);
56 assert_eq!("[1, 2, 3, 4]", format!("{}", raw_result));
57 }
58
59 #[test]
60 fn format_raw_result_with_two_rolls() {
61 let raw_result = RollResult::new(vec![4, 2, 1, 3], Some(vec![5, 2, 3, 4]), 14);
62 assert_eq!("[[4, 2, 1, 3], [5, 2, 3, 4]]", format!("{}", raw_result));
63 }
64}