1use std::collections::HashMap;
2
3const GMOCLI_DATA: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/data/gmocli.tsv"));
4
5type GmocliMap<'a> = HashMap<&'a str, Gmocli<'a>>;
6
7#[derive(Debug)]
8pub struct Gmocli<'a> {
9 pub emoji: &'a str,
10 pub name: &'a str,
11 pub category_name: &'a str,
12 pub subcategory_name: &'a str,
13 pub en_keywords: &'a str,
14 pub en_tts_description: &'a str,
15 pub gitmoji_description: &'a str,
16}
17
18impl<'a> Gmocli<'a> {
19 pub fn print(&self, with_info: bool) {
20 match with_info {
21 true => {
22 let mut gitmoji_description = String::new();
23 if self.gitmoji_description.len() > 0 {
24 gitmoji_description = format!(" # {}", self.gitmoji_description);
25 }
26 println!(
27 "{} {} | {} / {} | {}{}",
28 self.emoji,
29 self.name,
30 self.category_name,
31 self.subcategory_name,
32 self.en_keywords,
33 &gitmoji_description[..],
34 )
35 }
36 false => {
37 print!("{}", self.emoji);
38 }
39 }
40 }
41}
42
43#[derive(Debug)]
44pub struct GmocliIndex<'a> {
45 pub ordering: Vec<&'a str>,
46 pub map: GmocliMap<'a>,
47}
48
49impl<'a> GmocliIndex<'a> {
50 pub fn get_emoji_by_name(&self, name: &str) -> Option<&'a str> {
51 let mut ret = None;
52 for emoji in self.ordering.iter() {
53 let gmocli = self.map.get(emoji).unwrap();
54 if gmocli.name == name {
55 ret = Some(&emoji[..]);
56 break;
57 }
58 }
59 ret
60 }
61
62 pub fn new() -> GmocliIndex<'a> {
63 let mut ordering: Vec<&'a str> = vec![];
64 let mut map: GmocliMap = GmocliMap::new();
65 let gmocli_data = std::str::from_utf8(GMOCLI_DATA).unwrap();
66
67 for line in gmocli_data.lines() {
68 let split_tabs: Vec<&str> = line.split("\t").collect();
69 let emoji = split_tabs[0];
70 let name = split_tabs[1];
71 let category_name = split_tabs[2];
72 let subcategory_name = split_tabs[3];
73 let en_tts_description = split_tabs[4];
74 let en_keywords = split_tabs[5];
75 let gitmoji_description = split_tabs[6];
76 ordering.push(emoji);
77 map.insert(
78 emoji,
79 Gmocli {
80 emoji,
81 name,
82 category_name,
83 subcategory_name,
84 en_keywords,
85 en_tts_description,
86 gitmoji_description,
87 },
88 );
89 }
90
91 GmocliIndex { ordering, map }
92 }
93
94 pub fn print_list(&self, with_info: bool) {
95 for emoji in self.ordering.iter() {
96 self.map.get(emoji).unwrap().print(with_info);
97 }
98 }
99
100 pub fn search_gmoclis(&self, search_keys: Vec<&str>) -> Vec<&'a str> {
101 let mut matches: Vec<&'a str> = vec![];
102 if &search_keys.len() > &0 {
103 for emoji in self.ordering.iter() {
104 let gmocli = self.map.get(emoji).unwrap();
105 let search_string = format!(
106 "{} {} {} {} {} {} {}",
107 gmocli.emoji,
108 gmocli.name,
109 gmocli.category_name,
110 gmocli.subcategory_name,
111 gmocli.en_keywords,
112 gmocli.en_tts_description,
113 gmocli.gitmoji_description,
114 );
115 let mut has_match = false;
116 for search_key in &search_keys {
117 if search_string
118 .to_lowercase()
119 .contains(search_key.to_lowercase().as_str())
120 {
121 has_match = true;
122 break;
123 }
124 }
125 if has_match {
126 matches.push(emoji);
127 }
128 }
129 }
130 matches
131 }
132}