use core::{fmt, str::FromStr};
use compact_str::CompactString;
use thiserror::Error;
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(try_from = "CompactString"))]
#[repr(transparent)]
pub struct Author(CompactString);
#[derive(Debug, Error, Eq, PartialEq)]
pub enum AuthorError {
#[error(
"Author must have at least {} characters but has {_0}",
Author::MIN_CHAR_LENGTH
)]
TooShort(usize),
#[error(
"Author must not have more than {} characters but has {_0}",
Author::MAX_CHAR_LENGTH
)]
TooLong(usize),
}
impl Author {
pub const MIN_CHAR_LENGTH: usize = 2;
pub const MAX_CHAR_LENGTH: usize = 256;
pub fn new<T: AsRef<str> + Into<CompactString>>(author: T) -> Result<Self, AuthorError> {
match author.as_ref().chars().count() {
count if count < Self::MIN_CHAR_LENGTH => Err(AuthorError::TooShort(count)),
count if count > Self::MAX_CHAR_LENGTH => Err(AuthorError::TooLong(count)),
_ => Ok(Self(author.into())),
}
}
#[must_use]
#[inline]
pub unsafe fn new_unchecked<T: Into<CompactString>>(author: T) -> Self {
Self(author.into())
}
#[must_use]
#[inline]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl AsRef<str> for Author {
#[inline]
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl fmt::Display for Author {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl FromStr for Author {
type Err = AuthorError;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::new(s)
}
}
impl TryFrom<CompactString> for Author {
type Error = AuthorError;
#[inline]
fn try_from(value: CompactString) -> Result<Self, Self::Error> {
Self::new(value)
}
}