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
//! # random-unicode-emoji
//!
//! A simple Rust crate that returns random Unicode emojis. ❤️

use include_dir::{include_dir, Dir};
use rand::seq::SliceRandom;
use include_dir::File;
use std::u32;

pub fn random_emoji(count: usize, version: &str) -> Vec<String> {

    static PROJECT_DIR: Dir<'_> = include_dir!("emoji");
    let file: &File = PROJECT_DIR.get_file(version.to_owned() + "/emoji-test.txt").expect(&("Unicode version \"".to_owned() + version + "\" not supported."));
    let lines: &str = file.contents_utf8().unwrap();

    let mut lines: Vec<String> = lines.split('\n').map(|line| line.replace("#", "#")).collect();
    lines.retain(|value: &String| !value.starts_with("#") & !value.is_empty());

    let mut unicode: Vec<String> = Vec::new();

    for mut line in lines{
        line = (&line[0..line.find(";").unwrap_or(line.len())]).trim().to_string();
        let mut full_emoji: String = "".to_string();

        for word in line.split_whitespace(){
            let hex: u32 = u32::from_str_radix(&word, 16).unwrap();
            let emoji: char = char::from_u32(hex).unwrap();
            full_emoji.push(emoji);
        }
        unicode.push(full_emoji);
    }

    let sample = unicode.choose_multiple(&mut rand::thread_rng(), count).cloned().collect();

    return sample;
}