use emojicons::EmojiFormatter;
use serde::de::{Deserialize, Deserializer, Error};
use std::{char::ParseCharError, ops::Deref, str::FromStr};
#[derive(Clone, Copy, Debug)]
pub struct Emoji(char);
impl Deref for Emoji {
type Target = char;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl ToString for Emoji {
fn to_string(&self) -> String {
format!("{}", self.0)
}
}
impl FromStr for Emoji {
type Err = ParseCharError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
format!("{}", EmojiFormatter(&format!(":{}:", s)))
.parse::<char>()
.map(Emoji)
}
}
impl<'de> Deserialize<'de> for Emoji {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
String::deserialize(deserializer)
.and_then(|s| Emoji::from_str(&s).map_err(D::Error::custom))
}
}