warlocks_cauldron/providers/
text.rs

1use super::dependencies::*;
2
3
4/// A struct for generating text data
5pub struct Text<'a>(pub &'a Locale);
6
7impl<'a> Text<'a> {
8    /// Private. Return global parsed data from own locale
9    fn data(&self) -> &ParsedData { self.0.get_data() }
10    
11
12    /// Get an alphabet for current locale
13    /// 
14    /// return example: Alphabet
15    ///
16    /// # Arguments
17    /// * `lower_case` - Return alphabet in lower case
18    pub fn alphabet(&self, lower_case: bool) -> &Vec<String> {
19        match lower_case {
20            true => &self.data().text.alphabet.lowercase,
21            false => &self.data().text.alphabet.uppercase,
22        }
23    }
24
25    /// Get an alphabet for current locale
26    /// 
27    /// return example: critical
28    pub fn level(&self) -> &str {
29        get_random_element(self.data().text.level.iter())
30    }
31
32    /// Generate the text
33    /// 
34    /// return example: 
35    /// 
36    /// # Arguments
37    /// * `quantity` - Quantity of sentences.
38    pub fn text(&self, quantity: usize) -> String {
39        get_random_elements(self.data().text.text.iter(), quantity)
40            .iter().join(" ")
41    }
42
43    /// Get a random sentence from text
44    /// 
45    /// return example: Sentence
46    pub fn sentence(&self) -> String {
47        self.text(1)
48    }
49
50    /// Get a random title
51    /// 
52    /// return example: The title
53    pub fn title(&self) -> String {
54        self.text(1)
55    }
56    
57    /// Generate a vec of random words
58    /// 
59    /// return example: vec!\[science, network, god, octopus, love\]
60    /// 
61    /// # Arguments
62    /// * `quantity` - Quantity of words
63    pub fn words(&self, quantity: usize) -> Vec<&String> {
64        get_random_elements(self.data().text.words.normal.iter(), quantity)
65    }
66
67    /// Get a random word
68    /// 
69    /// return example: Science
70    pub fn word(&self) -> &str {
71        self.words(1).first().unwrap()
72    }
73
74    /// Get a random swear word
75    /// 
76    /// return example: Damn
77    pub fn swear_word(&self) -> &str {
78        get_random_element(self.data().text.words.bad.iter())
79    }
80
81    /// Get a random quote from movie
82    /// 
83    /// return example:  Bond... James Bond
84    pub fn quote(&self) -> &str {
85        get_random_element(self.data().text.quotes.iter())
86    }
87
88    /// Get a random quote from movie
89    /// 
90    /// return example: No
91    pub fn answer(&self) -> &str {
92        get_random_element(self.data().text.answers.iter())
93    }
94
95    /// Get a random name of color
96    /// 
97    /// return example: Red
98    pub fn color(&self) -> &str {
99        get_random_element(self.data().text.color.iter())
100    }
101
102    /// Generate a random hex color
103    /// 
104    /// return example: #d8346b
105    /// 
106    /// # Arguments
107    /// * `safe` - Get safe Flat UI hex color
108    pub fn hex_color(safe: bool) -> String {
109        match safe {
110            true => get_random_element(SAFE_COLORS.iter()).to_string(),
111            false => format!("#{:#06x}", randint(0x000000, 0xFFFFFF)),
112        }
113    }
114
115    /// Generate a random rgb color tuple
116    /// 
117    /// return example: (252, 85, 32)
118    ///
119    /// # Arguments
120    /// * `safe` - Get safe RGB tuple
121    pub fn rgb_color(safe: bool) -> (u8, u8, u8) {
122        let color = Self::hex_color(safe).replacen("#", "", 1);
123        (
124            u8::from_str_radix(&color[0..2], 16).expect("Cant convert RGB hex to u8 tuple!"),
125            u8::from_str_radix(&color[2..4], 16).expect("Cant convert RGB hex to u8 tuple!"),
126            u8::from_str_radix(&color[4..6], 16).expect("Cant convert RGB hex to u8 tuple!"),
127        )
128    }
129}