-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgabble.rs
More file actions
79 lines (72 loc) · 1.87 KB
/
gabble.rs
File metadata and controls
79 lines (72 loc) · 1.87 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
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
use crate::generator::generate;
use crate::Syllable::{self, *};
/// Generator type used for generating custom variant of pseudo-word
/// ## Example
/// ```
/// use gabble::Gabble;
/// use gabble::Syllable::{Alphabet, Consonant};
/// use rand::rng;
/// let mut rng = rng();
/// //Generator configured to generate words
/// //that starts with consonant syllable and ends with a number
/// let gabble = Gabble::new()
/// .with_length(10)
/// .starts_with(Alphabet)
/// .ends_with(Consonant);
/// println!("customized answer to life is {}", gabble.generate(&mut rng));
/// ```
///
pub struct Gabble {
pub start: Syllable,
pub end: Syllable,
pub length: Option<usize>,
}
impl Gabble {
pub fn new() -> Self {
Self {
start: Alphabet,
end: Consonant,
length: None,
}
}
pub fn with_length(mut self, n: usize) -> Self {
if n < 3 {
panic!("provide appropriate length");
}
self.length = Some(n);
self
}
pub fn starts_with(mut self, syllable: Syllable) -> Self {
self.start = syllable;
self
}
pub fn ends_with(mut self, syllable: Syllable) -> Self {
self.end = syllable;
self
}
pub fn generate<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> String {
generate(rng, self.start.clone(), self.end.clone(), self.length)
}
}
impl Default for Gabble {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
#[test]
pub fn gabble() {
use crate::Gabble;
use crate::Syllable::*;
use rand::rng;
let mut rng = rng();
let gib = Gabble::new()
.with_length(6)
.starts_with(Alphabet)
.ends_with(Consonant);
let word = gib.generate(&mut rng);
assert!(!word.is_empty());
println!("gabble {}", word);
}
}