1use std::fmt;
2use regex::{
3 Regex,
4 Captures,
5};
6
7#[cfg(test)]
8mod tests;
9
10pub struct EmojiFormatter<'a>(pub &'a str);
15
16impl<'a> std::fmt::Display for EmojiFormatter<'a> {
17 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
18 let re = Regex::new(r":([a-zA-Z0-9_\+\-]+):").unwrap();
19
20 let result = re.replace_all(self.0, EmojiReplacer);
21
22 write!(f, "{}", result)
23 }
24}
25
26struct EmojiReplacer;
27
28impl regex::Replacer for EmojiReplacer {
29 fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
30 let sym = caps.get(1).unwrap().into();
31 dst.push_str(emojis::get_by_shortcode(sym).map_or_else(|| caps.get(0).unwrap().into(), emojis::Emoji::as_str));
32 }
33}