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
6 combinator::delimited,6 combinator::delimited,
7 combinator::fold_repeat,7 combinator::fold_repeat,
8 combinator::opt,8 combinator::opt,
9 error::{ErrorKind, InputError as NomError},9 error::{ErrorKind, InputError as NomError, ParserError as _},
10 prelude::*,10 prelude::*,
11 stream::AsChar,11 stream::AsChar,
12 token::{one_of, take_till0, take_while},12 token::{one_of, take_till0, take_while},
@@ -161,10 +161,7 @@ fn section_header(i: &[u8]) -> IResult<&[u8], section::Header<'_>> { · 148 unmodified lines
161 };161 };
162 162
163 if header.name.is_empty() {163 if header.name.is_empty() {
164 return Err(winnow::error::ErrMode::Backtrack(NomError {164 return Err(winnow::error::ErrMode::from_error_kind(i, ErrorKind::Fail));
165 input: i,
166 kind: ErrorKind::Fail,
167 }));
168 }165 }
169 return Ok((i, header));166 return Ok((i, header));
170 }167 }
@@ -202,39 +199,27 @@ fn sub_section_delegate<'a>(i: &'a [u8], push_byte: &mut dyn FnMut(u8)) -> IResu · 31 unmodified lines
202 while let Some(mut b) = bytes.next() {199 while let Some(mut b) = bytes.next() {
203 cursor += 1;200 cursor += 1;
204 if b == b'\n' || b == 0 {201 if b == b'\n' || b == 0 {
205 return Err(winnow::error::ErrMode::Backtrack(NomError {202 return Err(winnow::error::ErrMode::from_error_kind(&i[cursor..], ErrorKind::Fail));
206 input: &i[cursor..],
207 kind: ErrorKind::Fail,
208 }));
209 }203 }
210 if b == b'"' {204 if b == b'"' {
211 found_terminator = true;205 found_terminator = true;
212 break;206 break;
213 }207 }
214 if b == b'\\' {208 if b == b'\\' {
215 b = bytes.next().ok_or_else(|| {209 b = bytes
216 winnow::error::ErrMode::Backtrack(NomError {210 .next()
217 input: &i[cursor..],211 .ok_or_else(|| winnow::error::ErrMode::from_error_kind(&i[cursor..], ErrorKind::Fail))?;
218 kind: ErrorKind::Fail,
219 })
220 })?;
221 found_escape = true;212 found_escape = true;
222 cursor += 1;213 cursor += 1;
223 if b == b'\n' {214 if b == b'\n' {
224 return Err(winnow::error::ErrMode::Backtrack(NomError {215 return Err(winnow::error::ErrMode::from_error_kind(&i[cursor..], ErrorKind::Fail));
225 input: &i[cursor..],
226 kind: ErrorKind::Fail,
227 }));
228 }216 }
229 }217 }
230 push_byte(b);218 push_byte(b);
231 }219 }
232 220
233 if !found_terminator {221 if !found_terminator {
234 return Err(winnow::error::ErrMode::Backtrack(NomError {222 return Err(winnow::error::ErrMode::from_error_kind(&i[cursor..], ErrorKind::Fail));
235 input: &i[cursor..],
236 kind: ErrorKind::Fail,
237 }));
238 }223 }
239 224
240 Ok((&i[cursor - 1..], (found_escape, cursor - 1)))225 Ok((&i[cursor - 1..], (found_escape, cursor - 1)))
@@ -264,17 +249,11 @@ fn key_value_pair<'a>( · 23 unmodified lines
264/// trimmed of any leading whitespace.249/// trimmed of any leading whitespace.
265fn config_name(i: &[u8]) -> IResult<&[u8], &BStr> {250fn config_name(i: &[u8]) -> IResult<&[u8], &BStr> {
266 if i.is_empty() {251 if i.is_empty() {
267 return Err(winnow::error::ErrMode::Backtrack(NomError {252 return Err(winnow::error::ErrMode::from_error_kind(i, ErrorKind::Fail));
268 input: i,
269 kind: ErrorKind::Fail,
270 }));
271 }253 }
272 254
273 if !i[0].is_ascii_alphabetic() {255 if !i[0].is_ascii_alphabetic() {
274 return Err(winnow::error::ErrMode::Backtrack(NomError {256 return Err(winnow::error::ErrMode::from_error_kind(i, ErrorKind::Token));
275 input: i,
276 kind: ErrorKind::Token,
277 }));
278 }257 }
279 258
280 let (i, name) = take_while(0.., |c: u8| c.is_ascii_alphanumeric() || c == b'-').parse_next(i)?;259 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
303/// line values as well as values that are continuations.282/// line values as well as values that are continuations.
304fn value_impl<'a>(i: &'a [u8], dispatch: &mut impl FnMut(Event<'a>)) -> IResult<&'a [u8], usize> {283fn value_impl<'a>(i: &'a [u8], dispatch: &mut impl FnMut(Event<'a>)) -> IResult<&'a [u8], usize> {
305 let (i, value_end, newlines, mut dispatch) = {284 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);
307 let mut value_end = None::<usize>;286 let mut value_end = None::<usize>;
308 let mut value_start: usize = 0;287 let mut value_start: usize = 0;
309 let mut newlines = 0;288 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
419fn take_spaces(i: &[u8]) -> IResult<&[u8], &BStr> {398fn take_spaces(i: &[u8]) -> IResult<&[u8], &BStr> {
420 let (i, v) = take_while(0.., |c: u8| c.is_ascii() && c.is_space()).parse_next(i)?;399 let (i, v) = take_while(0.., |c: u8| c.is_ascii() && c.is_space()).parse_next(i)?;
421 if v.is_empty() {400 if v.is_empty() {
422 Err(winnow::error::ErrMode::Backtrack(NomError {401 Err(winnow::error::ErrMode::from_error_kind(i, ErrorKind::Eof))
423 input: i,
424 kind: ErrorKind::Eof,
425 }))
426 } else {402 } else {
427 Ok((i, v.as_bstr()))403 Ok((i, v.as_bstr()))
428 }404 }
@@ -453,10 +429,7 @@ fn take_newlines(i: &[u8]) -> IResult<&[u8], (&BStr, usize)> { · 24 unmodified lines
453 }429 }
454 let (v, i) = i.split_at(consumed_bytes);430 let (v, i) = i.split_at(consumed_bytes);
455 if v.is_empty() {431 if v.is_empty() {
456 Err(winnow::error::ErrMode::Backtrack(NomError {432 Err(winnow::error::ErrMode::from_error_kind(i, ErrorKind::Eof))
457 input: i,
458 kind: ErrorKind::Eof,
459 }))
460 } else {433 } else {
461 Ok((i, (v.as_bstr(), counter)))434 Ok((i, (v.as_bstr(), counter)))
462 }435 }