Skip to content

Commit

Permalink
Merge pull request #92 from epage/macro
Browse files Browse the repository at this point in the history
fix(error)!: Polish the API
  • Loading branch information
epage authored Jan 27, 2023
2 parents ef9ef88 + 339e028 commit 6191b4b
Show file tree
Hide file tree
Showing 32 changed files with 1,321 additions and 1,139 deletions.
24 changes: 14 additions & 10 deletions benches/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate criterion;
use criterion::Criterion;

use winnow::character::{f64, recognize_float};
use winnow::error::Error;
use winnow::error::ErrorKind;
use winnow::input::ParseTo;
use winnow::number::be_u64;
Expand All @@ -28,49 +29,52 @@ fn number(c: &mut Criterion) {
fn recognize_float_bytes(c: &mut Criterion) {
println!(
"recognize_float_bytes result: {:?}",
recognize_float::<_, (_, ErrorKind), false>(&b"-1.234E-12"[..])
recognize_float::<_, Error<_>, false>(&b"-1.234E-12"[..])
);
c.bench_function("recognize float bytes", |b| {
b.iter(|| recognize_float::<_, (_, ErrorKind), false>(&b"-1.234E-12"[..]));
b.iter(|| recognize_float::<_, Error<_>, false>(&b"-1.234E-12"[..]));
});
}

fn recognize_float_str(c: &mut Criterion) {
println!(
"recognize_float_str result: {:?}",
recognize_float::<_, (_, ErrorKind), false>("-1.234E-12")
recognize_float::<_, Error<_>, false>("-1.234E-12")
);
c.bench_function("recognize float str", |b| {
b.iter(|| recognize_float::<_, (_, ErrorKind), false>("-1.234E-12"));
b.iter(|| recognize_float::<_, Error<_>, false>("-1.234E-12"));
});
}

fn float_bytes(c: &mut Criterion) {
println!(
"float_bytes result: {:?}",
f64::<_, (_, ErrorKind), false>(&b"-1.234E-12"[..])
f64::<_, Error<_>, false>(&b"-1.234E-12"[..])
);
c.bench_function("float bytes", |b| {
b.iter(|| f64::<_, (_, ErrorKind), false>(&b"-1.234E-12"[..]));
b.iter(|| f64::<_, Error<_>, false>(&b"-1.234E-12"[..]));
});
}

fn float_str(c: &mut Criterion) {
println!(
"float_str result: {:?}",
f64::<_, (_, ErrorKind), false>("-1.234E-12")
f64::<_, Error<_>, false>("-1.234E-12")
);
c.bench_function("float str", |b| {
b.iter(|| f64::<_, (_, ErrorKind), false>("-1.234E-12"));
b.iter(|| f64::<_, Error<_>, false>("-1.234E-12"));
});
}

fn std_float(input: &[u8]) -> IResult<&[u8], f64, (&[u8], ErrorKind)> {
fn std_float(input: &[u8]) -> IResult<&[u8], f64, Error<&[u8]>> {
match recognize_float(input) {
Err(e) => Err(e),
Ok((i, s)) => match s.parse_to() {
Some(n) => Ok((i, n)),
None => Err(Err::Error((i, ErrorKind::Float))),
None => Err(Err::Error(Error {
input: i,
kind: ErrorKind::Float,
})),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/css/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl std::str::FromStr for Color {
fn from_str(s: &str) -> Result<Self, Self::Err> {
hex_color(s).finish().map_err(|err| winnow::error::Error {
input: err.input.to_owned(),
code: err.code,
kind: err.kind,
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/custom_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ impl<I> ParseError<I> for CustomError<I> {
CustomError::Nom(input, kind)
}

fn append(_: I, _: ErrorKind, other: Self) -> Self {
other
fn append(self, _: I, _: ErrorKind) -> Self {
self
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/bits/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::{Err, IResult};
/// assert_eq!(parser(([0b00010010].as_ref(), 4), 4), Ok((([].as_ref(), 0), 0b00000010)));
///
/// // Tries to consume 12 bits but only 8 are available
/// assert_eq!(parser(([0b00010010].as_ref(), 0), 12), Err(winnow::Err::Error(Error{input: ([0b00010010].as_ref(), 0), code: ErrorKind::Eof })));
/// assert_eq!(parser(([0b00010010].as_ref(), 0), 12), Err(winnow::Err::Error(Error{input: ([0b00010010].as_ref(), 0), kind: ErrorKind::Eof })));
/// ```
///
/// **WARNING:** Deprecated, replaced with [`winnow::bits::take`][crate::bits::take]
Expand Down Expand Up @@ -183,7 +183,7 @@ mod test {
result,
Err(crate::Err::Error(crate::error::Error {
input: (input, 8),
code: ErrorKind::Eof
kind: ErrorKind::Eof
}))
);
}
Expand Down Expand Up @@ -219,7 +219,7 @@ mod test {
result,
Err(crate::Err::Error(crate::error::Error {
input: (input, 8),
code: ErrorKind::Eof
kind: ErrorKind::Eof
}))
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/bits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ where
/// assert_eq!(parser(([0b00010010].as_ref(), 4), 4), Ok((([].as_ref(), 0), 0b00000010)));
///
/// // Tries to consume 12 bits but only 8 are available
/// assert_eq!(parser(([0b00010010].as_ref(), 0), 12), Err(winnow::Err::Error(Error{input: ([0b00010010].as_ref(), 0), code: ErrorKind::Eof })));
/// assert_eq!(parser(([0b00010010].as_ref(), 0), 12), Err(winnow::Err::Error(Error{input: ([0b00010010].as_ref(), 0), kind: ErrorKind::Eof })));
/// ```
#[inline(always)]
pub fn take<I, O, C, E: ParseError<(I, usize)>, const STREAMING: bool>(
Expand Down Expand Up @@ -184,7 +184,7 @@ where
/// parser(0b000000_01, 2, ([0b111111_11].as_ref(), 0)),
/// Err(winnow::Err::Error(Error {
/// input: ([0b11111111].as_ref(), 0),
/// code: ErrorKind::TagBits
/// kind: ErrorKind::TagBits
/// }))
/// );
///
Expand All @@ -193,7 +193,7 @@ where
/// parser(0b11111110, 8, ([0b11111111].as_ref(), 0)),
/// Err(winnow::Err::Error(Error {
/// input: ([0b11111111].as_ref(), 0),
/// code: ErrorKind::TagBits
/// kind: ErrorKind::TagBits
/// }))
/// );
/// ```
Expand Down
2 changes: 1 addition & 1 deletion src/bits/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ mod test {
result,
Err(crate::Err::Error(crate::error::Error {
input: (input, offset),
code: ErrorKind::TagBits
kind: ErrorKind::TagBits
}))
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/bits/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn test_take_complete_eof() {
result,
Err(crate::Err::Error(crate::error::Error {
input: (input, 8),
code: ErrorKind::Eof
kind: ErrorKind::Eof
}))
);
}
Expand Down Expand Up @@ -136,7 +136,7 @@ fn test_tag_streaming_err() {
result,
Err(crate::Err::Error(crate::error::Error {
input: (input, offset),
code: ErrorKind::TagBits
kind: ErrorKind::TagBits
}))
);
}
Expand All @@ -160,7 +160,7 @@ fn test_bool_eof_complete() {
result,
Err(crate::Err::Error(crate::error::Error {
input: (input, 8),
code: ErrorKind::Eof
kind: ErrorKind::Eof
}))
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/branch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ macro_rules! alt_trait_inner(
}
);
($it:tt, $self:expr, $input:expr, $err:expr, $head:ident) => (
Err(Err::Error(Error::append($input, ErrorKind::Alt, $err)))
Err(Err::Error($err.append($input, ErrorKind::Alt)))
);
);

Expand Down Expand Up @@ -205,7 +205,7 @@ macro_rules! permutation_trait_impl(
// or errored on the remaining input
if let Some(err) = err {
// There are remaining parsers, and all errored on the remaining input
return Err(Err::Error(Error::append(input, ErrorKind::Permutation, err)));
return Err(Err::Error(err.append(input, ErrorKind::Permutation)));
}

// All parsers were applied
Expand Down
4 changes: 2 additions & 2 deletions src/branch/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ impl<I: Debug> ParseError<I> for ErrorStr {
ErrorStr(format!("custom error message: ({:?}, {:?})", input, kind))
}

fn append(input: I, kind: ErrorKind, other: Self) -> Self {
fn append(self, input: I, kind: ErrorKind) -> Self {
ErrorStr(format!(
"custom error message: ({:?}, {:?}) - {:?}",
input, kind, other
input, kind, self
))
}
}
Expand Down
Loading

0 comments on commit 6191b4b

Please sign in to comment.