Skip to main content

rdice_core/
error.rs

1//! Error types returned by `rdice_core`.
2
3use thiserror::Error;
4
5/// Errors that can occur while managing dice, trays, expressions, or storage.
6#[derive(Debug, Error)]
7pub enum DiceError {
8    /// The requested tray does not exist.
9    #[error("Tray not found: {0}")]
10    TrayNotFound(String),
11    /// A tray with the requested name already exists.
12    #[error("Tray already exists: {0}")]
13    TrayAlreadyExists(String),
14    /// The requested die does not exist.
15    #[error("Die not found: {0}")]
16    DieNotFound(String),
17    /// A die with the requested canonical name already exists.
18    #[error("Die already exists: {0}")]
19    DieAlreadyExists(String),
20    /// The requested tray slot does not exist.
21    #[error("Slot #{slot_id} not found in tray '{tray}'")]
22    SlotNotFound {
23        /// Tray that was searched.
24        tray: String,
25        /// Slot identifier that was requested.
26        slot_id: u32,
27    },
28    /// Built-in dice cannot be modified or deleted.
29    #[error("Cannot modify builtin die: {0}")]
30    CannotModifyBuiltin(String),
31    /// The die cannot be deleted because one or more trays reference it.
32    #[error("Cannot delete die '{die}' — in use by trays: {trays:?}")]
33    CannotDeleteInUse {
34        /// Canonical die name that was requested for deletion.
35        die: String,
36        /// Trays that still reference the die.
37        trays: Vec<String>,
38    },
39    /// Storage serialization or persistence failed.
40    #[error("Storage error: {0}")]
41    StorageError(String),
42    /// A dice expression is malformed or unsupported.
43    #[error("Invalid expression: {0}")]
44    InvalidExpression(String),
45    /// A command or parser received invalid arguments.
46    #[error("Invalid arguments: {0}")]
47    InvalidArguments(String),
48    /// A tray did not contain enough matching dice to satisfy a request.
49    #[error(
50        "Tray '{tray}' does not contain enough matching dice for '{die}' (requested {requested}, available {available})"
51    )]
52    InsufficientMatchingDice {
53        /// Tray that was searched.
54        tray: String,
55        /// Die name that was requested.
56        die: String,
57        /// Number of matching dice requested.
58        requested: usize,
59        /// Number of matching dice available in the tray.
60        available: usize,
61    },
62    /// A custom die or counted expression had no faces.
63    #[error("Die must have at least 1 face")]
64    InvalidFaceCount,
65    /// Numeric dice must have at least two faces.
66    #[error("Numeric dice must have at least 2 faces: {0}")]
67    InvalidNumericDie(String),
68    /// A name was empty or contained whitespace.
69    #[error("Invalid name: must not be empty or contain whitespace")]
70    InvalidName,
71}
72
73/// Crate-wide result type.
74pub type Result<T> = std::result::Result<T, DiceError>;