pub struct DiceEngine { /* private fields */ }Expand description
In-memory dice and tray engine.
A new engine starts with the built-in dice and no trays. Custom dice and trays are held in memory; callers that need persistence should serialize the data structures exposed by this crate.
Implementations§
Source§impl DiceEngine
impl DiceEngine
Sourcepub fn set_custom_dice(&mut self, custom: Vec<Die>)
pub fn set_custom_dice(&mut self, custom: Vec<Die>)
Replaces all custom dice while preserving the built-in dice.
Each supplied die is normalized to DieKind::Custom and receives
CUSTOM_PREFIX if it does not already have it.
Sourcepub fn custom_dice(&self) -> Vec<&Die>
pub fn custom_dice(&self) -> Vec<&Die>
Returns all custom dice.
Sourcepub fn list_dice(&self) -> &[Die]
pub fn list_dice(&self) -> &[Die]
Returns all dice known to the engine.
Built-in dice are listed before custom dice.
Sourcepub fn list_trays(&self) -> &[Tray]
pub fn list_trays(&self) -> &[Tray]
Returns all trays known to the engine.
Sourcepub fn resolve_die_name(&self, name: &str) -> Option<String>
pub fn resolve_die_name(&self, name: &str) -> Option<String>
Resolves a user-provided die name to its canonical engine name.
The lookup first tries an exact match, then a case-insensitive match, and
finally the custom-die form with CUSTOM_PREFIX.
Sourcepub fn create_die(&mut self, name: &str, faces: Vec<FaceValue>) -> Result<&Die>
pub fn create_die(&mut self, name: &str, faces: Vec<FaceValue>) -> Result<&Die>
Creates a custom die and returns the stored die.
The supplied name must be non-empty and contain no whitespace. Names are
normalized with CUSTOM_PREFIX.
§Errors
Returns DiceError::InvalidName for invalid names,
DiceError::InvalidFaceCount for empty faces, and
DiceError::DieAlreadyExists when the normalized name is already in
use.
Sourcepub fn modify_die(&mut self, name: &str, faces: Vec<FaceValue>) -> Result<&Die>
pub fn modify_die(&mut self, name: &str, faces: Vec<FaceValue>) -> Result<&Die>
Replaces the faces of a custom die and returns the stored die.
§Errors
Returns DiceError::InvalidFaceCount for empty faces,
DiceError::CannotModifyBuiltin for built-in dice,
DiceError::InvalidName for invalid custom names, and
DiceError::DieNotFound when the die does not exist.
Sourcepub fn delete_die(&mut self, name: &str) -> Result<()>
pub fn delete_die(&mut self, name: &str) -> Result<()>
Deletes a custom die.
§Errors
Returns DiceError::CannotModifyBuiltin for built-in dice,
DiceError::InvalidName for invalid custom names,
DiceError::DieNotFound when the die does not exist, and
DiceError::CannotDeleteInUse when any tray still references the die.
Sourcepub fn create_tray(&mut self, name: &str) -> Result<&Tray>
pub fn create_tray(&mut self, name: &str) -> Result<&Tray>
Creates an empty tray and returns it.
§Errors
Returns DiceError::InvalidName for empty names or names containing
whitespace, and DiceError::TrayAlreadyExists when the name is already
in use.
Sourcepub fn delete_tray(&mut self, name: &str) -> Result<()>
pub fn delete_tray(&mut self, name: &str) -> Result<()>
Sourcepub fn rename_tray(&mut self, old_name: &str, new_name: &str) -> Result<&Tray>
pub fn rename_tray(&mut self, old_name: &str, new_name: &str) -> Result<&Tray>
Renames a tray and returns it.
§Errors
Returns DiceError::TrayNotFound when old_name does not exist,
DiceError::InvalidName when new_name is invalid, and
DiceError::TrayAlreadyExists when new_name belongs to another tray.
Sourcepub fn add_die_to_tray(
&mut self,
die_name: &str,
tray_name: &str,
) -> Result<u32>
pub fn add_die_to_tray( &mut self, die_name: &str, tray_name: &str, ) -> Result<u32>
Adds a die to a tray and returns the assigned slot identifier.
The die name is resolved through Self::resolve_die_name. Slot
identifiers are assigned by the tray and are not reused after removal.
§Errors
Returns DiceError::DieNotFound when the die cannot be resolved and
DiceError::TrayNotFound when the tray does not exist.
Sourcepub fn remove_slot(&mut self, tray_name: &str, slot_id: u32) -> Result<()>
pub fn remove_slot(&mut self, tray_name: &str, slot_id: u32) -> Result<()>
Removes a slot from a tray.
§Errors
Returns DiceError::TrayNotFound when the tray does not exist and
DiceError::SlotNotFound when the slot does not exist in that tray.
Sourcepub fn lock_slot(&mut self, tray_name: &str, slot_id: u32) -> Result<()>
pub fn lock_slot(&mut self, tray_name: &str, slot_id: u32) -> Result<()>
Locks a tray slot so future tray rolls preserve its current value.
§Errors
Returns DiceError::TrayNotFound when the tray does not exist and
DiceError::SlotNotFound when the slot does not exist in that tray.
Sourcepub fn unlock_slot(&mut self, tray_name: &str, slot_id: u32) -> Result<()>
pub fn unlock_slot(&mut self, tray_name: &str, slot_id: u32) -> Result<()>
Unlocks a tray slot so future tray rolls can update its current value.
§Errors
Returns DiceError::TrayNotFound when the tray does not exist and
DiceError::SlotNotFound when the slot does not exist in that tray.
Sourcepub fn roll_die(&self, die_name: &str) -> Result<DieRoll>
pub fn roll_die(&self, die_name: &str) -> Result<DieRoll>
Rolls one die.
Numeric names such as d13 are supported even when the die is not stored
in the engine.
§Errors
Returns DiceError::InvalidNumericDie for numeric dice with fewer than
two faces and DiceError::DieNotFound for unresolved named dice.
Sourcepub fn roll_dice(&self, die_names: &[String]) -> Result<RollBatchResult>
pub fn roll_dice(&self, die_names: &[String]) -> Result<RollBatchResult>
Rolls multiple dice in order.
The returned roll identifiers start at 1 and follow the input order.
§Errors
Returns the first error encountered while resolving or rolling a die.
Sourcepub fn analyze_roll(
&self,
die_names: &[String],
modifiers: &[i64],
) -> Result<RollAnalysis>
pub fn analyze_roll( &self, die_names: &[String], modifiers: &[i64], ) -> Result<RollAnalysis>
Computes expected value and inclusive point range without rolling.
Integer faces contribute their value. Text faces contribute zero points. Modifiers are added to both expected value and range.
§Errors
Returns the first error encountered while resolving or analyzing a die.
Sourcepub fn roll_tray(&mut self, tray_name: &str) -> Result<&Tray>
pub fn roll_tray(&mut self, tray_name: &str) -> Result<&Tray>
Rolls every unlocked slot in a tray and returns the updated tray.
Locked slots keep their existing current_value.
§Errors
Returns DiceError::TrayNotFound when the tray does not exist and
DiceError::DieNotFound when a tray slot references a missing die.
Sourcepub fn show_tray(&self, tray_name: &str) -> Result<TrayResult>
pub fn show_tray(&self, tray_name: &str) -> Result<TrayResult>
Returns a snapshot of a tray and its current slot values.
§Errors
Returns DiceError::TrayNotFound when the tray does not exist.
Trait Implementations§
Source§impl Clone for DiceEngine
impl Clone for DiceEngine
Source§fn clone(&self) -> DiceEngine
fn clone(&self) -> DiceEngine
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more