1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use std::fmt::{self, Debug, Formatter};
use std::str::FromStr;
use amplify::ascii::{AsAsciiStrError, AsciiChar, AsciiString, FromAsciiError};
use amplify::confinement::Confined;
use amplify::{confinement, Wrapper};
use crate::{impl_strict_newtype, STRICT_TYPES_LIB};
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Display, Error, From)]
#[display(doc_comments)]
pub enum InvalidIdent {
Empty,
NonAlphabetic(AsciiChar),
InvalidChar(AsciiChar),
#[from(AsAsciiStrError)]
NonAsciiChar,
#[from]
Confinement(confinement::Error),
}
impl<O> From<FromAsciiError<O>> for InvalidIdent {
fn from(_: FromAsciiError<O>) -> Self { InvalidIdent::NonAsciiChar }
}
#[derive(Wrapper, WrapperMut, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
#[wrapper(Deref, Display)]
#[wrapper_mut(DerefMut)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
pub struct Ident(Confined<AsciiString, 1, 64>);
impl FromStr for Ident {
type Err = InvalidIdent;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = AsciiString::from_ascii(s.as_bytes())?;
Ident::try_from(s)
}
}
impl From<&'static str> for Ident {
fn from(s: &'static str) -> Self { Self::from_str(s).expect("invalid identifier name") }
}
impl TryFrom<String> for Ident {
type Error = InvalidIdent;
fn try_from(s: String) -> Result<Self, Self::Error> {
let s = AsciiString::from_ascii(s.as_bytes())?;
Ident::try_from(s)
}
}
impl TryFrom<AsciiString> for Ident {
type Error = InvalidIdent;
fn try_from(ascii: AsciiString) -> Result<Self, InvalidIdent> {
if ascii.is_empty() {
return Err(InvalidIdent::Empty);
}
let first = ascii[0];
if !first.is_alphabetic() && first != '_' {
return Err(InvalidIdent::NonAlphabetic(first));
}
if let Some(ch) = ascii
.as_slice()
.iter()
.copied()
.find(|ch| !ch.is_ascii_alphanumeric() && *ch != b'_')
{
return Err(InvalidIdent::InvalidChar(ch));
}
let s = Confined::try_from(ascii)?;
Ok(Self(s))
}
}
impl Debug for Ident {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_tuple("Ident").field(&self.as_str()).finish()
}
}
impl_strict_newtype!(Ident, STRICT_TYPES_LIB, "Dumb");
#[derive(Wrapper, WrapperMut, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
#[wrapper(Deref, Display, FromStr)]
#[wrapper_mut(DerefMut)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
pub struct TypeName(Ident);
impl From<&'static str> for TypeName {
fn from(ident: &'static str) -> Self { TypeName(Ident::from(ident)) }
}
impl TryFrom<String> for TypeName {
type Error = InvalidIdent;
fn try_from(s: String) -> Result<Self, Self::Error> { Ident::try_from(s).map(Self) }
}
impl Debug for TypeName {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_tuple("TypeName").field(&self.as_str()).finish()
}
}
impl_strict_newtype!(TypeName, STRICT_TYPES_LIB);
#[derive(Wrapper, WrapperMut, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
#[wrapper(Deref, Display, FromStr)]
#[wrapper_mut(DerefMut)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
pub struct FieldName(Ident);
impl From<&'static str> for FieldName {
fn from(ident: &'static str) -> Self { FieldName(Ident::from(ident)) }
}
impl TryFrom<String> for FieldName {
type Error = InvalidIdent;
fn try_from(s: String) -> Result<Self, Self::Error> { Ident::try_from(s).map(Self) }
}
impl Debug for FieldName {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_tuple("FieldName").field(&self.as_str()).finish()
}
}
impl_strict_newtype!(FieldName, STRICT_TYPES_LIB);
pub type VariantName = FieldName;
#[derive(Wrapper, WrapperMut, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
#[wrapper(Deref, Display, FromStr)]
#[wrapper_mut(DerefMut)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
pub struct LibName(Ident);
impl From<&'static str> for LibName {
fn from(ident: &'static str) -> Self { LibName(Ident::from(ident)) }
}
impl TryFrom<String> for LibName {
type Error = InvalidIdent;
fn try_from(s: String) -> Result<Self, Self::Error> { Ident::try_from(s).map(Self) }
}
impl Debug for LibName {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_tuple("LibName").field(&self.as_str()).finish()
}
}
impl_strict_newtype!(LibName, STRICT_TYPES_LIB);