Skip to content

Commit

Permalink
Merge pull request #723 from epage/cherry
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 08f08cc + 64dc753 commit 6b80d18
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 18 deletions.
26 changes: 15 additions & 11 deletions examples/json/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use winnow::prelude::*;
use winnow::Partial;

mod json;
mod parser;
mod parser_alt;
mod parser_dispatch;
mod parser_partial;

Expand All @@ -13,29 +13,33 @@ fn json_bench(c: &mut criterion::Criterion) {
let len = sample.len();
group.throughput(criterion::Throughput::Bytes(len as u64));

group.bench_with_input(criterion::BenchmarkId::new("unit", name), &len, |b, _| {
type Error<'i> = ();

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

b.iter(|| parser::json::<Error>.parse_peek(sample).unwrap());
b.iter(|| parser_dispatch::json::<Error>.parse_peek(sample).unwrap());
},
);
group.bench_with_input(
criterion::BenchmarkId::new("dispatch", name),
criterion::BenchmarkId::new("empty-error", name),
&len,
|b, _| {
type Error = winnow::error::ContextError;
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;

b.iter(|| parser_alt::json::<Error>.parse_peek(sample).unwrap());
});
group.bench_with_input(
criterion::BenchmarkId::new("streaming", name),
&len,
Expand Down
4 changes: 2 additions & 2 deletions examples/json/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod json;
mod parser;
mod parser_alt;
mod parser_dispatch;
#[allow(dead_code)]
mod parser_partial;
Expand All @@ -25,7 +25,7 @@ fn main() -> Result<(), lexopt::Error> {
});

let result = match args.implementation {
Impl::Naive => parser::json::<ErrorKind>.parse(data),
Impl::Naive => parser_alt::json::<ErrorKind>.parse(data),
Impl::Dispatch => parser_dispatch::json::<ErrorKind>.parse(data),
};
match result {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/_topic/json.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! # json
//!
//! ```rust,ignore
#![doc = include_str!("../../examples/json/parser.rs")]
#![doc = include_str!("../../examples/json/parser_dispatch.rs")]
//! ```
87 changes: 83 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
//! that can help convert to a `Result` for integrating with your application's error reporting.
//!
//! Error types include:
//! - `()`
//! - [`EmptyError`] when the reason for failure doesn't matter
//! - [`ErrorKind`]
//! - [`InputError`] (mostly for testing)
//! - [`ContextError`]
//! - [`InputError`] (mostly for testing)
//! - [`TreeError`] (mostly for testing)
//! - [Custom errors][crate::_topic::error]
Expand Down Expand Up @@ -265,9 +265,16 @@ where
/// It provides methods to create an error from some combinators,
/// and combine existing errors in combinators like `alt`.
pub trait ParserError<I: Stream>: Sized {
/// Creates an error from the input position and an [`ErrorKind`]
/// Deprecated, replaced with [`ParserError::from_input`]
#[deprecated(since = "0.6.26", note = "replaced with `ParserError::from_input`")]
fn from_error_kind(input: &I, kind: ErrorKind) -> Self;

/// Creates an error from the input position
#[inline(always)]
fn from_input(input: &I) -> Self {
Self::from_error_kind(input, ErrorKind::Fail)
}

/// Process a parser assertion
#[inline(always)]
fn assert(input: &I, _message: &'static str) -> Self
Expand Down Expand Up @@ -364,10 +371,20 @@ pub struct InputError<I: Clone> {
impl<I: Clone> InputError<I> {
/// Creates a new basic error
#[inline]
#[deprecated(since = "0.6.26", note = "replaced with `InputError::at`")]
pub fn new(input: I, kind: ErrorKind) -> Self {
Self { input, kind }
}

/// Creates a new basic error
#[inline]
pub fn at(input: I) -> Self {
Self {
input,
kind: ErrorKind::Fail,
}
}

/// Translate the input type
#[inline]
pub fn map_input<I2: Clone, O: Fn(I) -> I2>(self, op: O) -> InputError<I2> {
Expand Down Expand Up @@ -474,6 +491,63 @@ impl<I: Clone + fmt::Debug + fmt::Display + Sync + Send + 'static> std::error::E
{
}

/// Track an error occurred without any other [`StrContext`]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct EmptyError;

impl<I: Stream> ParserError<I> for EmptyError {
#[inline(always)]
fn from_error_kind(_: &I, _: ErrorKind) -> Self {
Self
}

#[inline]
fn append(
self,
_input: &I,
_token_start: &<I as Stream>::Checkpoint,
_kind: ErrorKind,
) -> Self {
Self
}
}

impl<I: Stream, C> AddContext<I, C> for EmptyError {}

#[cfg(feature = "unstable-recover")]
#[cfg(feature = "std")]
impl<I: Stream> FromRecoverableError<I, Self> for EmptyError {
#[inline(always)]
fn from_recoverable_error(
_token_start: &<I as Stream>::Checkpoint,
_err_start: &<I as Stream>::Checkpoint,
_input: &I,
e: Self,
) -> Self {
e
}
}

impl<I, E> FromExternalError<I, E> for EmptyError {
#[inline(always)]
fn from_external_error(_input: &I, _kind: ErrorKind, _e: E) -> Self {
Self
}
}

impl ErrorConvert<EmptyError> for EmptyError {
#[inline(always)]
fn convert(self) -> EmptyError {
self
}
}

impl crate::lib::std::fmt::Display for EmptyError {
fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result {
"failed to parse".fmt(f)
}
}

impl<I: Stream> ParserError<I> for () {
#[inline]
fn from_error_kind(_: &I, _: ErrorKind) -> Self {}
Expand Down Expand Up @@ -1081,10 +1155,15 @@ impl<I: Stream + Clone + fmt::Display, C: fmt::Display> fmt::Display for TreeErr
}
}

/// Provide some minor debug context for errors
/// Deprecated
///
/// For error typse, use [`EmptyError`] instead
///
/// For creating an error, use [`ParserError::from_input`], [`InputError::at`]
#[rustfmt::skip]
#[derive(Debug,PartialEq,Eq,Hash,Clone,Copy)]
#[allow(missing_docs)]
#[deprecated(since = "0.6.26")]
pub enum ErrorKind {
Assert,
Token,
Expand Down

0 comments on commit 6b80d18

Please sign in to comment.