Skip to content

Commit

Permalink
Merge pull request #722 from epage/unit
Browse files Browse the repository at this point in the history
fix(error): Deprcate ErrorKind
  • Loading branch information
epage authored Jan 29, 2025
2 parents 6385ba4 + 21a314f commit 8718a90
Show file tree
Hide file tree
Showing 31 changed files with 608 additions and 513 deletions.
3 changes: 1 addition & 2 deletions benches/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use criterion::Criterion;

use winnow::ascii::float;
use winnow::binary::be_u64;
use winnow::error::ErrorKind;
use winnow::error::InputError;
use winnow::error::ParserError;
use winnow::prelude::*;
Expand Down Expand Up @@ -51,7 +50,7 @@ fn float_str(c: &mut Criterion) {
fn std_float(input: &mut &[u8]) -> ModalResult<f64> {
match input.parse_slice() {
Some(n) => Ok(n),
None => Err(ParserError::from_error_kind(input, ErrorKind::Slice)),
None => Err(ParserError::from_input(input)),
}
}

Expand Down
13 changes: 7 additions & 6 deletions examples/custom_error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use winnow::error::AddContext;
use winnow::error::ErrMode;
#[allow(deprecated)]
use winnow::error::ErrorKind;
use winnow::error::FromExternalError;
use winnow::error::ParserError;
Expand All @@ -9,19 +10,19 @@ use winnow::stream::Stream;
#[derive(Debug)]
pub enum CustomError<I> {
MyError,
Winnow(I, ErrorKind),
Winnow(I),
External {
cause: Box<dyn std::error::Error + Send + Sync + 'static>,
input: I,
kind: ErrorKind,
},
}

impl<I: Stream + Clone> ParserError<I> for CustomError<I> {
type Inner = Self;

fn from_error_kind(input: &I, kind: ErrorKind) -> Self {
CustomError::Winnow(input.clone(), kind)
#[allow(deprecated)]
fn from_error_kind(input: &I, _: ErrorKind) -> Self {
CustomError::Winnow(input.clone())
}

fn into_inner(self) -> Result<Self::Inner, Self> {
Expand All @@ -45,11 +46,11 @@ impl<I: Stream + Clone, E: std::error::Error + Send + Sync + 'static> FromExtern
for CustomError<I>
{
#[inline]
fn from_external_error(input: &I, kind: ErrorKind, e: E) -> Self {
#[allow(deprecated)]
fn from_external_error(input: &I, _: ErrorKind, e: E) -> Self {
CustomError::External {
cause: Box::new(e),
input: input.clone(),
kind,
}
}
}
Expand Down
20 changes: 12 additions & 8 deletions examples/json/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,19 @@ fn json_bench(c: &mut criterion::Criterion) {
b.iter(|| parser_dispatch::json::<Error>.parse_peek(sample).unwrap());
},
);
group.bench_with_input(criterion::BenchmarkId::new("unit", name), &len, |b, _| {
type Error<'i> = ();
group.bench_with_input(
criterion::BenchmarkId::new("empty-error", name),
&len,
|b, _| {
type Error<'i> = winnow::error::EmptyError;

b.iter(|| {
parser_dispatch::json::<Error<'_>>
.parse_peek(sample)
.unwrap()
});
});
b.iter(|| {
parser_dispatch::json::<Error<'_>>
.parse_peek(sample)
.unwrap()
});
},
);
group.bench_with_input(criterion::BenchmarkId::new("alt", name), &len, |b, _| {
type Error = winnow::error::ContextError;

Expand Down
6 changes: 3 additions & 3 deletions examples/json/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod parser_dispatch;
#[allow(dead_code)]
mod parser_partial;

use winnow::error::ErrorKind;
use winnow::error::EmptyError;
use winnow::prelude::*;

fn main() -> Result<(), lexopt::Error> {
Expand All @@ -25,8 +25,8 @@ fn main() -> Result<(), lexopt::Error> {
});

let result = match args.implementation {
Impl::Naive => parser_alt::json::<ErrorKind>.parse(data),
Impl::Dispatch => parser_dispatch::json::<ErrorKind>.parse(data),
Impl::Naive => parser_alt::json::<EmptyError>.parse(data),
Impl::Dispatch => parser_dispatch::json::<EmptyError>.parse(data),
};
match result {
Ok(json) => {
Expand Down
5 changes: 1 addition & 4 deletions fuzz/fuzz_targets/fuzz_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ fn incr(i: &mut &str) -> ModalResult<()> {

// limit the number of recursions, the fuzzer keeps running into them
if *l.borrow() >= 8192 {
Err(winnow::error::ParserError::from_error_kind(
i,
winnow::error::ErrorKind::Repeat,
))
Err(winnow::error::ParserError::from_input(i))
} else {
Ok(())
}
Expand Down
13 changes: 5 additions & 8 deletions src/_tutorial/chapter_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
//! # use winnow::Result;
//! use winnow::stream::Stream;
//! use winnow::error::ParserError;
//! use winnow::error::ErrorKind;
//!
//! fn parse_prefix(input: &mut &str) -> Result<char> {
//! let c = input.next_token().ok_or_else(|| {
//! ParserError::from_error_kind(input, ErrorKind::Token)
//! ParserError::from_input(input)
//! })?;
//! if c != '0' {
//! return Err(ParserError::from_error_kind(input, ErrorKind::Verify));
//! return Err(ParserError::from_input(input));
//! }
//! Ok(c)
//! }
Expand All @@ -39,15 +38,14 @@
//! ```rust
//! # use winnow::Result;
//! # use winnow::error::ParserError;
//! # use winnow::error::ErrorKind;
//! use winnow::Parser;
//! use winnow::token::any;
//!
//! fn parse_prefix(input: &mut &str) -> Result<char> {
//! let c = any
//! .parse_next(input)?;
//! if c != '0' {
//! return Err(ParserError::from_error_kind(input, ErrorKind::Verify));
//! return Err(ParserError::from_input(input));
//! }
//! Ok(c)
//! }
Expand Down Expand Up @@ -121,16 +119,15 @@
//! # use winnow::Result;
//! use winnow::stream::Stream;
//! use winnow::error::ParserError;
//! use winnow::error::ErrorKind;
//!
//! fn parse_prefix<'s>(input: &mut &'s str) -> Result<&'s str> {
//! let expected = "0x";
//! if input.len() < expected.len() {
//! return Err(ParserError::from_error_kind(input, ErrorKind::Slice));
//! return Err(ParserError::from_input(input));
//! }
//! let actual = input.next_slice(expected.len());
//! if actual != expected {
//! return Err(ParserError::from_error_kind(input, ErrorKind::Verify));
//! return Err(ParserError::from_input(input));
//! }
//! Ok(actual)
//! }
Expand Down
5 changes: 2 additions & 3 deletions src/_tutorial/chapter_7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,9 @@
//!
//! To make [`ErrMode`] more convenient, Winnow provides [`ModalResult`]:
//! ```rust
//! # use winnow::error::ErrorKind;
//! # use winnow::error::ContextError;
//! # use winnow::error::ErrMode;
//! pub type ModalResult<O, E = ErrorKind> = Result<O, ErrMode<E>>;
//! pub type ModalResult<O, E = ContextError> = Result<O, ErrMode<E>>;
//! ```
//!
//! So we can get the correct `context` by changing to [`ModalResult`] and adding [`cut_err`]:
Expand Down Expand Up @@ -722,7 +722,6 @@ use crate::combinator::fail;
use crate::error::ContextError;
use crate::error::ErrMode;
use crate::error::ErrMode::*;
use crate::error::ErrorKind;
use crate::ModalResult;
use crate::Parser;
use crate::Result;
Expand Down
Loading

0 comments on commit 8718a90

Please sign in to comment.