|
1 | 1 | use crate::input::{InputError, MapWithInputExt}; |
2 | 2 | use crate::parser::combinator::{ |
3 | | - Map, MapResult, Optional, Or, RepeatArrayVec, RepeatN, RepeatVec, WithPrefix, WithSuffix, |
| 3 | + Map, MapResult, Optional, Or, RepeatArrayVec, RepeatN, RepeatVec, WithConsumed, WithPrefix, |
| 4 | + WithSuffix, |
4 | 5 | }; |
5 | 6 | use crate::parser::error::{ParseError, WithErrorMsg}; |
6 | 7 | use crate::parser::iterator::{ParserIterator, ParserMatchesIterator}; |
@@ -233,6 +234,23 @@ pub trait Parser: Sized { |
233 | 234 | } |
234 | 235 | } |
235 | 236 |
|
| 237 | + /// Return the output of this parser as well as the bytes consumed. |
| 238 | + /// |
| 239 | + /// This can be used to map any errors that occur while processing the parsed input back to the |
| 240 | + /// problematic item's position in the input. |
| 241 | + /// |
| 242 | + /// # Examples |
| 243 | + /// ``` |
| 244 | + /// # use utils::parser::{self, Parser}; |
| 245 | + /// assert_eq!( |
| 246 | + /// parser::u32().with_consumed().parse(b"012,345,678"), |
| 247 | + /// Ok(((12, &b"012"[..]), &b",345,678"[..])) |
| 248 | + /// ); |
| 249 | + /// ``` |
| 250 | + fn with_consumed(self) -> WithConsumed<Self> { |
| 251 | + WithConsumed { parser: self } |
| 252 | + } |
| 253 | + |
236 | 254 | /// Parse a prefix (normally a string literal) before this parser. |
237 | 255 | /// |
238 | 256 | /// The result of the prefix parser is discarded. |
|
0 commit comments