use std::str::FromStr;
use amplify::Wrapper;
use crate::stl::{AlphaCapsLodash, AlphaLodash, AlphaNumLodash, AlphaSmallLodash};
use crate::{RString, STRICT_TYPES_LIB};
pub const IDENT_MAX_LEN: usize = 100;
#[macro_export]
macro_rules! impl_ident_type {
($ty:ty) => {
impl From<$ty> for String {
#[inline]
fn from(ident: $ty) -> String { ident.0.into() }
}
impl From<&'static str> for $ty {
#[inline]
fn from(ident: &'static str) -> Self { Self(RString::from(ident)) }
}
impl TryFrom<String> for $ty {
type Error = $crate::InvalidRString;
#[inline]
fn try_from(s: String) -> Result<Self, Self::Error> { Self::from_str(&s) }
}
impl ::core::fmt::Debug for $ty {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.debug_tuple(&$crate::type_name::<Self>()).field(&self.as_str()).finish()
}
}
impl ::core::borrow::Borrow<str> for $ty {
#[inline]
fn borrow(&self) -> &str { self.as_str() }
}
impl AsRef<str> for $ty {
#[inline]
fn as_ref(&self) -> &str { self.as_str() }
}
impl $ty {
#[inline]
pub fn as_str(&self) -> &str { self.0.as_str() }
}
};
}
#[macro_export]
macro_rules! impl_ident_subtype {
($ty:ty) => {
impl From<$ty> for $crate::Ident {
#[inline]
fn from(name: $ty) -> Self {
$crate::Ident::from_str(name.as_str()).expect("ident is a superset")
}
}
impl $ty {
#[inline]
pub fn to_ident(&self) -> $crate::Ident { self.clone().into() }
#[inline]
pub fn into_ident(self) -> $crate::Ident { self.into() }
}
};
}
#[derive(Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
#[wrapper(Deref, Display, FromStr)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
pub struct Ident(RString<AlphaLodash, AlphaNumLodash, 1, IDENT_MAX_LEN>);
impl_ident_type!(Ident);
impl_strict_newtype!(Ident, STRICT_TYPES_LIB);
#[derive(Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
#[wrapper(Deref, Display, FromStr)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
pub struct TypeName(RString<AlphaCapsLodash, AlphaNumLodash, 1, IDENT_MAX_LEN>);
impl_ident_type!(TypeName);
impl_ident_subtype!(TypeName);
impl_strict_newtype!(TypeName, STRICT_TYPES_LIB);
#[derive(Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
#[wrapper(Deref, Display, FromStr)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
pub struct FieldName(RString<AlphaSmallLodash, AlphaNumLodash, 1, IDENT_MAX_LEN>);
impl_ident_type!(FieldName);
impl_ident_subtype!(FieldName);
impl_strict_newtype!(FieldName, STRICT_TYPES_LIB);
#[derive(Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
#[wrapper(Deref, Display, FromStr)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
pub struct VariantName(RString<AlphaSmallLodash, AlphaNumLodash, 1, IDENT_MAX_LEN>);
impl_ident_type!(VariantName);
impl_ident_subtype!(VariantName);
impl_strict_newtype!(VariantName, STRICT_TYPES_LIB);
#[derive(Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
#[wrapper(Deref, Display, FromStr)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
pub struct LibName(RString<AlphaCapsLodash, AlphaNumLodash, 1, IDENT_MAX_LEN>);
impl_ident_type!(LibName);
impl_ident_subtype!(LibName);
impl_strict_newtype!(LibName, STRICT_TYPES_LIB);