use anyhow::{ensure, Result};
use serde::{Deserialize, Serialize};
use std::{fmt, str::FromStr};
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct StickerId(String);
impl StickerId {
pub fn parse(value: impl AsRef<str>) -> Result<Self> {
let value = value.as_ref();
ensure!(
value.len() == 32 && value.bytes().all(|b| b.is_ascii_hexdigit()),
"invalid_sticker_id"
);
Ok(Self(value.to_ascii_lowercase()))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct StickerPackId(String);
impl StickerPackId {
pub fn parse(value: impl AsRef<str>) -> Result<Self> {
let value = value.as_ref();
ensure!(
!value.is_empty() && value.len() <= 1024 && !value.chars().any(char::is_control),
"invalid_sticker_pack_id"
);
Ok(Self(value.into()))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
macro_rules! identity_traits {
($kind:ty) => {
impl FromStr for $kind {
type Err = anyhow::Error;
fn from_str(value: &str) -> Result<Self> {
Self::parse(value)
}
}
impl TryFrom<String> for $kind {
type Error = anyhow::Error;
fn try_from(value: String) -> Result<Self> {
Self::parse(value)
}
}
impl From<$kind> for String {
fn from(value: $kind) -> Self {
value.0
}
}
impl AsRef<str> for $kind {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::ops::Deref for $kind {
type Target = str;
fn deref(&self) -> &str {
self.as_str()
}
}
impl fmt::Display for $kind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
};
}
identity_traits!(StickerId);
identity_traits!(StickerPackId);