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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//! This module contains parsing extensions to the standard library implementations. Notably
//! implementations of [`FromStrFront`]/[`FromStrBack`] which will try to parse as much of a string
//! as it can.
use FromStr;
// TODO: floats and other notable types
pub use ;
/// Types that may try parsing from the beginning of a [`str`]. While [`FromStr`] generally requires
/// the whole input to be a valid representation of `Self`, this trait tries to parse until it
/// encounters unknown input and ignores it. Since it is trivial to [`FromStr`] if [`FromStrFront`]
/// is implemented it is a required super trait, see the [`forward`] macro for forwarding the
/// implementation easily on your own types.
/// Types that may try parsing from the end of a [`str`]. While [FromStr] generally requires the
/// whole input to be a valid representation of `Self`, this trait tries to parse until it
/// encounters unknown input and ignores it. Since it is trivial to [`FromStr`] if [`FromStrBack`]
/// is implemented it is a required super trait, see the [`forward`] macro for forwarding the
/// implementation easily on your own types.
/// Forwards [`FromStr`] to [`FromStrFront`]/[`FromStrBack`] by checking if all of the input was
/// consumed. The given closure-like argument binds the parsed value and rest [`str`] if
/// `from_str_*` doesn't completely consume the string and succeeded, allowing the implementor to
/// construct a custom error in this special case, or default if not given.
/// ```no_run
/// # #![feature(decl_macro)]
/// # #[macro_use] extern crate strtools;
/// use std::{str::FromStr, default::Default};
/// use strtools::parse::{forward, FromStrFront};
///
/// #[derive(Debug)]
/// struct A;
/// struct Error(String);
///
/// impl Default for Error {
/// fn default() -> Self {
/// Self("invalid".to_string())
/// }
/// }
///
/// impl FromStrFront for A {
/// type Error = Error;
///
/// fn from_str_front(input: &str) -> Result<(Self, &str), Self::Error> {
/// if let Some(rest) = input.strip_prefix('a') {
/// Ok((A, rest))
/// } else {
/// Err(Error(format!("invalid: {:?}, expected \"a...\"", input)))
/// }
/// }
/// }
///
/// # fn main() {
/// forward!(front for A; |parsed, rest| {
/// Error(format!("parsed {:?}, but had unexpected rest: {:?}", parsed, rest))
/// });
/// # }
/// ```
/// Generates this impl:
/// ```no_run
/// # use strtools::parse::FromStrFront;
/// # use std::str::FromStr;
/// # #[derive(Debug)] struct A; struct Error(String);
/// # impl FromStrFront for A {
/// # type Error = Error;
/// # fn from_str_front(input: &str) -> Result<(Self, &str), Self::Error> {
/// # if let Some(rest) = input.strip_prefix('a') {
/// # Ok((A, rest))
/// # } else {
/// # Err(Error(format!("invalid: {:?}, expected \"a...\"", input)))
/// # }
/// # }
/// # }
/// impl FromStr for A {
/// type Err = Error;
///
/// fn from_str(input: &str) -> Result::<Self, Self::Err> {
/// match A::from_str_front(input) {
/// Ok((value, "")) => Ok(value),
/// // the special error case with left over tokens
/// Ok((parsed, rest)) => Err({
/// Error(format!("parsed {:?}, but had unexpected rest: {:?}", parsed, rest))
/// }),
/// Err(err) => Err(err),
/// }
/// }
/// }
/// ```
/// Use [Default] for the special case:
/// ```
/// # #[macro_use] extern crate strtools;
/// # use strtools::parse::FromStrFront;
/// # use std::str::FromStr;
/// #[derive(Debug)]
/// struct A;
///
/// #[derive(Default)]
/// struct Error(String);
///
/// // FromStrFront impl omitted
/// // ...
///
/// # impl FromStrFront for A {
/// # type Error = Error;
/// # fn from_str_front(input: &str) -> Result<(Self, &str), Self::Error> {
/// # if let Some(rest) = input.strip_prefix('a') {
/// # Ok((A, rest))
/// # } else {
/// # Err(Error(format!("invalid: {:?}, expected \"a...\"", input)))
/// # }
/// # }
/// # }
/// # fn main() {
/// # use strtools::parse::forward;
/// forward!(front for A);
/// # }
/// ```
/// Likewise this forwards to the [FromStrBack] impl:
/// ```
/// # #[macro_use] extern crate strtools;
/// # use strtools::parse::FromStrBack;
/// # use std::str::FromStr;
/// #[derive(Debug)]
/// struct A;
/// struct Error(String);
///
/// // FromStrBack impl omitted
/// // ...
///
/// # impl FromStrBack for A {
/// # type Error = Error;
/// # fn from_str_back(input: &str) -> Result<(Self, &str), Self::Error> {
/// # if let Some(rest) = input.strip_suffix('a') {
/// # Ok((A, rest))
/// # } else {
/// # Err(Error(format!("invalid: {:?}, expected \"a...\"", input)))
/// # }
/// # }
/// # }
/// # fn main() {
/// # use strtools::parse::forward;
/// forward!(back for A; |parsed, rest| {
/// Error(format!("parsed {:?}, but had unexpected rest: {:?}", parsed, rest))
/// });
/// # }
/// ```
pub macro forward
/// An [`Error`][0] for [`FromStrBack`] on [`bool`]s.
///
/// [0]: std::error::Error
;
/// Returns true if a given `literal` was yielded form the front, behaves similar to
/// [`FromStrFront::from_str_front`] see it's documentation for more info.
/// Returns true if a given `literal` was yielded form the back, behaves similar to
/// [`FromStrBack::from_str_back`] see it's documentation for more info.