use std::borrow::Borrow;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::sync::Arc;
#[derive(Clone)]
pub struct SharedText(Repr);
#[derive(Clone)]
enum Repr {
Static(&'static str),
Shared(Arc<str>),
}
impl SharedText {
pub const fn from_static(text: &'static str) -> SharedText {
SharedText(Repr::Static(text))
}
pub fn as_str(&self) -> &str {
match &self.0 {
Repr::Static(text) => text,
Repr::Shared(text) => text,
}
}
}
impl Deref for SharedText {
type Target = str;
fn deref(&self) -> &str {
self.as_str()
}
}
impl AsRef<str> for SharedText {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Borrow<str> for SharedText {
fn borrow(&self) -> &str {
self.as_str()
}
}
impl From<&'static str> for SharedText {
fn from(text: &'static str) -> SharedText {
SharedText::from_static(text)
}
}
impl From<String> for SharedText {
fn from(text: String) -> SharedText {
SharedText(Repr::Shared(text.into()))
}
}
impl From<Arc<str>> for SharedText {
fn from(text: Arc<str>) -> SharedText {
SharedText(Repr::Shared(text))
}
}
impl PartialEq for SharedText {
fn eq(&self, other: &SharedText) -> bool {
self.as_str() == other.as_str()
}
}
impl Eq for SharedText {}
impl PartialEq<str> for SharedText {
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}
}
impl PartialEq<&str> for SharedText {
fn eq(&self, other: &&str) -> bool {
self.as_str() == *other
}
}
impl PartialOrd for SharedText {
fn partial_cmp(&self, other: &SharedText) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for SharedText {
fn cmp(&self, other: &SharedText) -> std::cmp::Ordering {
self.as_str().cmp(other.as_str())
}
}
impl Hash for SharedText {
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_str().hash(state)
}
}
impl fmt::Display for SharedText {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl fmt::Debug for SharedText {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self.as_str(), f)
}
}
impl serde::Serialize for SharedText {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}
impl<'de> serde::Deserialize<'de> for SharedText {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<SharedText, D::Error> {
String::deserialize(deserializer).map(SharedText::from)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Kind {
name: SharedText,
title: Option<SharedText>,
}
impl Kind {
pub const fn new(name: &'static str) -> Kind {
Kind { name: SharedText::from_static(name), title: None }
}
pub const fn titled(name: &'static str, title: &'static str) -> Kind {
Kind { name: SharedText::from_static(name), title: Some(SharedText::from_static(title)) }
}
pub fn shared(name: impl Into<Arc<str>>) -> Kind {
Kind { name: SharedText::from(name.into()), title: None }
}
pub fn with_title(self, title: impl Into<Arc<str>>) -> Kind {
Kind { title: Some(SharedText::from(title.into())), ..self }
}
pub fn name(&self) -> &str {
self.name.as_str()
}
pub fn title(&self) -> Option<&str> {
self.title.as_deref()
}
pub(super) fn shared_name(&self) -> &SharedText {
&self.name
}
pub(super) fn shared_title(&self) -> Option<&SharedText> {
self.title.as_ref()
}
}