1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! `rollz` is a super-simple Rust crate that allows you to... roll dice! What else would you need from a crate?!
//!
//! This crate implements standard D&D dice set: `D4`, `D6`, `D8`, `D10`, `D12` and `D20`.
//!
//! Here's an example:
//!
//! ```rust
//! use rollz::prelude::*;
//! use rollz::D10;
//!
//! let d: D10 = roll();
//! assert!((1..=10).contains(&d.val()));
//! ```
//!
//! You can also implement your own custom die if you implement the `Rollable` trait:
//!
//! ```rust
//! use rollz::prelude::*;
//!
//! struct Fake100(u8);
//!
//! impl Rollable for Fake100 {
//!     fn roll() -> Fake100 {
//!         Fake100 { 0: 100 }
//!     }
//!     fn val(&self) -> u8 {
//!         self.0
//!     }
//! }
//!
//! let d: Fake100 = roll();
//! assert_eq!(100, d.val());
//!```

use rand::{thread_rng, Rng};

/// This is the trait that every die needs to implement to be... well... "rollable", right?
pub trait Rollable {
    /// Roll the die
    fn roll() -> Self;
    /// Get the value from the latest roll
    fn val(&self) -> u8;
}

/// A generic function to roll a given die.
/// It uses return type polymorphism so it will roll the die you actually want! Boom, like magic!
pub fn roll<T: Rollable>() -> T {
    Rollable::roll()
}

pub mod prelude;

/// A D4 die (4 faces): a roll will give you a `u8` in the `1..=4` range.
#[derive(Debug)]
pub struct D4(u8);
impl Rollable for D4 {
    fn roll() -> D4 {
        D4 {
            0: thread_rng().gen_range(1..=4),
        }
    }
    fn val(&self) -> u8 {
        self.0
    }
}

/// A D6 die (6 faces): a roll will give you a `u8` in the `1..=6` range.
#[derive(Debug)]
pub struct D6(u8);
impl Rollable for D6 {
    fn roll() -> D6 {
        D6 {
            0: thread_rng().gen_range(1..=6),
        }
    }
    fn val(&self) -> u8 {
        self.0
    }
}

/// A D8 die (8 faces): a roll will give you a `u8` in the `1..=8` range.
#[derive(Debug)]
pub struct D8(u8);
impl Rollable for D8 {
    fn roll() -> D8 {
        D8 {
            0: thread_rng().gen_range(1..=8),
        }
    }
    fn val(&self) -> u8 {
        self.0
    }
}

/// A D10 die (10 faces): a roll will give you a `u8` in the `1..=10` range.
#[derive(Debug)]
pub struct D10(u8);
impl Rollable for D10 {
    fn roll() -> D10 {
        D10 {
            0: thread_rng().gen_range(1..=10),
        }
    }
    fn val(&self) -> u8 {
        self.0
    }
}

/// A D12 die (12 faces): a roll will give you a `u8` in the `1..=12` range.
#[derive(Debug)]
pub struct D12(u8);
impl Rollable for D12 {
    fn roll() -> D12 {
        D12 {
            0: thread_rng().gen_range(1..=12),
        }
    }
    fn val(&self) -> u8 {
        self.0
    }
}

/// A D20 die (20 faces): a roll will give you a `u8` in the `1..=20` range.
#[derive(Debug)]
pub struct D20(u8);
impl Rollable for D20 {
    fn roll() -> D20 {
        D20 {
            0: thread_rng().gen_range(1..=20),
        }
    }
    fn val(&self) -> u8 {
        self.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_rolls_a_d4() {
        let d: D4 = roll();
        assert!((1..=4).contains(&d.val()));
    }

    #[test]
    fn it_rolls_a_d6() {
        let d: D6 = roll();
        assert!((1..=6).contains(&d.val()));
    }

    #[test]
    fn it_rolls_a_d8() {
        let d: D8 = roll();
        assert!((1..=8).contains(&d.val()));
    }

    #[test]
    fn it_rolls_a_d10() {
        let d: D10 = roll();
        assert!((1..=10).contains(&d.val()));
    }

    #[test]
    fn it_rolls_a_d12() {
        let d: D12 = roll();
        assert!((1..=12).contains(&d.val()));
    }

    #[test]
    fn it_rolls_a_d20() {
        let d: D20 = roll();
        assert!((1..=20).contains(&d.val()));
    }

    #[test]
    fn it_rolls_multiple_d6() {
        let d: (D6, D6, D6) = (roll(), roll(), roll());
        assert!((1..=6).contains(&d.0.val()));
        assert!((1..=6).contains(&d.1.val()));
        assert!((1..=6).contains(&d.2.val()));
    }

    #[test]
    fn it_rolls_multiple_mixed_dice() {
        let d: (D6, D8, D10, D12) = (roll(), roll(), roll(), roll());
        assert!((1..=6).contains(&d.0.val()));
        assert!((1..=8).contains(&d.1.val()));
        assert!((1..=10).contains(&d.2.val()));
        assert!((1..=12).contains(&d.3.val()));
    }
}