1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use std::fmt;

use crate::{AttackOutcome, CheckOutcome, DamageOutcome};

/// The outcome of rolling a check, damage, or attack roll.
///
/// This is normally constructed as the result of calling `roll()` on a
/// `Roll` roll expression.
///
/// A `RollOutcome` can be printed with `Display` and `Debug`, but if
/// you need more information about the result you will need to
/// destructure the enum and handle the different types individually.
///
/// ```
/// use critfail::{RollExpression, Roll, RollOutcome};
///
/// let check: RollOutcome = Roll::new("r+6").unwrap().roll();
/// let damage: RollOutcome = Roll::new("4d4+6").unwrap().roll();
/// let attack: RollOutcome = Roll::new("r+3?2d8+3").unwrap().roll();
///
/// fn print_score(outcome: RollOutcome) {
///     match outcome {
///         RollOutcome::Check(check) => println!("Check score: {}", check.score()),
///         RollOutcome::Damage(damage) => println!("Damage score: {}", damage.score()),
///         RollOutcome::Attack(attack) => {
///             println!("Check score: {}", attack.check().score());
///             println!("Damage score: {}", attack.damage().score())
///         }
///     }
/// }
///
/// print_score(check);
/// print_score(damage);
/// print_score(attack);
/// ```
#[derive(Clone)]
pub enum RollOutcome {
    /// The outcome of a `Roll` that contained a `Check`.
    Check(CheckOutcome),
    /// The outcome of a `Roll` that contained a `Damage`.
    Damage(DamageOutcome),
    /// The outcome of a `Roll` that contained an `Attack`.
    Attack(AttackOutcome),
}

impl RollOutcome {
    /// Return true if this `RollOutcome` is the outcome of a check roll.
    ///
    /// ```
    /// use critfail::{RollExpression, Roll};
    ///
    /// assert_eq!(Roll::new("r+3").unwrap().roll().is_check(), true);
    /// assert_eq!(Roll::new("2d8+5").unwrap().roll().is_check(), false);
    /// assert_eq!(Roll::new("r+3?2d8+5").unwrap().roll().is_check(), false);
    /// ```
    pub fn is_check(&self) -> bool {
        match self {
            Self::Check(_) => true,
            _ => false,
        }
    }

    /// Return true if this `RollOutcome` is the outcome of a damage roll.
    ///
    /// ```
    /// use critfail::{RollExpression, Roll};
    ///
    /// assert_eq!(Roll::new("r+3").unwrap().roll().is_damage(), false);
    /// assert_eq!(Roll::new("2d8+5").unwrap().roll().is_damage(), true);
    /// assert_eq!(Roll::new("r+3?2d8+5").unwrap().roll().is_damage(), false);
    /// ```
    pub fn is_damage(&self) -> bool {
        match self {
            Self::Damage(_) => true,
            _ => false,
        }
    }

    /// Return true if this `RollOutcome` is the outcome of an attack roll.
    ///
    /// ```
    /// use critfail::{RollExpression, Roll};
    ///
    /// assert_eq!(Roll::new("r+3").unwrap().roll().is_attack(), false);
    /// assert_eq!(Roll::new("2d8+5").unwrap().roll().is_attack(), false);
    /// assert_eq!(Roll::new("r+3?2d8+5").unwrap().roll().is_attack(), true);
    /// ```
    pub fn is_attack(&self) -> bool {
        match self {
            Self::Attack(_) => true,
            _ => false,
        }
    }
}

impl fmt::Display for RollOutcome {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            RollOutcome::Check(c) => write!(f, "{}", c),
            RollOutcome::Damage(d) => write!(f, "{}", d),
            RollOutcome::Attack(a) => write!(f, "{}", a),
        }
    }
}

impl fmt::Debug for RollOutcome {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RollOutcome::Check(c) => write!(f, "{:?}", c),
            RollOutcome::Damage(d) => write!(f, "{:?}", d),
            RollOutcome::Attack(a) => write!(f, "{:?}", a),
        }
    }
}

impl From<CheckOutcome> for RollOutcome {
    fn from(o: CheckOutcome) -> Self {
        Self::Check(o)
    }
}

impl From<DamageOutcome> for RollOutcome {
    fn from(o: DamageOutcome) -> Self {
        Self::Damage(o)
    }
}

impl From<AttackOutcome> for RollOutcome {
    fn from(o: AttackOutcome) -> Self {
        Self::Attack(o)
    }
}