use non_non_full::NonEmptyString;
use serde::{Deserialize, Serialize};
use std::ops::Deref;
use tap::TapOptional;
#[derive(PartialEq, Eq, Clone, Hash, Serialize, Deserialize, Debug)]
pub struct EntryId(pub NonEmptyString);
impl EntryId {
#[must_use]
pub fn new(s: String) -> Option<Self> {
let inner = NonEmptyString::new(s).tap_none(|| {
tracing::warn!("Tried to create an Entry ID from an empty string");
})?;
Some(Self(inner))
}
#[must_use]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl Deref for EntryId {
type Target = str;
fn deref(&self) -> &Self::Target {
self.0.as_str()
}
}
impl From<NonEmptyString> for EntryId {
fn from(value: NonEmptyString) -> Self {
Self(value)
}
}
impl From<u32> for EntryId {
fn from(value: u32) -> Self {
Self(
NonEmptyString::new(value.to_string())
.expect("a number's string representation should never be empty"),
)
}
}
impl TryFrom<&str> for EntryId {
type Error = ();
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::new(value.to_owned()).ok_or(())
}
}
impl TryFrom<String> for EntryId {
type Error = ();
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::new(value).ok_or(())
}
}
impl PartialEq<str> for EntryId {
fn eq(&self, other: &str) -> bool {
self.0.as_str() == other
}
}