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
mod data;
use rand::prelude::{SliceRandom, ThreadRng};
use crate::util::capitalize;
use data::{FILL, TEMPLATES};
pub struct Message {
temp: &'static str,
fill: Option<String>,
}
impl Message {
pub fn random(rng: &mut ThreadRng) -> Self {
let temp: &str = TEMPLATES.choose(rng).unwrap();
let fill: Option<String> = match temp.find('\x1F') {
Some(i) => {
let mut s = String::from(*FILL.choose(rng).unwrap());
if i == 0 || temp.contains(':') { capitalize(&mut s); }
Some(s)
}
None => { None }
};
Self { temp, fill }
}
}
impl std::fmt::Display for Message {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.fill {
Some(fill) => {
let i: usize = self.temp.find('\x1F').unwrap_or_else(|| self.temp.len());
write!(f, "{}{}{}", &self.temp[..i], &fill, &self.temp[i + 1..])
}
_ => write!(f, "{}", &self.temp),
}
}
}