Skip to main content

random_unicode_emoji/
lib.rs

1//! # random-unicode-emoji
2//!
3//! A simple Rust crate that returns random Unicode emojis. ⚙️
4
5use include_dir::{include_dir, Dir};
6use rand::seq::SliceRandom;
7use include_dir::File;
8use std::u32;
9
10pub fn random_emoji(count: usize, version: &str) -> Vec<String>
11{
12    static PROJECT_DIR: Dir<'_> = include_dir!("emoji");
13    let file: &File = PROJECT_DIR.get_file(version.to_owned() + "/emoji-test.txt").expect(&("Unicode version \"".to_owned() + version + "\" not supported."));
14    let lines: &str = file.contents_utf8().unwrap();
15
16    let mut lines: Vec<String> = lines.split('\n').map(|line| line.replace("#", "#")).collect();
17    lines.retain(|value: &String| !value.starts_with("#") & !value.is_empty());
18
19    let mut unicode: Vec<String> = Vec::new();
20
21    for mut line in lines
22    {
23        line = (&line[0..line.find(";").unwrap_or(line.len())]).trim().to_string();
24        let mut full_emoji: String = "".to_string();
25
26        for word in line.split_whitespace()
27        {
28            let hex: u32 = u32::from_str_radix(&word, 16).unwrap();
29            let emoji: char = char::from_u32(hex).unwrap();
30            full_emoji.push(emoji);
31        }
32        unicode.push(full_emoji);
33    }
34
35    let emojis: Vec<String> = unicode.choose_multiple(&mut rand::thread_rng(), count).cloned().collect();
36
37    return emojis;
38}