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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
//! ✨ Lookup and iterate over emoji names, shortcodes, and groups.
//!
//! # Examples
//!
//! Lookup any emoji by Unicode value or GitHub shortcode.
//! ```
//! let hand = emojis::lookup("🤌").unwrap();
//! // Or
//! let hand = emojis::lookup("pinched_fingers").unwrap();
//!
//! assert_eq!(hand.as_str(), "\u{1f90c}");
//! assert_eq!(hand.name(), "pinched fingers");
//! assert_eq!(hand.group(), emojis::Group::PeopleAndBody);
//! assert_eq!(hand.shortcode().unwrap(), "pinched_fingers");
//! assert_eq!(hand.skin_tone().unwrap(), emojis::SkinTone::Default);
//! ```
//!
//! Iterate over all the emojis.
//! ```
//! let smiley = emojis::iter().next().unwrap();
//! assert_eq!(smiley, "😀");
//! ```
//!
//! Iterate over all the emojis in a group.
//! ```
//! let grapes = emojis::Group::FoodAndDrink.emojis().next().unwrap();
//! assert_eq!(grapes, "🍇");
//! ```
//!
//! Iterate over the skin tones for an emoji.
//!
//! ```
//! let raised_hands = emojis::lookup("🙌🏼").unwrap();
//! let iter = raised_hands.skin_tones().unwrap();
//! let skin_tones: Vec<_> = iter.map(emojis::Emoji::as_str).collect();
//! assert_eq!(skin_tones, ["🙌", "🙌🏻", "🙌🏼", "🙌🏽", "🙌🏾", "🙌🏿"]);
//! ```
#![no_std]
#[cfg(test)]
extern crate alloc;
mod generated;
use core::cmp;
use core::convert;
use core::fmt;
pub use crate::generated::Group;
/// Represents an emoji.
///
/// See [Unicode.org](https://unicode.org/emoji/charts/full-emoji-list.html) for
/// more information.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Emoji {
id: u16,
emoji: &'static str,
name: &'static str,
group: Group,
skin_tone: Option<(u16, SkinTone)>,
aliases: Option<&'static [&'static str]>,
variations: &'static [&'static str],
}
/// Represents the skin tone of an emoji.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SkinTone {
Default,
Light,
MediumLight,
Medium,
MediumDark,
Dark,
}
impl Emoji {
/// Returns this emoji as a string.
///
/// # Examples
///
/// ```
/// let rocket = emojis::lookup("🚀").unwrap();
/// assert_eq!(rocket.as_str(), "🚀")
/// ```
pub const fn as_str(&self) -> &str {
self.emoji
}
/// Returns the CLDR short name for this emoji.
///
/// # Examples
///
/// ```
/// let cool = emojis::lookup("😎").unwrap();
/// assert_eq!(cool.name(), "smiling face with sunglasses");
/// ```
pub const fn name(&self) -> &str {
self.name
}
/// Returns this emoji's group.
///
/// # Examples
///
/// ```
/// # use emojis::Group;
/// #
/// let flag = emojis::lookup("🇿🇦").unwrap();
/// assert_eq!(flag.group(), Group::Flags);
/// ```
pub const fn group(&self) -> Group {
self.group
}
/// Returns the skin tone of this emoji.
///
/// # Examples
///
/// ```
/// use emojis::SkinTone;
///
/// let peace = emojis::lookup("✌️").unwrap();
/// assert_eq!(peace.skin_tone(), Some(SkinTone::Default));
///
/// let peace = emojis::lookup("✌🏽").unwrap();
/// assert_eq!(peace.skin_tone(), Some(SkinTone::Medium));
/// ```
///
/// For emojis where skin tones are not applicable this will be `None`.
///
/// ```
/// let cool = emojis::lookup("😎").unwrap();
/// assert!(cool.skin_tone().is_none());
/// ```
pub fn skin_tone(&self) -> Option<SkinTone> {
self.skin_tone.map(|(_, v)| v)
}
/// Returns an iterator over the emoji and all the related skin tone emojis.
///
/// # Examples
///
/// ```
/// use emojis::Emoji;
///
/// let luck = emojis::lookup("🤞🏼").unwrap();
/// let tones: Vec<_> = luck.skin_tones().unwrap().map(Emoji::as_str).collect();
/// assert_eq!(tones, ["🤞", "🤞🏻", "🤞🏼", "🤞🏽", "🤞🏾", "🤞🏿"]);
/// ```
///
/// For emojis where skin tones are not applicable this will return `None`.
///
/// ```
/// let cool = emojis::lookup("😎").unwrap();
/// assert!(cool.skin_tones().is_none());
/// ```
pub fn skin_tones(&self) -> Option<impl Iterator<Item = &'static Self>> {
let (id, _) = self.skin_tone?;
Some(crate::generated::EMOJIS[id as usize..].iter().take(6))
}
/// Returns a version of this emoji that has the given skin tone.
///
/// # Examples
///
/// ```
/// use emojis::SkinTone;
///
/// let peace = emojis::lookup("🙌🏼")
/// .unwrap()
/// .with_skin_tone(SkinTone::MediumDark)
/// .unwrap();
/// assert_eq!(peace, emojis::lookup("🙌🏾").unwrap());
/// ```
///
/// For emojis where skin tones are not applicable this will be `None`.
///
/// ```
/// use emojis::SkinTone;
///
/// let cool = emojis::lookup("😎").unwrap();
/// assert!(cool.with_skin_tone(SkinTone::Medium).is_none());
/// ```
pub fn with_skin_tone(&self, skin_tone: SkinTone) -> Option<&'static Self> {
self.skin_tones()?
.find(|emoji| emoji.skin_tone().unwrap() == skin_tone)
}
/// Returns this emoji's GitHub shortcode.
///
/// See [gemoji] for more information.
///
/// # Examples
///
/// ```
/// let thinking = emojis::lookup("🤔").unwrap();
/// assert_eq!(thinking.shortcode().unwrap(), "thinking");
/// ```
///
/// [gemoji]: https://github.com/github/gemoji
pub fn shortcode(&self) -> Option<&str> {
self.aliases.and_then(|aliases| aliases.first().copied())
}
}
impl cmp::PartialEq<str> for Emoji {
fn eq(&self, s: &str) -> bool {
cmp::PartialEq::eq(self.as_str(), s)
}
}
impl cmp::PartialEq<&str> for Emoji {
fn eq(&self, s: &&str) -> bool {
cmp::PartialEq::eq(self.as_str(), *s)
}
}
impl convert::AsRef<str> for Emoji {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl fmt::Display for Emoji {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.as_str(), f)
}
}
/// Returns an iterator over all emojis.
///
/// - Ordered by Unicode CLDR data.
/// - Excludes skin tones.
///
/// # Examples
///
/// ```
/// let mut iter = emojis::iter();
/// assert_eq!(iter.next().unwrap(), "😀");
/// ```
pub fn iter() -> impl Iterator<Item = &'static Emoji> {
crate::generated::EMOJIS
.iter()
.filter(|emoji| emoji.skin_tone().is_none())
}
/// Lookup an emoji by Unicode value or shortcode.
///
/// # Examples
///
/// ```
/// let rocket = emojis::lookup("🚀").unwrap();
/// assert_eq!(rocket.shortcode().unwrap(), "rocket");
///
/// let rocket = emojis::lookup("rocket").unwrap();
/// assert_eq!(rocket, "🚀");
/// ```
pub fn lookup(query: &str) -> Option<&'static Emoji> {
crate::generated::EMOJIS.iter().find(|&e| {
e == query
|| e.variations.contains(&query)
|| e.aliases
.map(|aliases| aliases.contains(&query))
.unwrap_or(false)
})
}
impl Group {
/// Returns an iterator over all emojis in this group.
///
/// # Examples
///
/// ```
/// let mut iter = emojis::Group::Flags.emojis();
/// assert_eq!(iter.next().unwrap(), "🏁");
/// ```
pub fn emojis(&self) -> impl Iterator<Item = &'static Emoji> {
let group = *self;
iter()
.skip_while(move |emoji| emoji.group != group)
.take_while(move |emoji| emoji.group == group)
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::format;
use alloc::vec::Vec;
#[test]
fn emoji_partial_eq_str() {
assert_eq!(lookup("😀").unwrap(), "😀");
}
#[test]
fn emoji_display() {
let buf = format!("{}", lookup("😀").unwrap());
assert_eq!(buf.as_str(), "😀");
}
#[test]
fn lookup_variation() {
assert_eq!(lookup("☹"), lookup("☹️"));
}
#[test]
fn skin_tones() {
let skin_tones = [
SkinTone::Default,
SkinTone::Light,
SkinTone::MediumLight,
SkinTone::Medium,
SkinTone::MediumDark,
SkinTone::Dark,
];
for emoji in iter() {
match emoji.skin_tone() {
Some(_) => {
let emojis: Vec<_> = emoji.skin_tones().unwrap().collect();
assert_eq!(emojis.len(), 6);
let default = emojis[0];
for (emoji, skin_tone) in emojis.into_iter().zip(skin_tones) {
assert_eq!(emoji.skin_tone().unwrap(), skin_tone);
assert_eq!(emoji.with_skin_tone(SkinTone::Default).unwrap(), default);
}
}
None => {
assert!(emoji.skin_tones().is_none());
}
}
}
}
}