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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// =============================================================================
// USES
// =============================================================================
// -----------------------------------------------------------------------------
use crateAppendClosed;
use Debug;
use Hash;
// =============================================================================
// TYPES
// =============================================================================
/// A trait for signifying that a type can be used as a delimiter in an
/// identifier.
///
/// # Must Be a Char!
///
/// A delimiter must be exactly one [`char`] (one unicode code point).
///
/// If joining characters are present to the left or right of a delimiter that,
/// this crate will consider them incomplete parts of the surrounding chunk,
/// *NOT* a part of the delimiter (even if they visually appear that way).
///
/// # Must be Optional!
///
/// A valid identifier is allowed to have no delimiters whatsoever.
///
/// This crate does not enforce the structure of an identifier past validating
/// start and continue characters. If you need something more explicit, like an
/// identifier that *MUST* start with a specific character, it's recommended you
/// wrap the identifier in a new-type and handle that validation explicitly.
///
/// For example, for PHP it would be recommended to do the following:
///
/// ```
/// # #[derive(Copy, Clone, Debug)]
/// # enum MyError { NoDollarStart, FormatError }
/// # impl From<typed_ident::Error> for MyError {
/// # fn from(_: typed_ident::Error) -> Self {
/// # Self::FormatError
/// # }
/// # }
/// use typed_ident::presets::ascii::CamelIdent;
///
/// struct PhpVariable<'a>(&'a CamelIdent);
///
/// impl<'a> PhpVariable<'a> {
/// pub fn new(s: &'a str) -> Result<Self, MyError> {
/// let Some(remaining) = s.strip_prefix('$') else {
/// return Err(MyError::NoDollarStart);
/// };
/// Ok(Self(CamelIdent::new(remaining)?))
/// }
/// }
/// ```
///
/// # Must *NOT* Be Stateful!
///
/// A delimiter character must not be different depending on whether it's been
/// parsed from [`is_ident_start`] vs. [`is_chunk_delim`].
///
/// If you were to use stateful delimiters, then you might be surprised by the
/// values returned by segmentation (they may not be what you expect). It will
/// still segment chunks properly, but the delimiter values may be strange.
///
/// For example, this would be an ***INCORRECT*** implementation:
///
/// ```
/// # use typed_ident::syntax::delimiter::Delimiter;
/// #[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
/// enum BadDelimiter {
/// FoundAtStart,
/// FoundInChunk,
/// }
///
/// impl Delimiter for BadDelimiter {
/// fn as_char(&self) -> char {
/// '_'
/// }
/// fn from_ident_start(c: char) -> Option<Self> {
/// if c == '_' {
/// Some(Self::FoundAtStart)
/// } else {
/// None
/// }
/// }
/// fn from_chunk_delim(c: char) -> Option<Self> {
/// if c == '_' {
/// Some(Self::FoundInChunk)
/// } else {
/// None
/// }
/// }
/// }
/// ```
///
/// [`is_chunk_delim`]: Delimiter::is_chunk_delim
/// [`is_ident_start`]: Delimiter::is_ident_start