Skip to content

Commit

Permalink
chore(deps): bump winnow 0.7 (#862)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Jan 31, 2025
1 parent 3f28b07 commit 053918b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ rand = { version = "0.8", default-features = false }
rayon = { version = "1.2", default-features = false }
ruint = { version = "1.12.3", default-features = false, features = ["alloc"] }
ruint-macro = { version = "1", default-features = false }
winnow = { version = "0.6", default-features = false, features = ["alloc"] }
winnow = { version = "0.7", default-features = false, features = ["alloc"] }
73 changes: 24 additions & 49 deletions crates/dyn-abi/src/coerce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use alloc::vec::Vec;
use alloy_primitives::{Address, Function, Sign, I256, U256};
use alloy_sol_types::Word;
use core::fmt;
use hex::FromHexError;
use parser::{
new_input,
utils::{array_parser, char_parser, spanned},
Expand All @@ -13,12 +12,12 @@ use winnow::{
ascii::{alpha0, alpha1, digit1, hex_digit0, hex_digit1, space0},
combinator::{cut_err, dispatch, empty, fail, opt, preceded, trace},
error::{
AddContext, ContextError, ErrMode, ErrorKind, FromExternalError, ParserError, StrContext,
AddContext, ContextError, ErrMode, FromExternalError, ParserError, StrContext,
StrContextValue,
},
stream::Stream,
token::take_while,
ModalResult, Parser,
ModalParser, ModalResult, Parser,
};

impl DynSolType {
Expand Down Expand Up @@ -89,7 +88,7 @@ struct ValueParser<'a> {
list_end: Option<char>,
}

impl<'i> Parser<Input<'i>, DynSolValue, ContextError> for ValueParser<'_> {
impl<'i> Parser<Input<'i>, DynSolValue, ErrMode<ContextError>> for ValueParser<'_> {
fn parse_next(&mut self, input: &mut Input<'i>) -> ModalResult<DynSolValue, ContextError> {
#[cfg(feature = "debug")]
let name = self.ty.sol_type_name();
Expand Down Expand Up @@ -146,7 +145,7 @@ impl<'a> ValueParser<'a> {
}

#[inline]
fn string<'s, 'i: 's>(&'s self) -> impl Parser<Input<'i>, &'i str, ContextError> + 's {
fn string<'s, 'i: 's>(&'s self) -> impl ModalParser<Input<'i>, &'i str, ContextError> + 's {
trace("string", |input: &mut Input<'i>| {
let Some(delim) = input.chars().next() else {
return Ok("");
Expand Down Expand Up @@ -185,7 +184,7 @@ impl<'a> ValueParser<'a> {
}

#[inline]
fn array<'i: 'a>(self) -> impl Parser<Input<'i>, Vec<DynSolValue>, ContextError> + 'a {
fn array<'i: 'a>(self) -> impl ModalParser<Input<'i>, Vec<DynSolValue>, ContextError> + 'a {
#[cfg(feature = "debug")]
let name = format!("{}[]", self.ty);
#[cfg(not(feature = "debug"))]
Expand All @@ -197,7 +196,7 @@ impl<'a> ValueParser<'a> {
fn fixed_array<'i: 'a>(
self,
len: usize,
) -> impl Parser<Input<'i>, Vec<DynSolValue>, ContextError> + 'a {
) -> impl ModalParser<Input<'i>, Vec<DynSolValue>, ContextError> + 'a {
#[cfg(feature = "debug")]
let name = format!("{}[{len}]", self.ty);
#[cfg(not(feature = "debug"))]
Expand All @@ -219,7 +218,7 @@ impl<'a> ValueParser<'a> {
fn tuple<'i: 's, 't: 's, 's>(
&'s self,
tuple: &'t Vec<DynSolType>,
) -> impl Parser<Input<'i>, Vec<DynSolValue>, ContextError> + 's {
) -> impl ModalParser<Input<'i>, Vec<DynSolValue>, ContextError> + 's {
#[cfg(feature = "debug")]
let name = DynSolType::Tuple(tuple.clone()).to_string();
#[cfg(not(feature = "debug"))]
Expand Down Expand Up @@ -296,7 +295,7 @@ fn bool(input: &mut Input<'_>) -> ModalResult<bool> {
}

#[inline]
fn int<'i>(size: usize) -> impl Parser<Input<'i>, I256, ContextError> {
fn int<'i>(size: usize) -> impl ModalParser<Input<'i>, I256, ContextError> {
#[cfg(feature = "debug")]
let name = format!("int{size}");
#[cfg(not(feature = "debug"))]
Expand Down Expand Up @@ -329,7 +328,7 @@ fn int_sign(input: &mut Input<'_>) -> ModalResult<Sign> {
}

#[inline]
fn uint<'i>(len: usize) -> impl Parser<Input<'i>, U256, ContextError> {
fn uint<'i>(len: usize) -> impl ModalParser<Input<'i>, U256, ContextError> {
#[cfg(feature = "debug")]
let name = format!("uint{len}");
#[cfg(not(feature = "debug"))]
Expand All @@ -345,40 +344,33 @@ fn uint<'i>(len: usize) -> impl Parser<Input<'i>, U256, ContextError> {
))
.parse_next(input)?;

let intpart = intpart
.parse::<U256>()
.map_err(|e| ErrMode::from_external_error(input, ErrorKind::Verify, e))?;
let intpart =
intpart.parse::<U256>().map_err(|e| ErrMode::from_external_error(input, e))?;
let e = opt(scientific_notation).parse_next(input)?.unwrap_or(0);

let _ = space0(input)?;
let units = int_units(input)?;

let units = units as isize + e;
if units < 0 {
return Err(ErrMode::from_external_error(
input,
ErrorKind::Verify,
Error::NegativeUnits,
));
return Err(ErrMode::from_external_error(input, Error::NegativeUnits));
}
let units = units as usize;

let uint = if let Some(fract) = fract {
let fract_uint = U256::from_str_radix(fract, 10)
.map_err(|e| ErrMode::from_external_error(input, ErrorKind::Verify, e))?;
.map_err(|e| ErrMode::from_external_error(input, e))?;

if units == 0 && !fract_uint.is_zero() {
return Err(ErrMode::from_external_error(
input,
ErrorKind::Verify,
Error::FractionalNotAllowed(fract_uint),
));
}

if fract.len() > units {
return Err(ErrMode::from_external_error(
input,
ErrorKind::Verify,
Error::TooManyDecimals(units, fract.len()),
));
}
Expand All @@ -400,12 +392,10 @@ fn uint<'i>(len: usize) -> impl Parser<Input<'i>, U256, ContextError> {
} else {
Some(intpart)
}
.ok_or_else(|| {
ErrMode::from_external_error(input, ErrorKind::Verify, Error::IntOverflow)
})?;
.ok_or_else(|| ErrMode::from_external_error(input, Error::IntOverflow))?;

if uint.bit_len() > len {
return Err(ErrMode::from_external_error(input, ErrorKind::Verify, Error::IntOverflow));
return Err(ErrMode::from_external_error(input, Error::IntOverflow));
}

Ok(uint)
Expand All @@ -427,7 +417,7 @@ fn prefixed_int<'i>(input: &mut Input<'i>) -> ModalResult<&'i str> {
} else {
digit1(input)
}
.map_err(|e| {
.map_err(|e: ErrMode<_>| {
e.add_context(
input,
&checkpoint,
Expand Down Expand Up @@ -458,33 +448,30 @@ fn int_units(input: &mut Input<'_>) -> ModalResult<usize> {
fn scientific_notation(input: &mut Input<'_>) -> ModalResult<isize> {
// Check if we have 'e' or 'E' followed by an optional sign and digits
if !matches!(input.chars().next(), Some('e' | 'E')) {
return Err(ErrMode::from_error_kind(input, ErrorKind::Fail));
};
return Err(ErrMode::from_input(input));
}
let _ = input.next_token();
winnow::ascii::dec_int(input)
}

#[inline]
fn fixed_bytes<'i>(len: usize) -> impl Parser<Input<'i>, Word, ContextError> {
fn fixed_bytes<'i>(len: usize) -> impl ModalParser<Input<'i>, Word, ContextError> {
#[cfg(feature = "debug")]
let name = format!("bytes{len}");
#[cfg(not(feature = "debug"))]
let name = "bytesN";
trace(name, move |input: &mut Input<'_>| {
if len > Word::len_bytes() {
return Err(ErrMode::from_external_error(
input,
ErrorKind::Fail,
Error::InvalidFixedBytesLength(len),
)
.cut());
return Err(
ErrMode::from_external_error(input, Error::InvalidFixedBytesLength(len)).cut()
);
}

let hex = hex_str(input)?;
let mut out = Word::ZERO;
match hex::decode_to_slice(hex, &mut out[..len]) {
Ok(()) => Ok(out),
Err(e) => Err(hex_error(input, e).cut()),
Err(e) => Err(ErrMode::from_external_error(input, e).cut()),
}
})
}
Expand All @@ -511,25 +498,13 @@ fn hex_str<'i>(input: &mut Input<'i>) -> ModalResult<&'i str> {
let has_prefix = opt("0x").parse_next(input)?.is_some();
let s = hex_digit0(input)?;
if !has_prefix && s.is_empty() {
return Err(ErrMode::from_external_error(
input,
ErrorKind::Verify,
Error::EmptyHexStringWithoutPrefix,
));
return Err(ErrMode::from_external_error(input, Error::EmptyHexStringWithoutPrefix));
}
Ok(s)
})
.parse_next(input)
}

fn hex_error(input: &&str, e: FromHexError) -> ErrMode<ContextError> {
let kind = match e {
FromHexError::InvalidHexCharacter { .. } => unreachable!("{e:?}"),
FromHexError::InvalidStringLength | FromHexError::OddLength => ErrorKind::Eof,
};
ErrMode::from_external_error(input, kind, e)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 2 additions & 2 deletions crates/sol-type-parser/src/ident.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use winnow::{
error::{ErrMode, ErrorKind, ParserError},
error::{ErrMode, ParserError},
stream::{AsBStr, Stream},
ModalResult,
};
Expand Down Expand Up @@ -68,7 +68,7 @@ where
let mut chars = input.as_bstr().iter().map(|b| *b as char);

let Some(true) = chars.next().map(is_id_start) else {
return Err(ErrMode::from_error_kind(input, ErrorKind::Fail));
return Err(ErrMode::from_input(input));
};

// 1 for the first character, we know it's ASCII
Expand Down
12 changes: 4 additions & 8 deletions crates/sol-type-parser/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Recursion implementation modified from `toml`: https://github.com/toml-rs/toml/blob/a02cbf46cab4a8683e641efdba648a31498f7342/crates/toml_edit/src/parser/mod.rs#L99

use core::fmt;
use winnow::{error::ContextError, Parser};
use winnow::{error::ContextError, ModalParser};

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CustomError {
Expand All @@ -28,17 +28,13 @@ pub fn new_input(input: &str) -> Input<'_> {
}

pub fn check_recursion<'a, O>(
mut parser: impl Parser<Input<'a>, O, ContextError>,
) -> impl Parser<Input<'a>, O, ContextError> {
mut parser: impl ModalParser<Input<'a>, O, ContextError>,
) -> impl ModalParser<Input<'a>, O, ContextError> {
move |input: &mut Input<'a>| {
input.state.enter().map_err(|_err| {
// TODO: Very weird bug with features: https://github.com/alloy-rs/core/issues/717
// use winnow::error::FromExternalError;
// let err = winnow::error::ContextError::from_external_error(
// input,
// winnow::error::ErrorKind::Eof,
// _err,
// );
// let err = winnow::error::ContextError::from_external_error(input, _err);
let err = winnow::error::ContextError::new();
winnow::error::ErrMode::Cut(err)
})?;
Expand Down
4 changes: 2 additions & 2 deletions crates/sol-type-parser/src/type_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::num::NonZeroUsize;
use winnow::{
ascii::digit0,
combinator::{cut_err, delimited, repeat, trace},
error::{ErrMode, ErrorKind, FromExternalError},
error::{ErrMode, FromExternalError},
ModalResult, Parser,
};

Expand Down Expand Up @@ -144,7 +144,7 @@ fn array_size_parser(input: &mut Input<'_>) -> ModalResult<Option<NonZeroUsize>>
if digits.is_empty() {
return Ok(None);
}
digits.parse().map(Some).map_err(|e| ErrMode::from_external_error(input, ErrorKind::Verify, e))
digits.parse().map(Some).map_err(|e| ErrMode::from_external_error(input, e))
}

#[cfg(test)]
Expand Down
26 changes: 13 additions & 13 deletions crates/sol-type-parser/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ use core::{slice, str};
use winnow::{
ascii::space0,
combinator::{alt, cut_err, opt, preceded, separated, terminated, trace},
error::{ContextError, StrContext, StrContextValue},
error::{ContextError, ParserError, StrContext, StrContextValue},
stream::{Accumulate, AsChar, Stream},
ModalResult, Parser,
ModalParser, ModalResult, Parser,
};

pub use crate::ident::identifier;

#[inline]
pub fn spanned<'a, O, E>(
mut f: impl Parser<Input<'a>, O, E>,
) -> impl Parser<Input<'a>, (&'a str, O), E> {
pub fn spanned<'a, O, E: ParserError<Input<'a>>>(
mut f: impl ModalParser<Input<'a>, O, E>,
) -> impl ModalParser<Input<'a>, (&'a str, O), E> {
trace("spanned", move |input: &mut Input<'a>| {
let start = input.as_ptr();

Expand All @@ -37,7 +37,7 @@ pub fn spanned<'a, O, E>(
}

#[inline]
pub fn char_parser<'a>(c: char) -> impl Parser<Input<'a>, char, ContextError> {
pub fn char_parser<'a>(c: char) -> impl ModalParser<Input<'a>, char, ContextError> {
#[cfg(feature = "debug")]
let name = format!("char={c:?}");
#[cfg(not(feature = "debug"))]
Expand All @@ -46,7 +46,7 @@ pub fn char_parser<'a>(c: char) -> impl Parser<Input<'a>, char, ContextError> {
}

#[inline]
pub fn str_parser<'a>(s: &'static str) -> impl Parser<Input<'a>, &'a str, ContextError> {
pub fn str_parser<'a>(s: &'static str) -> impl ModalParser<Input<'a>, &'a str, ContextError> {
#[cfg(feature = "debug")]
let name = format!("str={s:?}");
#[cfg(not(feature = "debug"))]
Expand All @@ -55,14 +55,14 @@ pub fn str_parser<'a>(s: &'static str) -> impl Parser<Input<'a>, &'a str, Contex
}

pub fn tuple_parser<'a, O1, O2: Accumulate<O1>>(
f: impl Parser<Input<'a>, O1, ContextError>,
) -> impl Parser<Input<'a>, O2, ContextError> {
f: impl ModalParser<Input<'a>, O1, ContextError>,
) -> impl ModalParser<Input<'a>, O2, ContextError> {
list_parser('(', ',', ')', f)
}

pub fn array_parser<'a, O1, O2: Accumulate<O1>>(
f: impl Parser<Input<'a>, O1, ContextError>,
) -> impl Parser<Input<'a>, O2, ContextError> {
f: impl ModalParser<Input<'a>, O1, ContextError>,
) -> impl ModalParser<Input<'a>, O2, ContextError> {
list_parser('[', ',', ']', f)
}

Expand All @@ -71,8 +71,8 @@ fn list_parser<'i, O1, O2>(
open: char,
delim: char,
close: char,
f: impl Parser<Input<'i>, O1, ContextError>,
) -> impl Parser<Input<'i>, O2, ContextError>
f: impl ModalParser<Input<'i>, O1, ContextError>,
) -> impl ModalParser<Input<'i>, O2, ContextError>
where
O2: Accumulate<O1>,
{
Expand Down

0 comments on commit 053918b

Please sign in to comment.