Struct Roll

Source
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

Source

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.

Source

pub fn parse_roll(input: &str) -> 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 limit of 100 dice per roll.2
§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));

  1. Valid die types are: d2, d4, d6, d8, d10, d12, d20, d100 

  2. If you wish to allow more (or only allow less) than 100 dice per roll, use Roll::parse_roll_with_limit() instead. 

Source

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).
§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));

  1. Valid die types are: d2, d4, d6, d8, d10, d12, d20, d100 

Trait Implementations§

Source§

impl Debug for Roll

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Roll

Source§

fn eq(&self, other: &Roll) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Roll

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.