owoify/
lib.rs

1use rand::prelude::*;
2use regex::Regex;
3use std::str::FromStr;
4
5#[cfg(test)]
6mod tests {
7    use super::*;
8    #[test]
9    fn owo() {
10        let text = String::from("malfunction me mom.. t-till i break~~");
11        let owoified = text.owoify();
12        println!("\t{}", owoified);
13    }
14    #[test]
15    fn all_match_owo() {
16        let text = String::from("r l R L th na Na NA ove !!");
17        println!("\t{}", text.owoify());
18    }
19}
20
21pub trait OwOifiable {
22    /// The owoification method
23    fn owoify(&self) -> Self;
24}
25
26impl OwOifiable for String {
27    /// Owoifies a String
28    ///
29    /// # Examples
30    ///
31    /// ```
32    /// use owoify::OwOifiable;
33    /// let owoified = String::from("Example text").owoify();
34    /// ```
35    fn owoify(&self) -> Self {
36        let mut rng = rand::thread_rng();
37        let faces = ["(・`ω´・)", "OwO", "owo", "oωo", "òωó", "°ω°", "UwU", ">w<", "^w^"];
38        let face = &format!(" {} ", faces[rng.gen_range(0, faces.len())]).to_owned();
39        let pats: Vec<(&str, &str)> = vec![
40            ("(?:r|l)", "w"),
41            ("(?:R|L)", "W"),
42            ("n([aeiou])", "ny$1"),
43            ("N([aeiou])", "Ny$1"),
44            ("N([AEIOU])", "NY$1"),
45            ("th", "d"),
46            ("ove", "uv"),
47            ("!+", face),
48        ];
49
50        let mut owoified = String::from_str(&self).unwrap();
51
52        for &(f, t) in &pats {
53            let re = Regex::new(f).unwrap();
54            owoified = re.replace_all(&owoified, t).to_string();
55        }
56
57        owoified
58    }
59}