#![no_std]
#[cfg(feature = "std")]
extern crate std;
mod search;
use core::cmp;
use core::convert;
use core::ops;
pub use crate::generated::Group;
#[cfg(feature = "search")]
pub use crate::search::search;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Emoji {
id: usize,
emoji: &'static str,
name: &'static str,
group: Group,
aliases: Option<&'static [&'static str]>,
}
impl Emoji {
pub const fn as_str(&self) -> &str {
self.emoji
}
pub const fn name(&self) -> &str {
self.name
}
pub const fn group(&self) -> Group {
self.group
}
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 cmp::PartialOrd for Emoji {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(cmp::Ord::cmp(self, other))
}
}
impl cmp::Ord for Emoji {
fn cmp(&self, other: &Self) -> cmp::Ordering {
cmp::Ord::cmp(&self.id, &other.id)
}
}
impl convert::AsRef<str> for Emoji {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl ops::Deref for Emoji {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
pub fn iter() -> impl Iterator<Item = &'static Emoji> {
crate::generated::EMOJIS.iter()
}
pub fn lookup(query: &str) -> Option<Emoji> {
crate::generated::EMOJIS
.iter()
.find(|&e| {
e == query
|| e.aliases
.map(|aliases| aliases.contains(&query))
.unwrap_or(false)
})
.copied()
}
impl Group {
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)
}
}
mod generated;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn emoji_ordering() {
let grinning_face = lookup("π");
let winking_face = lookup("π");
assert!(grinning_face < winking_face);
assert!(winking_face > grinning_face);
assert!(grinning_face == grinning_face);
}
}