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
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
use rand;
use rand::Rng;
use anyhow;
use crate::Wishable;


/// Jirachi wrapper for adjective-noun key generator
pub struct Jirachi {
    adjectives: Vec<String>,
    nouns: Vec<String>
}

impl Jirachi {

    /// Returns a anyhow::Result containing a Jirachi instance
    ///
    /// # Example
    ///
    /// ```
    /// use jirachi::adjective_noun::Jirachi;
    ///
    /// let jirachi = Jirachi::new().unwrap();
    /// ```
    pub fn new() -> anyhow::Result<Self> {
        let raw_adjectives = include_bytes!("../../res/adjective_noun/adjectives.txt");
        let raw_nouns = include_bytes!("../../res/adjective_noun/nouns.txt");

        let mut tmp_str = String::new();

        let mut adjectives = vec![];
        let mut nouns = vec![];

        for byte in raw_adjectives.iter() {
            if byte.clone() == '\n' as u8 || byte.clone() == '\r' as u8 {
                if tmp_str.len() > 0 {
                    adjectives.push(tmp_str.clone());
                    tmp_str = String::new();
                }
            } else {
                tmp_str.push(byte.clone() as char);
            }
        }

        if tmp_str.len() > 0 {
            adjectives.push(tmp_str.clone());
        }

        tmp_str = String::new();

        for byte in raw_nouns.iter() {
            if byte.clone() == '\n' as u8 || byte.clone() == '\r' as u8 {
                if tmp_str.len() > 0 {
                    nouns.push(tmp_str.clone());
                    tmp_str = String::new();
                }
            } else {
                tmp_str.push(byte.clone() as char);
            }
        }

        if tmp_str.len() > 0 {
            nouns.push(tmp_str.clone());
        }

        if adjectives.len() == 0 || nouns.len() == 0 {
            return Err(anyhow!("Some crucial resources were not loaded."));
        }

        Ok(Self {
            adjectives,
            nouns
        })
    }

    fn get_random_noun(&self) -> String {
        let mut rng = rand::thread_rng();
        let rand_index = rng.gen_range(0, self.nouns.len() - 1);

        return self.nouns[rand_index].clone()
    }

    fn get_random_adjective(&self) -> String {
        let mut rng = rand::thread_rng();
        let rand_index = rng.gen_range(0, self.adjectives.len() - 1);

        return self.adjectives[rand_index].clone()
    }
}

impl Wishable for Jirachi {
    /// Returns a key created using random adjective-noun pairs
    ///
    /// # Example
    ///
    /// ```
    /// use jirachi::adjective_noun::Jirachi;
    /// use jirachi::Wishable;
    ///
    /// let mut jirachi = Jirachi::new().unwrap();
    /// let wish = jirachi.wish().unwrap();
    /// ```
    fn wish(&mut self) -> anyhow::Result<String> {
        let noun = self.get_random_noun().to_lowercase();
        let adjective = self.get_random_adjective().to_lowercase();

        Ok([adjective, "-".to_string(), noun].concat())
    }
}