pub struct Roll {
pub number_of_sides: u16,
pub number_of_dice: u16,
pub modifier: i32,
}
Expand description
Holds information about a die roll.
Fields§
§number_of_sides: u16
The type of die.
number_of_dice: u16
How many dice are to be rolled.
modifier: i32
A modifier to be added to the result of the die rolls.
Implementations§
Source§impl Roll
impl Roll
Sourcepub fn new(number_of_sides: u16, number_of_dice: u16, modifier: i32) -> Self
pub fn new(number_of_sides: u16, number_of_dice: u16, modifier: i32) -> Self
A convenience function that allows you to manually create a new Roll.
Sourcepub fn parse_roll(input: &str) -> Result<Roll, RollError>
pub fn parse_roll(input: &str) -> Result<Roll, RollError>
Tries to parse input as roll notation (e.g. 4d20 + 5
).
§Examples
use die_parser::Roll;
use die_parser::RollError;
let roll = Roll::parse_roll("3d10 - 5");
assert_eq!(roll, Ok(Roll::new(10, 3, -5)));
let invalid_roll = Roll::parse_roll("101d20");
assert_eq!(invalid_roll, Err(RollError::DiceExceedLimit));
Valid die types are: d2, d4, d6, d8, d10, d12, d20, d100 ↩
If you wish to allow more (or only allow less) than 100 dice per roll, use
Roll::parse_roll_with_limit()
instead. ↩
Sourcepub fn parse_roll_with_limit(
input: &str,
max_dice: u16,
) -> Result<Roll, RollError>
pub fn parse_roll_with_limit( input: &str, max_dice: u16, ) -> Result<Roll, RollError>
Tries to parse input as roll notation (e.g. 4d20 + 5
).
- Whitespaces are ignored.
- Checks for validity of roll.1
- Enforces a custom limit of how many dice are allowed per roll
(0 = no limit)
.
- Enforces a custom limit of how many dice are allowed per roll
§Examples
use die_parser::Roll;
use die_parser::RollError;
let roll = Roll::parse_roll_with_limit("3d10 - 5", 1000);
assert_eq!(roll, Ok(Roll::new(10, 3, -5)));
let invalid_roll = Roll::parse_roll_with_limit("15d20", 10);
assert_eq!(invalid_roll, Err(RollError::DiceExceedLimit));
Valid die types are: d2, d4, d6, d8, d10, d12, d20, d100 ↩
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Roll
impl RefUnwindSafe for Roll
impl Send for Roll
impl Sync for Roll
impl Unpin for Roll
impl UnwindSafe for Roll
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more