-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdefault.rs
More file actions
50 lines (44 loc) · 1.14 KB
/
default.rs
File metadata and controls
50 lines (44 loc) · 1.14 KB
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
use crate::generator::generate;
use crate::Syllable;
use rand::distr::{Distribution, StandardUniform};
use std::fmt;
use std::ops;
/// Pseudo-word of moderate length ( 6 to 15 chars )
///
/// Wrapper type around [`String`] which implements [`Distribution`](rand::distributions::Distribution)
#[derive(Debug)]
pub struct Gab(pub String);
impl Distribution<Gab> for StandardUniform {
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> Gab {
Gab(generate(rng, Syllable::Alphabet, Syllable::Consonant, None))
}
}
impl ops::Deref for Gab {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl ops::DerefMut for Gab {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl fmt::Display for Gab {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[cfg(test)]
mod tests {
#[test]
pub fn default() {
use crate::Gab;
use rand::rng;
use rand::RngExt;
let mut rng = rng();
let gib: Gab = rng.random();
assert!(!gib.is_empty());
println!("gab {}", gib);
}
}