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
use Debug;
use PhantomData;
use crateCtor;
use crateHandler;
use crateMatch;
use crateError;
use crateFallibleMap;
use crateRegex;
use crateimpl_not_for_regex;
use crateSpan;
///
/// Transforms the output type of a pattern through a mapping function.
///
/// This combinator allows you to change the result type of a constructor without affecting
/// its matching behavior. It's particularly useful for converting parsed values into more
/// convenient or domain-specific types immediately after parsing.
///
/// # Regex
///
/// The `Regex` implementation simply delegates to the inner pattern. The mapping function
/// has no effect on matching behavior - it only affects construction results. This ensures
/// that pattern matching and token recognition remain unchanged while allowing flexible
/// transformation of parsed values.
///
/// ## Example
///
/// ```
/// # use neure::err::Error;
/// # use neure::prelude::*;
/// #
/// # fn main() -> Result<(), Box<dyn core::error::Error>> {
/// let str = neu::ascii_alphabetic().count::<3>();
/// let num = neu::digit(10)
/// .count::<3>()
/// .try_map::<_, (Span, i32)>(|v: (Span, &str)| {
/// v.1.parse::<i32>()
/// .map_err(|_| Error::Uid(0))
/// .map(|n| (v.0, n))
/// });
/// let mut ctx = CharsCtx::new("foo777");
///
/// assert_eq!(ctx.try_mat(&str.then(num))?, Span::new(0, 6));
///
/// # Ok(())
/// # }
/// ```
///
/// # Ctor
///
/// First constructs a value of type `O` using the inner pattern, then applies the mapping
/// function to transform it into type `V`. If the inner pattern fails to construct a value,
/// the mapping function is never called and the error is propagated directly.
///
/// ## Example
///
/// ```
/// # use neure::prelude::*;
/// #
/// # fn main() -> Result<(), Box<dyn core::error::Error>> {
/// let str = regex!((neu::ascii_alphabetic()){3}).map(String::from);
/// let num = neu::digit(10)
/// .count::<3>()
/// // map &str to i32
/// .try_map(map::from_str::<i32>());
/// let re = str.then(num);
///
/// assert_eq!(CharsCtx::new("foo777").ctor(&re)?, ("foo".to_string(), 777));
///
/// # Ok(())
/// # }
/// ```
///
/// # Mapping Function
///
/// The mapping function must implement the [`FallibleMap`] trait, which provides the
/// `map_to` method for converting from input type `O` to output type `V`. Common implementations
/// include:
/// - Simple closures: `|x| x.to_uppercase()`
/// - Function pointers: `str::parse`
/// - Struct constructors: `MyType::from`
/// - Complex transformations: `|x| MyType { value: x.trim().to_string() }`
///
/// ## Example
///
/// ```
/// # use neure::err::Error;
/// # use neure::prelude::*;
/// #
/// # fn main() -> Result<(), Box<dyn core::error::Error>> {
/// let num = neu::digit(10).count::<3>();
/// let id =
/// CharsCtx::new("777").map(&num, |v: &str| v.parse::<i32>().map_err(|_| Error::Uid(0)))?;
///
/// assert_eq!(id, 777);
///
/// let (span, id) = CharsCtx::new("777").map_with(&num, |ctx, span| {
/// let orig = span.orig(ctx)?;
///
/// Ok((*span, orig.parse::<i32>().map_err(|_| Error::Uid(0))?))
/// })?;
///
/// assert_eq!(id, 777);
/// assert_eq!(span, Span::new(0, 3));
///
/// # Ok(())
/// # }
/// ```
///
/// # Performance
///
/// The mapping operation adds negligible overhead when the mapping function itself is efficient.
/// Since the mapping only occurs after successful construction, there is no performance penalty
/// for failed matches. For optimal performance, ensure your mapping function is lightweight
/// or consider moving heavy transformations outside the parsing phase.
///
impl_not_for_regex!;