use std::borrow::Cow;
use std::fmt::{Display, Formatter, Result as FmtResult};
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum Name {
Named(Cow<'static, str>),
Generated(Cow<'static, str>),
}
#[allow(missing_docs)]
impl Name {
pub const ANY: Self = Self::named("any");
pub const ANY_TYPE: Self = Self::named("anyType");
pub const ANY_SIMPLE_TYPE: Self = Self::named("anySimpleType");
pub const TYPE: Self = Self::named("type");
pub const ANY_ATTRIBUTE: Self = Self::named("any_attribute");
}
impl Name {
#[must_use]
pub const fn named(name: &'static str) -> Self {
Self::Named(Cow::Borrowed(name))
}
#[must_use]
pub const fn generated(name: &'static str) -> Self {
Self::Generated(Cow::Borrowed(name))
}
#[must_use]
pub fn new_named<T>(name: T) -> Self
where
T: Into<Cow<'static, str>>,
{
Self::Named(name.into())
}
#[must_use]
pub fn new_generated<T>(name: T) -> Self
where
T: Into<Cow<'static, str>>,
{
Self::Generated(name.into())
}
#[must_use]
pub fn is_named(&self) -> bool {
matches!(self, Self::Named(_))
}
#[must_use]
pub fn is_generated(&self) -> bool {
matches!(self, Self::Generated(_))
}
#[must_use]
pub fn as_str(&self) -> &str {
match self {
Self::Named(s) => s,
Self::Generated(s) => s,
}
}
#[must_use]
pub fn as_named_str(&self) -> Option<&str> {
match self {
Self::Named(s) => Some(s),
Self::Generated(_) => None,
}
}
}
impl AsRef<str> for Name {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl AsMut<String> for Name {
fn as_mut(&mut self) -> &mut String {
let x = match self {
Self::Named(x) => {
*x = Cow::Owned((**x).to_owned());
x
}
Self::Generated(x) => {
*x = Cow::Owned((**x).to_owned());
x
}
};
let Cow::Owned(x) = x else {
unreachable!();
};
x
}
}
impl Display for Name {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
Self::Named(x) => write!(f, "{x}"),
Self::Generated(x) => write!(f, "{x}"),
}
}
}
impl From<String> for Name {
fn from(value: String) -> Self {
Self::Named(Cow::Owned(value))
}
}
impl From<&'static str> for Name {
fn from(value: &'static str) -> Self {
Self::Named(Cow::Borrowed(value))
}
}
impl From<Name> for String {
fn from(value: Name) -> Self {
value.as_str().to_owned()
}
}
impl From<Name> for Cow<'static, str> {
fn from(value: Name) -> Self {
match value {
Name::Named(s) => s,
Name::Generated(s) => s,
}
}
}