mod data;
use data::TEMPLATES;
use rand::prelude::{SliceRandom, ThreadRng};
use std::fmt::{Display, Formatter, Result};
use super::DsMsg;
pub struct MessageDeS {
temp: &'static str,
fill: Option<&'static str>,
}
impl DsMsg for MessageDeS {
fn random(rng: &mut ThreadRng) -> Self {
let (temp, fills): &(&str, Option<&[&str]>) = TEMPLATES.choose(rng).unwrap();
let fill: Option<&str> = fills.and_then(|array| array.choose(rng).copied());
Self { temp, fill }
}
}
impl Display for MessageDeS {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match &self.fill {
Some(fill) if self.temp == "\x1F" => fill.fmt(f),
Some(fill) => match self.temp.find('\x1F') {
Some(i) => write!(
f, "{}{}{}",
&self.temp[..i], &fill, &self.temp[i + 1..],
),
None => self.temp.fmt(f),
}
None => self.temp.fmt(f),
}
}
}