1#![doc = include_str!("readme.md")]
2
3use core::{
8 fmt::{
9 Debug,
10 Display,
11 },
12 ops::{
13 BitOr,
14 FromResidual,
15 Try,
16 },
17};
18
19use crate::{
20 Contexting,
21 Parse,
22 Parsed,
23 Streaming,
24};
25
26mod span;
27pub use span::*;
28mod opt;
29pub use opt::*;
30mod and;
31pub use and::*;
32mod and_then;
33pub use and_then::*;
34mod and_drop;
35pub use and_drop::*;
36mod drop_and;
37pub use drop_and::*;
38mod or;
39pub use or::*;
40mod not;
41pub use not::*;
42mod peek;
43pub use peek::*;
44mod map;
45pub use map::*;
46mod to;
47pub use to::*;
48mod try_map;
49pub use try_map::*;
50mod drop;
51pub use drop::*;
52mod filter;
58pub use filter::*;
59mod filter_map;
60pub use filter_map::*;
61
62mod fold_bounds;
63pub use fold_bounds::*;
64mod try_fold_bounds;
65pub use try_fold_bounds::*;
66mod try_fold_iter;
67pub use try_fold_iter::*;
68mod fold_until;
69pub use fold_until::*;
70mod try_fold_until;
71pub use try_fold_until::*;
72mod fill;
73pub use fill::*;
74
75mod enumerate;
76pub use enumerate::*;
77mod limit;
78pub use limit::*;
79
80mod add_atom;
81pub use add_atom::*;
82
83mod acc;
84pub use acc::*;
85mod try_acc;
86pub use try_acc::*;
87mod extend;
88pub use extend::*;
89mod try_extend;
90pub use try_extend::*;
91mod push;
92pub use push::*;
93mod try_push;
94pub use try_push::*;
95
96#[derive(Debug, Clone, Copy, PartialEq, Eq)]
98pub enum UtilsAtom<Stream> {
99 MinNotReach {
101 i: usize,
103 min: usize,
105 },
106 UntilNotReach,
108 Max(usize),
112 Filter,
114 Diff {
119 stream: Stream,
121 stream_success: Stream,
123 },
124}
125
126impl<Stream> Display for UtilsAtom<Stream> {
127 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
128 match self {
129 UtilsAtom::MinNotReach { i, min } => write!(f, "MinNotReach: {} < {}", i, min),
130 UtilsAtom::UntilNotReach => write!(f, "UntilNotReach"),
131 UtilsAtom::Max(n) => write!(f, "Max {}", n),
133 UtilsAtom::Filter { .. } => write!(f, "Filter"),
134 UtilsAtom::Diff { .. } => write!(f, "Diff"),
135 }
136 }
137}
138
139pub trait Utils<Stream, Context>: Sized + Parse<Stream, Context>
141where
142 Stream: Streaming,
143{
144 fn and_then<OtherParser, F>(self, f: F) -> AndThen<Self, F>
149 where
150 OtherParser: Parse<Stream, Context>,
151 F: Fn(Self::Token) -> OtherParser,
152 {
153 and_then(self, f)
154 }
155
156 fn and<OtherParser, OtherToken>(self, other: OtherParser) -> And<Self, OtherParser>
160 where
161 OtherParser: Parse<Stream, Context, Token = OtherToken>,
162 {
163 and(self, other)
164 }
165
166 fn and_drop<OtherParser, OtherToken>(self, other: OtherParser) -> AndDrop<Self, OtherParser>
169 where
170 OtherParser: Parse<Stream, Context, Token = OtherToken>,
171 {
172 and_drop(self, other)
173 }
174
175 fn drop_and<OtherParser, OtherToken>(self, other: OtherParser) -> DropAnd<Self, OtherParser>
178 where
179 OtherParser: Parse<Stream, Context, Token = OtherToken>,
180 {
181 drop_and(self, other)
182 }
183
184 fn drop(self) -> Drop<Self> {
187 drop(self)
188 }
189
190 fn fill<const N: usize>(self) -> Fill<Self, N>
193 where
194 Context: Contexting<UtilsAtom<Stream>>,
195 {
196 fill(self)
197 }
198
199 fn try_fold_iter<IntoIter, Init, Acc, Ret, F>(
206 self, iter: IntoIter, init: Init, f: F,
207 ) -> TryFoldIter<Self, IntoIter, Init, F>
208 where
209 Context: Contexting<UtilsAtom<Stream>>,
210 IntoIter: IntoIterator + Clone,
211 Init: Fn() -> Ret,
212 F: Fn(Acc, Self::Token, IntoIter::Item) -> Ret,
213 Ret: Try<Output = Acc>,
214 Parsed<Acc, Stream, Context>: FromResidual<Ret::Residual>,
215 {
216 try_fold_iter(self, iter, init, f)
217 }
218
219 fn fold_until<TokenUntil, Acc, Until, Init, F>(
226 self, until: Until, init: Init, f: F,
227 ) -> FoldUntil<Self, Until, Init, F>
228 where
229 Context: Contexting<UtilsAtom<Stream>>,
230 Until: Parse<Stream, Context, Token = TokenUntil>,
231 Init: FnMut() -> Acc,
232 F: FnMut(Acc, Self::Token) -> Acc,
233 {
234 fold_until(self, until, init, f)
235 }
236
237 fn try_fold_until<TokenUntil, Acc, Parser, Until, Init, Ret, F>(
239 self, until: Until, init: Init, f: F,
240 ) -> TryFoldUntil<Self, Until, Init, F>
241 where
242 Context: Contexting<UtilsAtom<Stream>>,
243 Until: Parse<Stream, Context, Token = TokenUntil>,
244 Init: Fn() -> Ret,
245 F: Fn(Acc, Self::Token) -> Ret,
246 Ret: Try<Output = Acc>,
247 Parsed<(Acc, Until::Token), Stream, Context>: FromResidual<Ret::Residual>,
248 {
249 try_fold_until(self, until, init, f)
250 }
251
252 fn fold_bounds<Bounds, Acc, Init, F>(
272 self, bounds: Bounds, init: Init, f: F,
273 ) -> FoldBounds<Self, Bounds, Init, F>
274 where
275 Context: Contexting<UtilsAtom<Stream>>,
276 Init: FnMut() -> Acc,
277 F: FnMut(Acc, Self::Token) -> Acc,
278 Bounds: FoldBoundsParse,
279 Acc: Debug,
280 {
281 fold_bounds(self, bounds, init, f)
282 }
283
284 fn try_fold_bounds<Bounds, Acc, Init, Ret, F>(
286 self, bounds: Bounds, init: Init, f: F,
287 ) -> TryFoldBounds<Self, Bounds, Init, F>
288 where
289 Context: Contexting<UtilsAtom<Stream>>,
290 Init: Fn() -> Ret,
291 F: Fn(Acc, Self::Token) -> Ret,
292 Ret: Try<Output = Acc>,
293 Parsed<Acc, Stream, Context>: FromResidual<Ret::Residual>,
294 Bounds: TryFoldBoundsParse,
295 Acc: Debug,
296 {
297 try_fold_bounds(self, bounds, init, f)
298 }
299
300 fn add_atom<F, Atom>(self, f: F) -> AddAtom<Self, F>
303 where
304 F: Fn() -> Atom,
305 Context: Contexting<Atom>,
306 {
307 add_atom(self, f)
308 }
309
310 fn map<F, OtherToken>(self, f: F) -> Map<Self, F>
314 where
315 F: Fn(Self::Token) -> OtherToken,
316 {
317 map(self, f)
318 }
319
320 fn filter<F>(self, f: F) -> Filter<Self, F>
322 where
323 F: Fn(&Self::Token) -> bool,
324 Context: Contexting<UtilsAtom<Stream>>,
325 {
326 filter(self, f)
327 }
328
329 fn filter_map<F, OtherToken>(self, f: F) -> FilterMap<Self, F>
332 where
333 F: Fn(Self::Token) -> Option<OtherToken>,
334 OtherToken: Clone,
335 Context: Contexting<UtilsAtom<Stream>>,
336 {
337 filter_map(self, f)
338 }
339
340 fn to<OtherToken>(self, t: OtherToken) -> To<Self, OtherToken>
345 where
346 OtherToken: Clone,
347 {
348 to(self, t)
349 }
350
351 fn not(self) -> Not<Self>
355 where
356 Stream: Clone,
357 Context: Contexting<NotAtom<Self::Token, Stream>>,
358 {
359 not(self)
360 }
361
362 fn opt(self) -> Optional<Self>
366 where
367 Stream: Clone,
368 {
369 opt(self)
370 }
371
372 fn enumerate(self) -> Enumerate<Self> {
375 enumerate(self)
376 }
377
378 fn limit(self, n: usize) -> Limit<Self>
383 where
384 Context: Contexting<UtilsAtom<Stream>>,
385 {
386 limit(self, n)
387 }
388
389 fn or<OtherParser>(self, b: OtherParser) -> Or<Self, OtherParser>
393 where
394 Stream: Clone,
395 OtherParser: Parse<Stream, Context>,
396 Context: BitOr,
397 {
398 or(self, b)
399 }
400
401 fn peek(self) -> Peek<Self>
405 where
406 Stream: Clone,
407 {
408 peek(self)
409 }
410
411 fn span(self) -> Span<Self>
417 where
418 Context: Contexting<UtilsAtom<Stream>>,
419 {
420 span(self)
421 }
422
423 fn try_map<OtherToken, F, Ret>(self, f: F) -> TryMap<Self, F>
425 where
426 F: Fn(Self::Token) -> Ret,
427 Ret: Try<Output = OtherToken>,
428 Parsed<OtherToken, Stream, Context>: FromResidual<Ret::Residual>,
429 {
430 try_map(self, f)
431 }
432}
433
434impl<T, Stream, Context> Utils<Stream, Context> for T
435where
436 Stream: Streaming,
437 Self: Parse<Stream, Context>,
438{
439}