pub struct Emoji { /* private fields */ }
Expand description
Represents an emoji.
See Unicode.org for more information.
Implementations§
source§impl Emoji
impl Emoji
sourcepub const fn as_str(&self) -> &str
pub const fn as_str(&self) -> &str
Returns this emoji as a string.
§Examples
let rocket = emojis::get("🚀").unwrap();
assert_eq!(rocket.as_str(), "🚀")
sourcepub const fn as_bytes(&self) -> &[u8]
pub const fn as_bytes(&self) -> &[u8]
Returns this emoji as slice of UTF-8 encoded bytes.
§Examples
let rocket = emojis::get("🚀").unwrap();
assert_eq!(rocket.as_bytes(), &[0xf0, 0x9f, 0x9a, 0x80]);
Examples found in repository?
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
fn replace(mut s: &str, mut o: impl Write) -> io::Result<()> {
// The meaning of the index values is as follows.
//
// : r o c k e t :
// ^ ^ ^ ^
// i m n j
//
// i..j gives ":rocket:"
// m..n gives "rocket"
while let Some((i, m, n, j)) = s
.find(':')
.map(|i| (i, i + 1))
.and_then(|(i, m)| s[m..].find(':').map(|x| (i, m, m + x, m + x + 1)))
{
match emojis::get_by_shortcode(&s[m..n]) {
Some(emoji) => {
// Output everything preceding, except the first colon.
o.write_all(s[..i].as_bytes())?;
// Output the emoji.
o.write_all(emoji.as_bytes())?;
// Update the string to past the last colon.
s = &s[j..];
}
None => {
// Output everything preceding but not including the colon.
o.write_all(s[..n].as_bytes())?;
// Update the string to start with the last colon.
s = &s[n..];
}
}
}
o.write_all(s.as_bytes())
}
sourcepub const fn name(&self) -> &str
pub const fn name(&self) -> &str
Returns the CLDR name for this emoji.
§Examples
let cool = emojis::get("😎").unwrap();
assert_eq!(cool.name(), "smiling face with sunglasses");
sourcepub const fn unicode_version(&self) -> UnicodeVersion
pub const fn unicode_version(&self) -> UnicodeVersion
Returns the Unicode version this emoji first appeared in.
§Examples
use emojis::UnicodeVersion;
let villain = emojis::get("🦹").unwrap();
assert_eq!(villain.unicode_version(), UnicodeVersion::new(11, 0));
sourcepub const fn group(&self) -> Group
pub const fn group(&self) -> Group
Returns the group this emoji belongs to.
§Examples
use emojis::Group;
let flag = emojis::get("🇿🇦").unwrap();
assert_eq!(flag.group(), Group::Flags);
sourcepub fn skin_tone(&self) -> Option<SkinTone>
pub fn skin_tone(&self) -> Option<SkinTone>
Returns the skin tone of this emoji.
§Examples
use emojis::SkinTone;
let peace = emojis::get("✌️").unwrap();
assert_eq!(peace.skin_tone(), Some(SkinTone::Default));
let peace = emojis::get("✌🏽").unwrap();
assert_eq!(peace.skin_tone(), Some(SkinTone::Medium));
For emojis where skin tones are not applicable this will be None
.
let cool = emojis::get("😎").unwrap();
assert!(cool.skin_tone().is_none());
sourcepub fn skin_tones(&self) -> Option<impl Iterator<Item = &Self> + Clone>
pub fn skin_tones(&self) -> Option<impl Iterator<Item = &Self> + Clone>
Returns an iterator over the emoji and all the related skin tone emojis.
§Examples
use emojis::Emoji;
let luck = emojis::get("🤞🏼").unwrap();
let skin_tones: Vec<_> = luck.skin_tones().unwrap().map(Emoji::as_str).collect();
assert_eq!(skin_tones, ["🤞", "🤞🏻", "🤞🏼", "🤞🏽", "🤞🏾", "🤞🏿"]);
Some emojis have 26 skin tones!
use emojis::SkinTone;
let couple = emojis::get("👩🏿❤️👨🏼").unwrap();
let skin_tones = couple.skin_tones().unwrap().count();
assert_eq!(skin_tones, 26);
For emojis where skin tones are not applicable this will return None
.
let cool = emojis::get("😎").unwrap();
assert!(cool.skin_tones().is_none());
sourcepub fn with_skin_tone(&self, skin_tone: SkinTone) -> Option<&Self>
pub fn with_skin_tone(&self, skin_tone: SkinTone) -> Option<&Self>
Returns a version of this emoji that has the given skin tone.
§Examples
use emojis::SkinTone;
let raised_hands = emojis::get("🙌🏼")
.unwrap()
.with_skin_tone(SkinTone::MediumDark)
.unwrap();
assert_eq!(raised_hands, emojis::get("🙌🏾").unwrap());
use emojis::SkinTone;
let couple = emojis::get("👩❤️👨")
.unwrap()
.with_skin_tone(SkinTone::DarkAndMediumLight)
.unwrap();
assert_eq!(couple, emojis::get("👩🏿❤️👨🏼").unwrap());
For emojis where the skin tone is not applicable this will return
None
.
use emojis::SkinTone;
let cool = emojis::get("😎").unwrap();
assert!(cool.with_skin_tone(SkinTone::Medium).is_none());
sourcepub fn shortcode(&self) -> Option<&str>
pub fn shortcode(&self) -> Option<&str>
Returns the first GitHub shortcode for this emoji.
Most emojis only have zero or one shortcode but for a few there are
multiple. Use the shortcodes()
method to return
all the shortcodes. See gemoji for more information.
For emojis that have zero shortcodes this will return None
.
§Examples
let thinking = emojis::get("🤔").unwrap();
assert_eq!(thinking.shortcode().unwrap(), "thinking");
sourcepub fn shortcodes(&self) -> impl Iterator<Item = &str> + Clone
pub fn shortcodes(&self) -> impl Iterator<Item = &str> + Clone
Returns an iterator over the GitHub shortcodes for this emoji.
Most emojis only have zero or one shortcode but for a few there are
multiple. Use the shortcode()
method to return the
first shortcode. See gemoji for more information.
For emojis that have zero shortcodes this will return an empty iterator.
§Examples
let laughing = emojis::get("😆").unwrap();
assert_eq!(
laughing.shortcodes().collect::<Vec<_>>(),
vec!["laughing", "satisfied"]
);