History

Simplify error creation

Ed Page committed 3 years ago

Commit bdcd3791 parent 42ae7661 file · +13 −40Browse files

Unified · Split

gix-config/src/parse/nom/mod.rs+13−40
@@ -6,7 +6,7 @@ use winnow::{ · 5 unmodified lines
66 combinator::delimited,
77 combinator::fold_repeat,
88 combinator::opt,
9− error::{ErrorKind, InputError as NomError},
9+ error::{ErrorKind, InputError as NomError, ParserError as _},
1010 prelude::*,
1111 stream::AsChar,
1212 token::{one_of, take_till0, take_while},
@@ -161,10 +161,7 @@ fn section_header(i: &[u8]) -> IResult<&[u8], section::Header<'_>> { · 148 unmodified lines
161161 };
162162
163163 if header.name.is_empty() {
164− return Err(winnow::error::ErrMode::Backtrack(NomError {
165− input: i,
166− kind: ErrorKind::Fail,
167− }));
164+ return Err(winnow::error::ErrMode::from_error_kind(i, ErrorKind::Fail));
168165 }
169166 return Ok((i, header));
170167 }
@@ -202,39 +199,27 @@ fn sub_section_delegate<'a>(i: &'a [u8], push_byte: &mut dyn FnMut(u8)) -> IResu · 31 unmodified lines
202199 while let Some(mut b) = bytes.next() {
203200 cursor += 1;
204201 if b == b'\n' || b == 0 {
205− return Err(winnow::error::ErrMode::Backtrack(NomError {
206− input: &i[cursor..],
207− kind: ErrorKind::Fail,
208− }));
202+ return Err(winnow::error::ErrMode::from_error_kind(&i[cursor..], ErrorKind::Fail));
209203 }
210204 if b == b'"' {
211205 found_terminator = true;
212206 break;
213207 }
214208 if b == b'\\' {
215− b = bytes.next().ok_or_else(|| {
216− winnow::error::ErrMode::Backtrack(NomError {
217− input: &i[cursor..],
218− kind: ErrorKind::Fail,
219− })
220− })?;
209+ b = bytes
210+ .next()
211+ .ok_or_else(|| winnow::error::ErrMode::from_error_kind(&i[cursor..], ErrorKind::Fail))?;
221212 found_escape = true;
222213 cursor += 1;
223214 if b == b'\n' {
224− return Err(winnow::error::ErrMode::Backtrack(NomError {
225− input: &i[cursor..],
226− kind: ErrorKind::Fail,
227− }));
215+ return Err(winnow::error::ErrMode::from_error_kind(&i[cursor..], ErrorKind::Fail));
228216 }
229217 }
230218 push_byte(b);
231219 }
232220
233221 if !found_terminator {
234− return Err(winnow::error::ErrMode::Backtrack(NomError {
235− input: &i[cursor..],
236− kind: ErrorKind::Fail,
237− }));
222+ return Err(winnow::error::ErrMode::from_error_kind(&i[cursor..], ErrorKind::Fail));
238223 }
239224
240225 Ok((&i[cursor - 1..], (found_escape, cursor - 1)))
@@ -264,17 +249,11 @@ fn key_value_pair<'a>( · 23 unmodified lines
264249 /// trimmed of any leading whitespace.
265250 fn config_name(i: &[u8]) -> IResult<&[u8], &BStr> {
266251 if i.is_empty() {
267− return Err(winnow::error::ErrMode::Backtrack(NomError {
268− input: i,
269− kind: ErrorKind::Fail,
270− }));
252+ return Err(winnow::error::ErrMode::from_error_kind(i, ErrorKind::Fail));
271253 }
272254
273255 if !i[0].is_ascii_alphabetic() {
274− return Err(winnow::error::ErrMode::Backtrack(NomError {
275− input: i,
276− kind: ErrorKind::Token,
277− }));
256+ return Err(winnow::error::ErrMode::from_error_kind(i, ErrorKind::Token));
278257 }
279258
280259 let (i, name) = take_while(0.., |c: u8| c.is_ascii_alphanumeric() || c == b'-').parse_next(i)?;
@@ -303,7 +282,7 @@ fn config_value<'a>(i: &'a [u8], dispatch: &mut impl FnMut(Event<'a>)) -> IResul · 22 unmodified lines
303282 /// line values as well as values that are continuations.
304283 fn value_impl<'a>(i: &'a [u8], dispatch: &mut impl FnMut(Event<'a>)) -> IResult<&'a [u8], usize> {
305284 let (i, value_end, newlines, mut dispatch) = {
306− let new_err = |kind| winnow::error::ErrMode::Backtrack(NomError { input: i, kind });
285+ let new_err = |kind| winnow::error::ErrMode::from_error_kind(i, kind);
307286 let mut value_end = None::<usize>;
308287 let mut value_start: usize = 0;
309288 let mut newlines = 0;
@@ -419,10 +398,7 @@ fn value_impl<'a>(i: &'a [u8], dispatch: &mut impl FnMut(Event<'a>)) -> IResult< · 109 unmodified lines
419398 fn take_spaces(i: &[u8]) -> IResult<&[u8], &BStr> {
420399 let (i, v) = take_while(0.., |c: u8| c.is_ascii() && c.is_space()).parse_next(i)?;
421400 if v.is_empty() {
422− Err(winnow::error::ErrMode::Backtrack(NomError {
423− input: i,
424− kind: ErrorKind::Eof,
425− }))
401+ Err(winnow::error::ErrMode::from_error_kind(i, ErrorKind::Eof))
426402 } else {
427403 Ok((i, v.as_bstr()))
428404 }
@@ -453,10 +429,7 @@ fn take_newlines(i: &[u8]) -> IResult<&[u8], (&BStr, usize)> { · 24 unmodified lines
453429 }
454430 let (v, i) = i.split_at(consumed_bytes);
455431 if v.is_empty() {
456− Err(winnow::error::ErrMode::Backtrack(NomError {
457− input: i,
458− kind: ErrorKind::Eof,
459− }))
432+ Err(winnow::error::ErrMode::from_error_kind(i, ErrorKind::Eof))
460433 } else {
461434 Ok((i, (v.as_bstr(), counter)))
462435 }