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
//! Msg1: Generate Messages from the original Dark Souls.

mod data;

use rand::prelude::{SliceRandom, ThreadRng};

use crate::util::capitalize;
use data::{FILL, TEMPLATES};


/// Message: A complete Hint Message that could be found in-game. Consists of
///     either one or two `&str`s. One is a Template, and the other, if present,
///     is a Fill phrase.
pub struct Message {
    temp: &'static str,
    fill: Option<String>,
}

impl Message {
    /// Create a new `Message`, with at least one randomized `&str`. If the
    ///     chosen string contains a placeholder character, a second `&str` will
    ///     be chosen to fill it.
    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),
        }
    }
}