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
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
mod picker_struct;
extern crate regex;
use picker_struct::{EmojiData, EMOJI_DATA};
use regex::{escape, Regex};

pub use picker_struct::*;

/// the language for the emoji searcher
pub enum Language {
    /// english
    En,
    /// spanish
    Es,
    /// german
    Ger,
    /// french
    Fr,
    /// chinese
    Zh,
    /// japanese
    Ja,
}

/// the main interface with the static emoji
/// data.
pub struct EmojiUtil {
    pub current_emojis: Option<Vec<&'static EmojiData>>,
    pub search_string: Option<String>,
    pub language: Language,
}

impl EmojiUtil {
    pub fn new(lang: Language) -> EmojiUtil {
        EmojiUtil {
            current_emojis: Some(EMOJI_DATA.iter().collect()),
            search_string: None,
            language: lang,
        }
    }

    pub fn search(&mut self, search_string: String) {
        self.current_emojis = emoji_search(search_string, &self.language)
    }

    pub fn clear_search(&mut self) {
        self.search_string = None;
        self.current_emojis = Some(EMOJI_DATA.iter().collect())
    }

    pub fn set_language(&mut self, language: Language) {
        self.language = language;
    }
}

// Search The emoji structure for emojis with
// tags matching the search string
fn emoji_search(search_string: String, lang: &Language) -> Option<Vec<&'static EmojiData>> {
    if let Ok(re) = Regex::new(&escape(&search_string)) {
        Some(
            EMOJI_DATA
                .iter()
                .enumerate()
                .filter(|(_, datum)| datum.tags.iter().any(|tag| re.is_match(tag)))
                .map(|(i, _)| translate(i, lang))
                .collect(),
        )
    } else {
        None
    }
}

// runs the given text through
fn translate(index: usize, lang: &Language) -> &'static EmojiData {
    match lang {
        _ => &EMOJI_DATA[index],
        // TODO: run other languages through a lookup
        // table which substitutes their text for the correct
        // tag in another language. table should be lazy static
        // to avoid too much allocation.
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fmt::{Debug, Formatter, Result};

    impl PartialEq for EmojiData {
        fn eq(&self, other: &Self) -> bool {
            self.emoji == other.emoji
        }
    }
    impl Debug for EmojiData {
        fn fmt(&self, f: &mut Formatter<'_>) -> Result {
            write!(f, "({})", self.emoji)
        }
    }

    #[test]
    fn emoji_search_test() {
        let mut eu = EmojiUtil::new(Language::En);
        eu.search(String::from("thumb"));
        assert_eq!(
            Some(vec![
                &EmojiData {
                    emoji: "👍",
                    tags: &[],
                    skintone_modifier: false,
                },
                &EmojiData {
                    emoji: "👎",
                    tags: &[],
                    skintone_modifier: false,
                }
            ]),
            eu.current_emojis
        );
    }

    #[test]
    fn clear_search() {
        let mut eu = EmojiUtil::new(Language::En);
        let orig_clone = eu.current_emojis.clone();
        eu.search(String::from("thumb"));
        assert_eq!(
            Some(vec![
                &EmojiData {
                    emoji: "👍",
                    tags: &[],
                    skintone_modifier: false,
                },
                &EmojiData {
                    emoji: "👎",
                    tags: &[],
                    skintone_modifier: false,
                }
            ]),
            eu.current_emojis
        );
        eu.clear_search();
        assert_eq!(orig_clone, eu.current_emojis);
    }
}