1use std::str::FromStr;
25
26use amplify::Wrapper;
27
28use crate::stl::{AlphaCapsLodash, AlphaLodash, AlphaNumLodash, AlphaSmallLodash};
29use crate::{RString, STRICT_TYPES_LIB};
30
31pub const IDENT_MAX_LEN: usize = 100;
32
33#[macro_export]
34macro_rules! impl_ident_type {
35 ($ty:ty) => {
36 impl From<$ty> for String {
37 #[inline]
38 fn from(ident: $ty) -> String { ident.0.into() }
39 }
40
41 impl From<&'static str> for $ty {
42 #[inline]
43 fn from(ident: &'static str) -> Self { Self(RString::from(ident)) }
44 }
45
46 impl TryFrom<String> for $ty {
47 type Error = $crate::InvalidRString;
48
49 #[inline]
50 fn try_from(s: String) -> Result<Self, Self::Error> { Self::from_str(&s) }
51 }
52
53 impl ::core::fmt::Debug for $ty {
54 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
55 f.debug_tuple(&$crate::type_name::<Self>()).field(&self.as_str()).finish()
56 }
57 }
58
59 impl ::core::borrow::Borrow<str> for $ty {
60 #[inline]
61 fn borrow(&self) -> &str { self.as_str() }
62 }
63
64 impl AsRef<str> for $ty {
65 #[inline]
66 fn as_ref(&self) -> &str { self.as_str() }
67 }
68
69 impl $ty {
70 #[inline]
72 pub fn as_str(&self) -> &str { self.0.as_str() }
73 }
74 };
75}
76
77#[macro_export]
78macro_rules! impl_ident_subtype {
79 ($ty:ty) => {
80 impl From<$ty> for $crate::Ident {
81 #[inline]
82 fn from(name: $ty) -> Self {
83 $crate::Ident::from_str(name.as_str()).expect("ident is a superset")
84 }
85 }
86
87 impl $ty {
88 #[inline]
90 pub fn to_ident(&self) -> $crate::Ident { self.clone().into() }
91 #[inline]
93 pub fn into_ident(self) -> $crate::Ident { self.into() }
94 }
95 };
96}
97
98#[derive(Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
99#[wrapper(Deref, Display, FromStr)]
100#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
101pub struct Ident(RString<AlphaLodash, AlphaNumLodash, 1, IDENT_MAX_LEN>);
102
103impl_ident_type!(Ident);
104impl_strict_newtype!(Ident, STRICT_TYPES_LIB);
105
106#[derive(Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
107#[wrapper(Deref, Display, FromStr)]
108#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
109pub struct TypeName(RString<AlphaCapsLodash, AlphaNumLodash, 1, IDENT_MAX_LEN>);
110
111impl_ident_type!(TypeName);
112impl_ident_subtype!(TypeName);
113impl_strict_newtype!(TypeName, STRICT_TYPES_LIB);
114
115#[derive(Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
116#[wrapper(Deref, Display, FromStr)]
117#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
118pub struct FieldName(RString<AlphaSmallLodash, AlphaNumLodash, 1, IDENT_MAX_LEN>);
119
120impl_ident_type!(FieldName);
121impl_ident_subtype!(FieldName);
122impl_strict_newtype!(FieldName, STRICT_TYPES_LIB);
123
124#[derive(Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
125#[wrapper(Deref, Display, FromStr)]
126#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
127pub struct VariantName(RString<AlphaSmallLodash, AlphaNumLodash, 1, IDENT_MAX_LEN>);
128
129impl_ident_type!(VariantName);
130impl_ident_subtype!(VariantName);
131impl_strict_newtype!(VariantName, STRICT_TYPES_LIB);
132
133#[derive(Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
134#[wrapper(Deref, Display, FromStr)]
135#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
136pub struct LibName(RString<AlphaCapsLodash, AlphaNumLodash, 1, IDENT_MAX_LEN>);
137
138impl_ident_type!(LibName);
139impl_ident_subtype!(LibName);
140impl_strict_newtype!(LibName, STRICT_TYPES_LIB);