dicelib/
error.rs

1//! Errors that can occur in this crate.
2
3use crate::dice::DiceRoll;
4use thiserror::Error;
5
6/// Things that can go wrong when rolling dice.
7#[derive(Debug, Clone, Error)]
8pub enum RollError {
9    /// Attempted to add two integers which would overflow (usually because the
10    /// dice roll is too big)
11    #[error("Adding integers {0} and {1} would overflow")]
12    IntegerOverFlow(u32, u32),
13}
14
15/// Things that can go wrong when instantiating a Dice
16#[derive(Debug, Clone, Error)]
17pub enum Error {
18    /// Attempted to create a DiceRoll with too few sides.
19    #[error(
20        "'{0}' is an invalid number of sides for a Dice (must be at least {})",
21        DiceRoll::MINIMUM_SIDES
22    )]
23    InvalidSides(u32),
24
25    /// Attempted to create a DiceRoll with too few rolls.
26    #[error(
27        "'{0}' is an invalid number of rolls for a Dice (must be at least {})",
28        DiceRoll::MINIMUM_ROLLS
29    )]
30    InvalidRolls(u32),
31
32    /// Sides were not captured from the provided expression.
33    #[error("Sides were not captured from the provided expression '{0}'.")]
34    FailedToParseSides(String),
35
36    /// Rolls were not captured from the provided expression.
37    #[error("Rolls were not captured from the provided expression '{0}'.")]
38    FailedToParseRolls(String),
39
40    /// Attempted to create a DiceRoll from an invalid string expression.
41    #[error("'{0}' could not be parsed into a pair of rolls and sides.")]
42    InvalidExpression(String),
43}