use std::{borrow::Borrow, fmt, ops::Deref, sync::Arc};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[derive(Clone, Debug, Eq, Hash, PartialEq, Ord, PartialOrd)]
pub struct ViewName(Arc<str>);
impl Serialize for ViewName {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.0)
}
}
impl<'de> Deserialize<'de> for ViewName {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Ok(Self::from(s))
}
}
impl ViewName {
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn as_arc(&self) -> Arc<str> {
Arc::clone(&self.0)
}
}
impl fmt::Display for ViewName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl Deref for ViewName {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl AsRef<str> for ViewName {
fn as_ref(&self) -> &str {
&self.0
}
}
impl Borrow<str> for ViewName {
fn borrow(&self) -> &str {
&self.0
}
}
impl From<&str> for ViewName {
fn from(value: &str) -> Self {
Self(Arc::from(value))
}
}
impl From<String> for ViewName {
fn from(value: String) -> Self {
Self(Arc::from(value.into_boxed_str()))
}
}
impl From<&String> for ViewName {
fn from(value: &String) -> Self {
Self(Arc::from(value.as_str()))
}
}
impl From<Arc<str>> for ViewName {
fn from(value: Arc<str>) -> Self {
Self(value)
}
}
impl From<ViewName> for String {
fn from(value: ViewName) -> Self {
value.0.as_ref().to_owned()
}
}
impl PartialEq<str> for ViewName {
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}
}
impl PartialEq<&str> for ViewName {
fn eq(&self, other: &&str) -> bool {
self.as_str() == *other
}
}
impl PartialEq<String> for ViewName {
fn eq(&self, other: &String) -> bool {
self.as_str() == other.as_str()
}
}
#[cfg(test)]
mod tests;