riglet/
lib.rs

1//! # Riglet 
2//!
3//! Riglet is a Rust port of Figlet
4//! # Examples
5//!
6//! ```
7//! use riglet::riglet::convert;
8//! use riglet::riglet::print_ascii;
9//!
10//! let ascii = convert(String::from("Abc Def 123 456"));
11//! print_ascii(ascii);
12//! ```
13pub mod riglet {
14
15    use std::collections::HashMap;
16    use std::collections::BTreeMap;
17
18    /// Converts text to ascii letter that you can print out. It uses the standard font from figlet
19    /// # Examples
20    ///
21    /// ```
22    /// use riglet::riglet::convert;
23    ///
24    /// convert(String::from("abcdef123456"));
25    /// ```
26    pub fn convert(text: String) -> BTreeMap<i32, String> {
27        let data_file = std::include_str!("standard.flf");
28        let parts = data_file.split("@@");
29        let mut collection: Vec<&str> = parts.collect();
30        collection.remove(0);
31
32        let char_to_ascii = setup_hash_map(collection);
33
34        return string_to_ascii(text, char_to_ascii)
35    }
36
37
38    /// Setup the hash map with the char and string
39    fn setup_hash_map(collection: Vec<&str>) -> HashMap<char, String> {
40
41        let list_of_special_chars = [
42            '!', 
43            '"', 
44            '#',
45            '$',
46            '%',
47            '&',
48            '\'',
49            '(',
50            ')',
51            '*',
52            '+',
53            ',',
54            '-',
55            '.',
56            '/',
57            '0', 
58            '1', 
59            '2',
60            '3',
61            '4',
62            '5',
63            '6',
64            '7',
65            '8',
66            '9',
67            ':',
68            ';',
69            '<',
70            '=',
71            '>',
72            '?',
73            '@',
74            'A', 
75            'B', 
76            'C',
77            'D',
78            'E',
79            'F',
80            'G',
81            'H',
82            'I',
83            'J',
84            'K',
85            'L',
86            'M',
87            'N',
88            'O',
89            'P',
90            'Q',
91            'R',
92            'S',
93            'T',
94            'U',
95            'V',
96            'W',
97            'X',
98            'Y',
99            'Z',
100            '[',
101            '\\',
102            ']',
103            '^',
104            '_',
105            '\'',
106            'a', 
107            'b', 
108            'c',
109            'd',
110            'e',
111            'f',
112            'g',
113            'h',
114            'i',
115            'j',
116            'k',
117            'l',
118            'm',
119            'n',
120            'o',
121            'p',
122            'q',
123            'r',
124            's',
125            't',
126            'u',
127            'v',
128            'w',
129            'x',
130            'y',
131            'z',
132            '{',
133            '|',
134            '}',
135            '~',
136        ];
137
138
139        let mut char_to_ascii = HashMap::new();
140
141        let mut start_index = 0;
142        for hash_char in list_of_special_chars {
143            char_to_ascii.insert(
144                hash_char, 
145                collection.get(start_index).unwrap().replace("@", "")
146            );
147
148            start_index += 1;
149        }
150
151        char_to_ascii.insert(
152            ' ', 
153            collection
154            .get(101)
155            .unwrap()
156            .replace("$@", " ")
157            .replace("160  NO-BREAK SPACE", "  ")
158        );
159
160        return char_to_ascii
161    }
162
163    /// Your inputed string to ascii letters
164    fn string_to_ascii(text: String, char_to_ascii: HashMap<char, String>) -> BTreeMap<i32, String> {
165        let mut print_ascii = Vec::new();
166        
167        for character in text.chars() {
168            print_ascii.push(char_to_ascii.get(&character).unwrap());
169        }
170
171        let mut map_ascii = BTreeMap::from([
172            (0, String::from("")),
173            (1, String::from("")),
174            (2, String::from("")),
175            (3, String::from("")),
176            (4, String::from("")),
177            (5, String::from("")),
178            (6, String::from("")),
179        ]);
180
181        for string_ascii in &print_ascii {
182            let mut line_number = 0;
183
184            for string in string_ascii.split("\n") {
185                if line_number <= 6 {
186                   map_ascii.get_mut(&line_number).unwrap().push_str(string);
187                }
188
189                line_number += 1;
190            }
191        }
192
193        map_ascii
194    }
195
196
197    /// Prints the Ascii
198    /// # Examples
199    /// ```
200    /// use riglet::riglet::convert;
201    /// use riglet::riglet::print_ascii;
202    ///
203    /// let ascii = convert(String::from("Abc Def 123 456"));
204    /// ```
205    pub fn print_ascii(to_print_ascii: BTreeMap<i32, String>) -> () {
206        for (_c, string) in to_print_ascii {
207            println!("{}", string)
208        }
209    }
210
211    #[cfg(test)]
212    mod tests {
213        use super::*;
214
215        #[test]
216        fn test_convert() {
217            let args = String::from("T");
218            let anwser = convert(args);
219            
220            assert_eq!(Some(&String::from("")), anwser.get(&0));
221            assert_eq!(Some(&String::from("  _____ ")), anwser.get(&1));
222            assert_eq!(Some(&String::from(" |_   _|")), anwser.get(&2));
223            assert_eq!(Some(&String::from("   | |  ")), anwser.get(&3));
224            assert_eq!(Some(&String::from("   | |  ")), anwser.get(&4));
225            assert_eq!(Some(&String::from("   |_|  ")), anwser.get(&5));
226        }
227
228        #[test]
229        fn test_convert_space() {
230            let args = String::from(" ");
231            let anwser = convert(args);
232            
233            assert_eq!(Some(&String::from("")), anwser.get(&0));
234            assert_eq!(Some(&String::from("  ")), anwser.get(&1));
235            assert_eq!(Some(&String::from("  ")), anwser.get(&2));
236            assert_eq!(Some(&String::from("  ")), anwser.get(&3));
237            assert_eq!(Some(&String::from("  ")), anwser.get(&4));
238            assert_eq!(Some(&String::from("  ")), anwser.get(&5));
239        }
240
241        #[test]
242        fn test_string_to_ascii() {
243            let args = String::from("T");
244
245            let anwser = string_to_ascii(args, HashMap::from([('T', String::from("  _____ 
246 |_   _|
247   | |  
248   | |  
249   |_|  
250          "))])
251                                         );
252            
253            assert_eq!(Some(&String::from("  _____ ")), anwser.get(&0));
254            assert_eq!(Some(&String::from(" |_   _|")), anwser.get(&1));
255            assert_eq!(Some(&String::from("   | |  ")), anwser.get(&2));
256            assert_eq!(Some(&String::from("   | |  ")), anwser.get(&3));
257            assert_eq!(Some(&String::from("   |_|  ")), anwser.get(&4));
258        }
259    }
260}