gabble/
default.rs

1use crate::generator::generate;
2use crate::Syllable;
3use rand::distr::{Distribution, StandardUniform};
4use std::fmt;
5use std::ops;
6
7/// Pseudo-word of moderate length ( 6 to 15 chars )
8///
9/// Wrapper type around [`String`] which implements [`Distribution`](rand::distributions::Distribution)
10#[derive(Debug)]
11pub struct Gab(pub String);
12
13impl Distribution<Gab> for StandardUniform {
14    fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> Gab {
15        Gab(generate(rng, Syllable::Alphabet, Syllable::Consonant, None))
16    }
17}
18
19impl ops::Deref for Gab {
20    type Target = String;
21    fn deref(&self) -> &Self::Target {
22        &self.0
23    }
24}
25
26impl ops::DerefMut for Gab {
27    fn deref_mut(&mut self) -> &mut Self::Target {
28        &mut self.0
29    }
30}
31
32impl fmt::Display for Gab {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        write!(f, "{}", self.0)
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    #[test]
41    pub fn default() {
42        use crate::Gab;
43        use rand::rng;
44        use rand::Rng;
45        let mut rng = rng();
46        let gib: Gab = rng.random();
47        assert!(!gib.is_empty());
48        println!("gab {}", gib);
49    }
50}