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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
//! ✨ 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.unicode_version(), emojis::UnicodeVersion::new(13, 0));
//! 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 and filter out newer emoji versions.
//! ```
//! let iter = emojis::iter().filter(|e| {
//! e.unicode_version() < emojis::UnicodeVersion::new(13, 0)
//! });
//! assert_eq!(iter.count(), 1738);
//! ```
//!
//! 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::convert;
use core::fmt;
use core::{cmp, hash};
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)]
pub struct Emoji {
emoji: &'static str,
name: &'static str,
unicode_version: UnicodeVersion,
group: Group,
skin_tone: Option<(u16, SkinTone)>,
aliases: Option<&'static [&'static str]>,
variations: &'static [&'static str],
}
/// Represents a Unicode version.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct UnicodeVersion {
major: u32,
minor: u32,
}
/// Represents the skin tone of an emoji.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SkinTone {
Default,
Light,
MediumLight,
Medium,
MediumDark,
Dark,
}
impl UnicodeVersion {
/// Construct a new version.
pub const fn new(major: u32, minor: u32) -> Self {
Self { major, minor }
}
pub const fn major(self) -> u32 {
self.major
}
pub const fn minor(self) -> u32 {
self.minor
}
}
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 the Unicode version this emoji first appeared in.
///
/// # Examples
///
/// ```
/// use emojis::UnicodeVersion;
///
/// let villain = emojis::lookup("🦹").unwrap();
/// assert_eq!(villain.unicode_version(), UnicodeVersion::new(11, 0));
/// ```
pub const fn unicode_version(&self) -> UnicodeVersion {
self.unicode_version
}
/// 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 (i, _) = self.skin_tone?;
Some(crate::generated::EMOJIS[i 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<Emoji> for Emoji {
fn eq(&self, other: &Emoji) -> bool {
self.emoji == other.emoji
}
}
impl cmp::PartialEq<str> for Emoji {
fn eq(&self, s: &str) -> bool {
self.as_str() == s
}
}
impl cmp::PartialEq<&str> for Emoji {
fn eq(&self, s: &&str) -> bool {
self.as_str() == *s
}
}
impl cmp::Eq for Emoji {}
impl hash::Hash for Emoji {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.emoji.hash(state);
}
}
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 {
self.as_str().fmt(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| matches!(emoji.skin_tone(), Some(SkinTone::Default) | 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 version_ordering() {
assert!(UnicodeVersion::new(13, 0) >= UnicodeVersion::new(12, 0));
assert!(UnicodeVersion::new(12, 1) >= UnicodeVersion::new(12, 0));
assert!(UnicodeVersion::new(12, 0) >= UnicodeVersion::new(12, 0));
assert!(UnicodeVersion::new(12, 0) < UnicodeVersion::new(12, 1));
assert!(UnicodeVersion::new(11, 0) < UnicodeVersion::new(12, 1));
assert!(UnicodeVersion::new(11, 0) < UnicodeVersion::new(12, 1));
}
#[test]
fn lookup_variation() {
assert_eq!(lookup("☹"), lookup("☹️"));
}
#[test]
fn iter_only_default_skin_tones() {
assert!(iter().all(|emoji| matches!(emoji.skin_tone(), Some(SkinTone::Default) | None)));
assert_ne!(
iter()
.filter(|emoji| matches!(emoji.skin_tone(), Some(SkinTone::Default)))
.count(),
0
);
}
#[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.iter().zip(skin_tones) {
assert_eq!(emoji.skin_tone().unwrap(), skin_tone, "{:#?}", emojis);
assert_eq!(emoji.with_skin_tone(SkinTone::Default).unwrap(), default);
}
}
None => {
assert!(emoji.skin_tones().is_none());
}
}
}
}
}