Skip to content

Commit

Permalink
fix(sol-type-parser): winnow std error (#720)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Sep 3, 2024
1 parent ea7f594 commit 6bd4aed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
7 changes: 4 additions & 3 deletions crates/sol-type-parser/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::boxed::Box;
use alloc::{boxed::Box, string::String};
use core::fmt;

/// Parser result
Expand Down Expand Up @@ -59,12 +59,13 @@ impl Error {
#[inline(never)]
#[cold]
pub fn _new(s: &str, e: &dyn fmt::Display) -> Self {
Self(Repr(format!("{s}{e}").into_boxed_str()))
Self(Repr(Box::new(format!("{s}{e}"))))
}
}

#[derive(Clone, PartialEq, Eq)]
struct Repr(Box<str>);
#[allow(clippy::box_collection)] // `Box<String>` is smaller than `String` or `Box<str>`.
struct Repr(Box<String>);

impl fmt::Display for Repr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
21 changes: 12 additions & 9 deletions crates/sol-type-parser/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
// 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, FromExternalError},
Parser,
};
use winnow::{error::ContextError, Parser};

#[allow(dead_code)]
#[derive(Clone, Debug, PartialEq, Eq)]
enum CustomError {
pub enum CustomError {
RecursionLimitExceeded,
}

Expand All @@ -35,9 +31,16 @@ pub fn check_recursion<'a, O>(
mut parser: impl Parser<Input<'a>, O, ContextError>,
) -> impl Parser<Input<'a>, O, ContextError> {
move |input: &mut Input<'a>| {
input.state.enter().map_err(|err| {
winnow::error::ErrMode::from_external_error(input, winnow::error::ErrorKind::Eof, err)
.cut()
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::new();
winnow::error::ErrMode::Cut(err)
})?;
let result = parser.parse_next(input);
input.state.exit();
Expand Down

0 comments on commit 6bd4aed

Please sign in to comment.